Created
October 9, 2012 16:49
-
-
Save Woody2143/3859990 to your computer and use it in GitHub Desktop.
coerce doesn't work for me; I'm doing something wrong
This file contains hidden or 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 Index; | |
use Mouse; | |
use Mouse::Util::TypeConstraints; | |
subtype 'phonenumber', | |
as 'Str', | |
where { length($_) > 6 }, | |
message { "This number ($_) is not a valid phone number!" }; | |
coerce 'phonenumber', | |
from 'Str', | |
via { | |
s/[^\d]//g; # Strip anything that isn't a digit. | |
s/^1//g; # Strip leading 1 | |
}; | |
subtype 'testdate', | |
as 'Str', | |
where { length($_) > 7; }, | |
message { "This date ($_) is not a valid date!"}; | |
coerce 'testdate', | |
from 'Str', | |
via { | |
s/[^\d]//g; # Strip anything that isn't a digit. | |
}; | |
has 'telephonenumber' => ( | |
is => 'rw', | |
isa => 'phonenumber', | |
coerce => 1, | |
required => 1, | |
); | |
has 'date' => ( | |
is => 'rw', | |
isa => 'testdate', | |
coerce => 1, | |
required => 1, | |
); | |
has 'files' => ( | |
is => 'ro', | |
isa => 'ArrayRef', | |
); | |
__PACKAGE__->meta->make_immutable(); |
This file contains hidden or 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 Index::Test; | |
use base 'Test::Class'; | |
use Index; | |
use Test::More; | |
sub object : Test(1) { | |
my $index = Index->new( telephonenumber => '4045551212', date => "20121008" ); | |
isa_ok( $index, Index); | |
} | |
sub number : Test(2) { | |
my $index = Index->new( telephonenumber => "1 (404) 555-1212", date => "20121008" ); | |
isa_ok( $index, Index); | |
is( $index->telephonenumber(), '4045551212', 'Telephone number is stripped of non-digits matches' ); | |
} | |
sub date : Test(2) { | |
my $index = Index->new( telephonenumber => '1 (404) 555-1212', date => "2012/10/08" ); | |
isa_ok( $index, Index); | |
is( $index->date(), '20121008', 'Date is stripped of non-digits matches' ); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment