Skip to content

Instantly share code, notes, and snippets.

@deppp
Created November 4, 2009 18:30
Show Gist options
  • Save deppp/226274 to your computer and use it in GitHub Desktop.
Save deppp/226274 to your computer and use it in GitHub Desktop.
package App::Triangle;
use Moose;
use Moose::Util::TypeConstraints;
extends 'App::Shape';
subtype 'Vertices'
=> as 'ArrayRef[App::Point]'
=> where { $#{$_} eq 2 }
=> message { "triangle has exactly three vertices" };
coerce 'Vertices'
=> from 'ArrayRef[Str]'
=> via {
my $key = { map split ('x', $_), @$_ };
[ map { App::Point->new(x => $_, y => $key->{$_}) } keys %$key ];
};
has 'vertices' => (
traits => ['Array'],
is => 'rw',
isa => 'Vertices',
coerce => 1,
required => 1,
handles => {
get_vertice => 'get',
all_vertices => 'elements'
}
);
sub as_svg {
my $self = shift;
# pay attention that we access edges with all_adges call, it's defined
# in "handles" argument to our attribute and it also does an automatic
# dereferencing for us.
# take a look at Moose::Meta::Attribute::Native::Trait::Array to see
# what else you can do
my $points = join ' ', map { $_->x . "," . $_->y } $self->all_vertices;
my $str = '<svg:polygon points="' . $points . '" fill="#' . $self->color_as_hex . '" />';
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment