From 3f8cbe6bf3bf305c9bb77d8f15b1ad2143ee440a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 16 Jun 2009 22:23:03 -0500 Subject: split type constraints out into a type lib, with proper coercions --- lib/WWW/Unfuddle/Meta/Types.pm | 20 ++++++++++++++++++++ lib/WWW/Unfuddle/Project.pm | 23 ++++++++++++++--------- 2 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 lib/WWW/Unfuddle/Meta/Types.pm diff --git a/lib/WWW/Unfuddle/Meta/Types.pm b/lib/WWW/Unfuddle/Meta/Types.pm new file mode 100644 index 0000000..344a231 --- /dev/null +++ b/lib/WWW/Unfuddle/Meta/Types.pm @@ -0,0 +1,20 @@ +package WWW::Unfuddle::Meta::Types; +use Moose::Util::TypeConstraints; +use DateTime::Format::ISO8601; +use JSON; + +class_type 'JSON::Boolean'; +subtype 'WWW::Unfuddle::Bool', as 'Bool', where { !ref($_) }; +coerce 'WWW::Unfuddle::Bool', from 'JSON::Boolean', + via { $_ == JSON::true }; + +# 2009-06-16T22:55:06Z +class_type 'DateTime'; +subtype 'WWW::Unfuddle::DateTime', as 'DateTime'; +coerce 'WWW::Unfuddle::DateTime', from 'Str', + via { DateTime::Format::ISO8601->new->parse_datetime($_) }; + +enum 'WWW::Unfuddle::Assignee' => qw(reporter none nochange); +enum 'WWW::Unfuddle::ThemeColor' => qw(blue green grey orange purple red teal); + +1; diff --git a/lib/WWW/Unfuddle/Project.pm b/lib/WWW/Unfuddle/Project.pm index 5d0d79f..a675407 100644 --- a/lib/WWW/Unfuddle/Project.pm +++ b/lib/WWW/Unfuddle/Project.pm @@ -1,6 +1,6 @@ package WWW::Unfuddle::Project; use Moose; -use Moose::Util::TypeConstraints; # XXX +use WWW::Unfuddle::Meta::Types; has account_id => ( is => 'ro', @@ -9,27 +9,30 @@ has account_id => ( has archived => ( is => 'ro', - isa => 'Bool', + isa => 'WWW::Unfuddle::Bool', + coerce => 1, ); has assignee_on_resolve => ( is => 'ro', - isa => enum(qw(reporter none nochange)), + isa => 'WWW::Unfuddle::Assignee', ); has close_ticket_simultaneously_default => ( is => 'ro', - isa => 'Bool', + isa => 'WWW::Unfuddle::Bool', + coerce => 1, ); has created_at => ( is => 'ro', - isa => 'DateTime', + isa => 'WWW::Unfuddle::DateTime', + coerce => 1, ); has default_ticket_report_id => ( is => 'ro', - isa => 'Int', + isa => 'Maybe[Int]', ); has description => ( @@ -44,7 +47,8 @@ has disk_usage => ( has enable_time_tracking => ( is => 'ro', - isa => 'Bool', + isa => 'WWW::Unfuddle::Bool', + coerce => 1, ); has id => ( @@ -59,7 +63,7 @@ has short_name => ( has theme => ( is => 'ro', - isa => enum(qw(blue green grey orange purple red teal)), + isa => 'WWW::Unfuddle::ThemeColor', ); has title => ( @@ -69,7 +73,8 @@ has title => ( has updated_at => ( is => 'ro', - isa => 'DateTime', + isa => 'WWW::Unfuddle::DateTime', + coerce => 1, ); __PACKAGE__->meta->make_immutable; -- cgit v1.2.3