Changed MX:T:DateTime to use MX:Types. Updated MX:T:DateTimeX to respect changes...
John Napiorkowski [Tue, 3 Jun 2008 01:37:26 +0000 (01:37 +0000)]
Changes
Makefile.PL
lib/MooseX/Types/DateTime.pm
lib/MooseX/Types/DateTimeX.pm
t/01_basic.t
t/02_datetimex.t

diff --git a/Changes b/Changes
index f73dc88..7416a4d 100755 (executable)
--- a/Changes
+++ b/Changes
@@ -4,3 +4,6 @@ Revision history for DBIx-Class-PopulateMore
         First version, released on an unsuspecting world.
 0.02    25 May 2008
         Created the MooseX::Types::DateTimeX namespace for extensions.
+0.03    02 June 2008
+        -- Changed MooseX::Types::Datetime to use MooseX::Types.
+               -- Added Duration coercion for MooseX::Types::DateTimeX
\ No newline at end of file
index a615314..3327c1c 100644 (file)
@@ -19,6 +19,7 @@ WriteMakefile(
         'Test::Exception'    => 0,
                'MooseX::Types'      => '0.04',
                'DateTimeX::Easy'    => '0.082',
+               'Time::Duration::Parse' => '0.06',
     },
 );
 
index 2d50d72..a57aea0 100644 (file)
@@ -5,49 +5,63 @@ package MooseX::Types::DateTime;
 use strict;
 use warnings;
 
-our $VERSION = "0.01";
+our $VERSION = "0.03";
 
 use DateTime ();
 use DateTime::Locale ();
 use DateTime::TimeZone ();
 
-use Moose::Util::TypeConstraints;
+use MooseX::Types::Moose qw/Num HashRef Str/;
+use MooseX::Types
+    -declare => [qw( DateTime Duration TimeZone )];
 
 class_type "DateTime";
 class_type "DateTime::Duration";
 class_type "DateTime::TimeZone";
 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
 
-coerce "DateTime" => (
-    from "Num",
-    via { DateTime->from_epoch( epoch => $_ ) },
-    from "HashRef",
-    via { DateTime->new( %$_ ) },
-    Moose::Meta::TypeConstraint->new(
-        name => "__ANON__",
-        parent => find_type_constraint("Str"),
-        constraint => sub { $_ eq 'now' },
-        optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
-    ),
-    via { DateTime->now },
-);
-
-coerce "DateTime::Duration" => (
-    from "Num",
-    via { DateTime::Duration->new( seconds => $_ ) },
-    from "HashRef",
-    via { DateTime::Duration->new( %$_ ) },
-);
-
-coerce "DateTime::TimeZone" => (
-    from "Str",
-    via { DateTime::TimeZone->new( name => $_ ) },
-);
+subtype DateTime, as 'DateTime';
+subtype Duration, as 'DateTime::Duration';
+subtype TimeZone, as 'DateTime::TimeZone';
+
+
+for my $type ( "DateTime", DateTime ) {
+    coerce $type => (
+               from Num,
+               via { 'DateTime'->from_epoch( epoch => $_ ) },
+               from HashRef,
+               via { 'DateTime'->new( %$_ ) },
+               Moose::Meta::TypeConstraint->new(
+                       name => "__ANON__",
+                       parent => find_type_constraint("Str"),
+                       constraint => sub { $_ eq 'now' },
+                       optimise_as => sub { no warnings 'uninitialized'; !ref($_[0]) and $_[0] eq 'now' },
+               ),
+               via { 'DateTime'->now },
+       );
+}
+
+for my $type ( "DateTime::Duration", Duration ) {
+       coerce $type => (
+               from Num,
+               via { DateTime::Duration->new( seconds => $_ ) },
+               from HashRef,
+               via { DateTime::Duration->new( %$_ ) },
+       );
+}
+
+for my $type ( "DateTime::TimeZone", TimeZone ) {
+       coerce $type => (
+               from Str,
+               via { DateTime::TimeZone->new( name => $_ ) },
+       );
+
+}
 
 coerce "DateTime::Locale" => (
     from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
     via { DateTime::Locale->load($_->language_tag) },
-    from "Str",
+    from Str,
     via { DateTime::Locale->load($_) },
 );
 
@@ -64,10 +78,24 @@ Moose
 
 =head1 SYNOPSIS
 
+Export Example:
+
+       use MooseX::Types::DateTime qw(TimeZone);
+
+    has time_zone => (
+        isa => TimeZone,
+        is => "rw",
+        coerce => 1,
+    );
+
+    Class->new( time_zone => "Africa/Timbuktu" );
+
+Namespaced Example:
+
        use MooseX::Types::DateTime;
 
     has time_zone => (
-        isa => "DateTime::TimeZone",
+        isa => 'DateTime::TimeZone',
         is => "rw",
         coerce => 1,
     );
