Leser: 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package PIMYGC::Types::Library; use MooseX::Types -declare => [ qw( GoogleUrlId ) ]; use MooseX::Types::Moose qw/HashRef Str/; subtype GoogleUrlId, as HashRef[Str], where { $_ =~ m/\/([0-9a-z]+)$/xms }; coerce GoogleUrlId, from Str, via { my ($id) = $_ =~ m/\/([0-9a-z]+)$/xms; { full => $_, id => $id, } };
http://www.google.com/m8/feeds/contacts/email%40gmail.com/full/6578ac42881547eb
1 2 3 4 5 6 7 8 9 10 11
package PIMYGC::Foo; use Moose; #... use PIMYGC::Types::Library qw /GoogleUrlId/; has 'gcid' => ( is => 'rw', isa => GoogleUrlId, required => 1 );
1 2
new PIMYGC::Foo( gcid => 'http://www.google.com/m8/feeds/contacts/email%40gmail.com/full/6578ac42881547eb'
1
2
Attribute (gcid) does not pass the type constraint because: Validation failed for 'PIMYGC::Types::Library::GoogleUrlId' with value http://www.google.com/m8/feeds/contacts/email%40gmail.com/full/6578ac42881547eb at /var/www/foobar.de/cgi-bin/experiments/googlecontacts/lib/Controller/Test.pm line 47
Controller::Test::load('Controller::Test=HASH(0xc88390)') called at /var/www/foobar.de/cgi-bin/experiments/googlecontacts/core.pl line 51
1 2 3 4
subtype GoogleUrlId, as HashRef[Str], # where { $_ =~ m/\/([0-9a-z]+)$/xms }; where { $_->{full} =~ m/\/([0-9a-z]+)$/xms };
1 2 3 4 5 6 7 8 9
[ bless( { 'value' => 'mail@gmail.com', 'primary' => 1, 'type' => bless( { 'uri' => 'http://schemas.google.com/g/2005#other' }, 'WWW::Google::Contacts::Type::Rel' ) }, 'WWW::Google::Contacts::Type::Email' ) ]
1 2 3 4 5 6 7
my @primary = map { if ($_->{primary} == 1) { $_->{value} } } @$_; return $primary[0];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# GooglePrimaryEmail subtype GooglePrimaryEmail, as EmailAddress, where { $_ }, message { 'validation failed for: '. Dumper $_; }; coerce GooglePrimaryEmail, from ArrayRef[HashRef[Str]], via { my @primary = map { if ($_->{primary} == 1) { $_->{value} } } @$_; return $primary[0]; };
QuoteNunja, ein WWW::Google::Contacts::Type::Email Objekt ist eine "gesegnete" Hash-Referenz. Außerdem sind nicht nur Strings hinter den Schlüsseln gespeichert.In meinem Fall ist das eine
ArrayRef
-> Ref auf WWW::Google::Contacts::Type::Email
-> HashRef
-> Strings
QuoteDie gesamte Struktur muss nicht anagegeben werden. In deinem Fall würde ich from => ArrayRef[WWW::Google::Contacts::Type::Email] verwenden. Ansonsten würde dort auch ArrayRef, oder Ref, oder Defined, oder ... ausreichen.Allerdings braucht man, wenn man coerce benutzen will auch immer den Zustand der Ursprungsdatenstruktur.
1 2 3 4 5
via => { my $p = (grep { $_->{primary} } @$_)[0]; $p = {%$p}; # macht aus $p eine normale HashRef return $p; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# WWWGoogleContactsTypeEmail class_type WWWGoogleContactsTypeEmail, { class => 'WWW::Google::Contacts::Type::Email', }; # GooglePrimaryEmail subtype GooglePrimaryEmail, as EmailAddress, message { 'validation failed for: '. Dumper $_; }; coerce GooglePrimaryEmail, from ArrayRef[WWWGoogleContactsTypeEmail], via { my $p = (grep { $_->{primary} } @$_)[0]; $p = $p ? $p->{value} : @$_->[0]{value}; # no prim email -> take first address return $p; };
from => ArrayRef['WWW::Google::Contacts::Type::Email'],
sub WWW::Google::Contacts::Type::Email () { 'WWW::Google::Contacts::Type::Email' }
1 2 3 4 5 6 7 8 9 10 11 12 13
around('initialize_instance_slot', sub { my $orig = shift; my $self = shift; my $ia = $self->init_arg; # $_[2] is the hashref of options passed to the constructor. If our # parameter passed in was undef, pop it off the args... pop unless (exists($_[2]->{$ia}) && defined($_[2]->{$ia})); # Invoke the real init, as the above line cleared the unef $self->$orig(@_) });
1 2 3 4 5 6
use MooseX::Declare; class foo { has bar => (is => 'rw', isa => 'Maybe[Str]', ...); ... } # foo