Created MX:T:DateTimeX as extensions to the MX:T:DateTime, created test for new modul...
John Napiorkowski [Wed, 28 May 2008 02:02:30 +0000 (02:02 +0000)]
Changes [new file with mode: 0755]
Makefile.PL
lib/MooseX/Types/DateTimeX.pm [new file with mode: 0755]
t/02_datetimex.t [new file with mode: 0755]

diff --git a/Changes b/Changes
new file mode 100755 (executable)
index 0000000..f73dc88
--- /dev/null
+++ b/Changes
@@ -0,0 +1,6 @@
+Revision history for DBIx-Class-PopulateMore
+
+0.01    18 April 2008
+        First version, released on an unsuspecting world.
+0.02    25 May 2008
+        Created the MooseX::Types::DateTimeX namespace for extensions.
index 0304c07..a615314 100644 (file)
@@ -17,6 +17,8 @@ WriteMakefile(
         'DateTime::TimeZone' => 0,
         'Test::use::ok'      => 0,
         'Test::Exception'    => 0,
+               'MooseX::Types'      => '0.04',
+               'DateTimeX::Easy'    => '0.082',
     },
 );
 
diff --git a/lib/MooseX/Types/DateTimeX.pm b/lib/MooseX/Types/DateTimeX.pm
new file mode 100755 (executable)
index 0000000..19c37e6
--- /dev/null
@@ -0,0 +1,86 @@
+package MooseX::Types::DateTimeX;
+
+use strict;
+use warnings;
+
+our $VERSION = "0.02";
+our $AUTHORITY = 'cpan:JJNAPIORK';
+
+use DateTime;
+use DateTimeX::Easy; 
+use MooseX::Types::DateTime;  
+use MooseX::Types::Moose qw/Num HashRef Str/;
+use MooseX::Types 
+  -declare => [qw( DateTime )];
+  
+=head1 NAME
+
+MooseX::Types::DateTimeX - Extensions to L<MooseX::Types::DateTime>
+
+=head1 SYNOPSIS
+
+       package MyApp::MyClass;
+       
+    use MooseX::Types::DateTimeX qw( DateTime );
+       
+    has created => (
+        isa => DateTime,
+        is => "rw",
+        coerce => 1,
+    );
+       
+       my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
+       print $instance->created->year; # is 1980
+       
+       ## Coercions from the base type continue to work as normal.
+       my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
+
+Please see the test case for more example usage.
+
+=head1 DESCRIPTION
+
+This module builds on L<MooseX::Types::DateTime> to add additional custom
+types and coercions.  Since it builds on an existing type, all coercions and
+constraints are inherited.
+
+=head1 SUBTYPES
+
+This module defines the following additional subtypes.
+
+=head2 DateTime
+
+Subtype of 'DateTime'.  Adds an additional coercion from strings.
+
+Uses L<DateTimeX::Easy> to try and convert strings, like "yesterday" into a 
+valid L<DateTime> object.  Please note that due to ambiguity with how different
+systems might localize their timezone, string parsing may not always return 
+the most expected value.  IN general we try to localize to UTC whenever
+possible.  Feedback welcomed!
+
+=cut
+
+subtype DateTime,
+  as 'DateTime'; ## From MooseX::Types::DateTime
+
+coerce DateTime,
+  from Num,
+  via { 'DateTime'->from_epoch( epoch => $_ ) },
+  from HashRef,
+  via { 'DateTime'->new( %$_ ) },
+  from Str,
+  via { DateTimeX::Easy->new($_, default_time_zone=>'UTC') };
+
+
+=head1 AUTHOR
+
+John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
+
+=head1 COPYRIGHT
+
+       Copyright (c) 2008 John Napiorkowski. All rights reserved
+       This program is free software; you can redistribute
+       it and/or modify it under the same terms as Perl itself.
+
+=cut
+
+1;
diff --git a/t/02_datetimex.t b/t/02_datetimex.t
new file mode 100755 (executable)
index 0000000..0d48a1d
--- /dev/null
@@ -0,0 +1,154 @@
+use strict;
+use warnings;
+
+BEGIN {
+
+       use Test::More tests => 26;
+       use Test::Exception;
+       use DateTime;
+       
+       use_ok 'MooseX::Types::DateTimeX';
+}
+
+=head1 NAME
+
+String Coercion; Check that we can properly coerce a string.
+
+=head1 DESCRIPTION
+
+Make sure all the utility stuff works as expected
+
+=head1 TESTS
+
+This module defines the following tests.
+
+=head2 Test Class
+
+Create a L<Moose> class that is using the L<MooseX::Types::DateTimeX> types.
+
+=cut
+
+{
+       package MooseX::Types::DateTimeX::CoercionTest;
+       
+       use Moose;
+       use MooseX::Types::DateTimeX qw(DateTime);
+       
+       has 'date' => (is=>'rw', isa=>DateTime, coerce=>1);
+}
+
+ok my $class = MooseX::Types::DateTimeX::CoercionTest->new
+=> 'Created a good class';
+
+
+=head2 ParseDateTime Capabilities
+
+parse some dates and make sure the system can actually find something.
+
+=cut
+
+ok $class->date('2/13/1969 noon')
+=> "coerced a DateTime from '2/13/1969 noon'";
+
+       is $class->date, '1969-02-13T11:00:00'
+       => 'got correct date';
+
+ok $class->date('2/13/1969')
+=> "coerced a DateTime from '2/13/1969'";
+
+       is $class->date, '1969-02-13T00:00:00'
+       => 'got correct date';
+
+ok $class->date('2/13/1969 America/New_York')
+=> "coerced a DateTime from '2/13/1969 America/New_York'";
+
+       isa_ok $class->date->time_zone => 'DateTime::TimeZone::America::New_York'
+       => 'Got Correct America/New_York TimeZone';
+
+       is $class->date, '1969-02-13T00:00:00'
+       => 'got correct date';
+
+ok $class->date('jan 1 2006')
+=>"coerced a DateTime from 'jan 1 2006'";
+
+       is $class->date, '2006-01-01T00:00:00'
+       => 'got correct date';
+       
+
+=head2 relative dates
+
+Stuff like "yesterday".  We can make sure they returned something but we have
+no way to make sure the values are really correct.  Manual testing suggests
+they work well enough, given the inherent ambiguity we are dealing with.
+
+=cut
+
+ok $class->date('now')
+=> "coerced a DateTime from 'now'";
+
+ok $class->date('yesterday')
+=> "coerced a DateTime from 'yesterday'";
+
+
+ok $class->date('tomorrow')
+=> "coerced a DateTime from 'tomorrow'";
+
+
+ok $class->date('last week')
+=> "coerced a DateTime from 'last week'";
+
+
+=head2 check inherited constraints
+
+Just a few tests to make sure the object, hash, etc coercions and type checks 
+still work.
+
+=cut
+
+ok my $datetime = DateTime->now()
+=> 'Create a datetime object for testing';
+
+ok my $anyobject = bless({}, 'Bogus::Does::Not::Exist')
+=> 'Created a random object for proving the object constraint';
+
+ok $class->date($datetime)
+=> 'Passed Object type constraint test.';
+
+       isa_ok $class->date => 'DateTime'
+       => 'Got a good DateTime Object';
+
+dies_ok { $class->date($anyobject) } 'Does not allow the bad object';
+
+ok $class->date(1000)
+=> 'Passed Num coercion test.';
+
+       isa_ok $class->date => 'DateTime'
+       => 'Got a good DateTime Object';
+       
+       is $class->date => '1970-01-01T00:16:40'
+       => 'Got correct DateTime';
+
+ok $class->date({year=>2000,month=>1,day=>10})
+=> 'Passed HashRef coercion test.';
+
+       isa_ok $class->date => 'DateTime'
+       => 'Got a good DateTime Object';
+       
+       is $class->date => '2000-01-10T00:00:00'
+       => 'Got correct DateTime';
+       
+       
+=head1 AUTHOR
+
+John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
+
+=head1 COPYRIGHT
+
+       Copyright (c) 2008 John Napiorkowski. All rights reserved
+       This program is free software; you can redistribute
+       it and/or modify it under the same terms as Perl itself.
+
+=cut
+
+1;
+