repository moved to https://github.com/moose/MooseX-Types-DateTime
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTime.pm
index 4d44aad..a808a4e 100644 (file)
@@ -1,18 +1,20 @@
-#!/usr/bin/perl
-
 package MooseX::Types::DateTime;
 
 use strict;
 use warnings;
 
-our $VERSION = "0.01";
+use 5.008003;
+use Moose 0.41 ();
+use DateTime 0.4302 ();
+use DateTime::Duration 0.4302 ();
+use DateTime::Locale 0.4001 ();
+use DateTime::TimeZone 0.95 ();
+
+use MooseX::Types::Moose 0.30 qw/Num HashRef Str/;
 
-use DateTime ();
-use DateTime::Locale ();
-use DateTime::TimeZone ();
+use namespace::clean 0.08;
 
-use MooseX::Types::Moose qw/Num HashRef Str/;
-use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
+use MooseX::Types 0.30 -declare => [qw( DateTime Duration TimeZone Locale Now )];
 
 class_type "DateTime";
 class_type "DateTime::Duration";
@@ -27,51 +29,53 @@ subtype Locale,   as 'DateTime::Locale';
 subtype( Now,
     as Str,
     where { $_ eq 'now' },
-    Moose::Util::TypeConstraints::optimize_as {
-        no warnings 'uninitialized';
-        !ref($_[0]) and $_[0] eq 'now';
-    },
+    ($Moose::VERSION >= 2.0100
+        ? Moose::Util::TypeConstraints::inline_as {
+           'no warnings "uninitialized";'.
+           '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
+        }
+        : Moose::Util::TypeConstraints::optimize_as {
+            no warnings 'uninitialized';
+            !ref($_[0]) and $_[0] eq 'now';
+        }
+    ),
+);
+
+our %coercions = (
+    DateTime => [
+        from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
+        from HashRef, via { 'DateTime'->new( %$_ ) },
+        from Now, via { 'DateTime'->now },
+    ],
+    "DateTime::Duration" => [
+        from Num, via { DateTime::Duration->new( seconds => $_ ) },
+        from HashRef, via { DateTime::Duration->new( %$_ ) },
+    ],
+    "DateTime::TimeZone" => [
+        from Str, via { DateTime::TimeZone->new( name => $_ ) },
+    ],
+    "DateTime::Locale" => [
+        from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
+        via { DateTime::Locale->load($_->language_tag) },
+        from Str, via { DateTime::Locale->load($_) },
+    ],
 );
 
 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 },
-       );
+    coerce $type => @{ $coercions{DateTime} };
 }
 
 for my $type ( "DateTime::Duration", Duration ) {
-       coerce $type => (
-               from Num,
-               via { DateTime::Duration->new( seconds => $_ ) },
-               from HashRef,
-               via { DateTime::Duration->new( %$_ ) },
-       );
+    coerce $type => @{ $coercions{"DateTime::Duration"} };
 }
 
 for my $type ( "DateTime::TimeZone", TimeZone ) {
-       coerce $type => (
-               from Str,
-               via { DateTime::TimeZone->new( name => $_ ) },
-       );
-
+    coerce $type => @{ $coercions{"DateTime::TimeZone"} };
 }
 
-coerce "DateTime::Locale" => (
-    from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
-    via { DateTime::Locale->load($_->language_tag) },
-    from Str,
-    via { DateTime::Locale->load($_) },
-);
+for my $type ( "DateTime::Locale", Locale ) {
+    coerce $type => @{ $coercions{"DateTime::Locale"} };
+}
 
 __PACKAGE__
 
@@ -88,7 +92,7 @@ Moose
 
 Export Example:
 
-       use MooseX::Types::DateTime qw(TimeZone);
+    use MooseX::Types::DateTime qw(TimeZone);
 
     has time_zone => (
         isa => TimeZone,
@@ -98,9 +102,11 @@ Export Example:
 
     Class->new( time_zone => "Africa/Timbuktu" );
 
+=for stopwords Namespaced
+
 Namespaced Example:
 
-       use MooseX::Types::DateTime;
+    use MooseX::Types::DateTime;
 
     has time_zone => (
         isa => 'DateTime::TimeZone',
@@ -127,8 +133,8 @@ A class type for L<DateTime>.
 
 =item from C<Num>
 
-Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
-percision, see L<DateTime> for details.
+Uses L<DateTime/from_epoch>. Floating values will be used for sub-second
+precision, see L<DateTime> for details.
 
 =item from C<HashRef>
 
@@ -187,19 +193,26 @@ Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
 
 =back
 
-=head1 VERSION CONTROL
+=back
+
+=back
 
-L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
-#moose for commit bits.
+=head1 SEE ALSO
+
+L<MooseX::Types::DateTime::MoreCoercions>
+
+L<DateTime>, L<DateTimeX::Easy>
 
 =head1 AUTHOR
 
 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
 
+John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
+
 =head1 COPYRIGHT
 
-       Copyright (c) 2008 Yuval Kogman. All rights reserved
-       This program is free software; you can redistribute
-       it and/or modify it under the same terms as Perl itself.
+    Copyright (c) 2008 Yuval Kogman. All rights reserved
+    This program is free software; you can redistribute
+    it and/or modify it under the same terms as Perl itself.
 
 =cut