@@ -100,7 +128,7 @@ Calls L<DateTime/new> with the hash entries as arguments.
 
 =back
 
-=item L<DateTime::Duration>
+=item L<Duration>
 
 A class type for L<DateTime::Duration>
 
index 19c37e6..ca21a9b 100755 (executable)
@@ -3,15 +3,17 @@ package MooseX::Types::DateTimeX;
 use strict;
 use warnings;
 
-our $VERSION = "0.02";
+our $VERSION = "0.03";
 our $AUTHORITY = 'cpan:JJNAPIORK';
 
 use DateTime;
+use DateTime::Duration;
 use DateTimeX::Easy; 
+use Time::Duration::Parse ();
 use MooseX::Types::DateTime;  
 use MooseX::Types::Moose qw/Num HashRef Str/;
-use MooseX::Types 
-  -declare => [qw( DateTime )];
+use MooseX::Types -declare => [qw( DateTime Duration)];
+
   
 =head1 NAME
 
@@ -71,13 +73,36 @@ coerce DateTime,
   via { DateTimeX::Easy->new($_, default_time_zone=>'UTC') };
 
 
+=head2 Duration
+
+Subtype of 'DateTime::Duration' that coerces from a string.  We use the module
+L<Time::Duration::Parse> to attempt this.
+
+=cut
+
+subtype Duration,
+  as 'DateTime::Duration'; ## From MooseX::Types::Duration
+
+coerce Duration,
+  from Num,
+  via { DateTime::Duration->new( seconds => $_ ) },
+  from HashRef,
+  via { DateTime::Duration->new( %$_ ) },
+  from Str,
+  via { 
+       DateTime::Duration->new( 
+               seconds => Time::Duration::Parse::parse_duration($_)
+       )}; 
+
 =head1 AUTHOR
 
 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
 
-=head1 COPYRIGHT
+=head1 LICENSE
 
-       Copyright (c) 2008 John Napiorkowski. All rights reserved
+       Copyright (c) 2008 John Napiorkowski.
+       
        This program is free software; you can redistribute
        it and/or modify it under the same terms as Perl itself.
 
index 3a61db1..f4a4626 100644 (file)
@@ -129,3 +129,22 @@ isa_ok( find_type_constraint($_), "Moose::Meta::TypeConstraint" ) for qw(DateTim
         isa_ok( Gorch->new( loc => $handle )->loc, "DateTime::Locale::ja", "coerced from maketext" );;
     }
 }
+
+{
+       {
+               package Gondor;
+               
+               use Moose;
+               use MooseX::Types::DateTime qw(DateTime Duration);
+       
+               has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
+               has 'duration' => (is=>'rw', isa=>Duration, coerce=>1); 
+               
+       }
+       
+       my $epoch = time;
+       
+       ok my $gondor = Gondor->new(date=>$epoch, duration=>10)
+       => 'Instantiated object using export types';
+       
+}
index 0d48a1d..36e0d23 100755 (executable)
@@ -3,7 +3,7 @@ use warnings;
 
 BEGIN {
 
-       use Test::More tests => 26;
+       use Test::More tests => 30;
        use Test::Exception;
        use DateTime;
        
@@ -12,11 +12,13 @@ BEGIN {
 
 =head1 NAME
 
-String Coercion; Check that we can properly coerce a string.
+t/02_datetimex.t - Check that we can properly coerce a string.
 
 =head1 DESCRIPTION
 
-Make sure all the utility stuff works as expected
+Run some tests to make sure the the Duration and DateTime types continue to
+work exactly as from the L<MooseX::Types::DateTime> class, as well as perform
+the correct string to object coercions.
 
 =head1 TESTS
 
@@ -32,9 +34,10 @@ Create a L<Moose> class that is using the L<MooseX::Types::DateTimeX> types.
        package MooseX::Types::DateTimeX::CoercionTest;
        
        use Moose;
-       use MooseX::Types::DateTimeX qw(DateTime);
+       use MooseX::Types::DateTimeX qw(DateTime Duration);
        
        has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
+       has 'duration' => (is=>'rw', isa=>Duration, coerce=>1); 
 }
 
 ok my $class = MooseX::Types::DateTimeX::CoercionTest->new
@@ -137,6 +140,25 @@ ok $class->date({year=>2000,month=>1,day=>10})
        is $class->date => '2000-01-10T00:00:00'
        => 'Got correct DateTime';
        
+=head2 check duration
+
+make sure the Duration type constraint works as expected
+
+=cut
+
+ok $class->duration(100)
+=> 'got duration from integer';
+
+       is $class->duration->seconds, 100
+       => 'got correct duration from integer';
+       
+
+ok $class->duration('1 minute')
+=> 'got duration from string';
+
+       is $class->duration->seconds, 60
+       => 'got correct duration string';
+       
        
 =head1 AUTHOR