Created
November 4, 2009 18:30
-
-
Save deppp/226274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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