MooseX::Types::DateTime
Yuval Kogman [Fri, 18 Apr 2008 15:52:21 +0000 (15:52 +0000)]
MANIFEST.SKIP [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
lib/MooseX/Types/DateTime.pm [new file with mode: 0644]
t/01_basic.t [new file with mode: 0644]

diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP
new file mode 100644 (file)
index 0000000..10b9324
--- /dev/null
@@ -0,0 +1,38 @@
+# Avoid version control files.
+\bRCS\b
+\bCVS\b
+\bSCCS\b
+,v$
+\B\.svn\b
+\b_darcs\b
+
+# Avoid Makemaker generated and utility files.
+\bMANIFEST\.bak
+\bMakefile$
+\bblib/
+\bMakeMaker-\d
+\bpm_to_blib\.ts$
+\bpm_to_blib$
+\bblibdirs\.ts$         # 6.18 through 6.25 generated this
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\b_build/
+
+# Avoid temp and backup files.
+~$
+\.old$
+\#$
+\b\.#
+\.bak$
+
+# Avoid Devel::Cover files.
+\bcover_db\b
+
+### DEFAULT MANIFEST.SKIP ENDS HERE ####
+
+\.DS_Store$
+\.sw.$
+(\w+-)*(\w+)-\d\.\d+(?:\.tar\.gz)?$
+
+\.t\.log$
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..0304c07
--- /dev/null
@@ -0,0 +1,22 @@
+#!/usr/bin/perl -w
+
+use strict;
+
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+       NAME         => 'MooseX::Types::DateTime',
+       VERSION_FROM => 'lib/MooseX/Types/DateTime.pm',
+       INSTALLDIRS  => 'site',
+       SIGN         => 1,
+       PL_FILES     => { },
+    PREREQ_PM => {
+        'Moose'              => '0.41', # class_type
+        'DateTime'           => 0,
+        'DateTime::Locale'   => 0,
+        'DateTime::TimeZone' => 0,
+        'Test::use::ok'      => 0,
+        'Test::Exception'    => 0,
+    },
+);
+
diff --git a/lib/MooseX/Types/DateTime.pm b/lib/MooseX/Types/DateTime.pm
new file mode 100644 (file)
index 0000000..c1e7c19
--- /dev/null
@@ -0,0 +1,100 @@
+#!/usr/bin/perl
+
+package MooseX::Types::DateTime;
+
+use strict;
+use warnings;
+
+our $VERSION = "0.01";
+
+use DateTime ();
+use DateTime::Locale ();
+use DateTime::TimeZone ();
+
+use Moose::Util::TypeConstraints;
+
+class_type "DateTime";
+class_type "DateTime::TimeZone";
+class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
+
+coerce "DateTime" => (
+    from "Int",
+    via { DateTime->from_epoch( epoch => $_ ) },
+);
+
+coerce "DateTime::TimeZone" => (
+    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",
+    via { DateTime::Locale->load($_) },
+);
+
+__PACKAGE__
+
+__END__
+
+=pod
+
+=head1 NAME
+
+MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
+Moose
+
+=head1 SYNOPSIS
+
+       use MooseX::Types::DateTime;
+
+    has time_zone => (
+        isa => "DateTime::TimeZone",
+        is => "rw",
+        coerce => 1,
+    );
+
+    Class->new( time_zone => "Africa/Timbuktu" );
+
+=head1 DESCRIPTION
+
+This module packages several L<Moose::Util::TypeConstraints> with coercions,
+designed to work with the L<DateTime> suite of objects.
+
+=head1 CONSTRAINTS
+
+=over 4
+
+=item L<DateTime>
+
+A coercion from C<Int> using L<DateTime/from_epoch> is defined.
+
+=item L<DateTime::Locale>
+
+Coerces from C<Str>, where the string is the language tag, e.g. C<en> etc. See
+L<DateTime::Locale/load>.
+
+=item L<DateTime::TimeZone>
+
+Coerces from C<Str> where the string is any time zone name.
+
+The string may also be a number of special values (C<local>, C<floating>,
+offsets, etc). See L<DateTime::TimeZone/USAGE> for details.
+
+=head1 VERSION CONTROL
+
+L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
+#moose for commit bits.
+
+=head1 AUTHOR
+
+Yuval Kogman E<lt>nothingmuch@woobling.orgE<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.
+
+=cut
diff --git a/t/01_basic.t b/t/01_basic.t
new file mode 100644 (file)
index 0000000..d137131
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+use Test::Exception;
+
+use ok 'MooseX::Types::DateTime';
+
+use Moose::Util::TypeConstraints;
+
+isa_ok( find_type_constraint($_), "Moose::Meta::TypeConstraint" ) for qw(DateTime DateTime::TimeZone DateTime::Locale);
+
+{
+    {
+        package Foo;
+        use Moose;
+
+        has date => (
+            isa => "DateTime",
+            is  => "rw",
+            coerce => 1,
+        );
+    }
+
+    my $epoch = time;
+
+    my $coerced = Foo->new( date => $epoch )->date;
+
+    isa_ok( $coerced, "DateTime", "coerced epoch into datetime" );
+
+    is( $coerced->epoch, $epoch, "epoch is correct" );
+
+    throws_ok { Foo->new( date => "junk1!!" ) } qr/DateTime/, "constraint";
+}
+
+{
+    {
+        package Bar;
+        use Moose;
+
+        has time_zone => (
+            isa => "DateTime::TimeZone",
+            is  => "rw",
+            coerce => 1,
+        );
+    }
+
+    my $tz = Bar->new( time_zone => "Africa/Timbuktu" )->time_zone;
+
+    isa_ok( $tz, "DateTime::TimeZone", "coerced string into time zone object" );
+
+    like( $tz->name, qr/^Africa/, "correct time zone" );
+
+    dies_ok { Bar->new( time_zone => "Space/TheMoon" ) } "bad time zone";
+}
+
+{
+    {
+        package Gorch;
+        use Moose;
+
+        has loc => (
+            isa => "DateTime::Locale",
+            is  => "rw",
+            coerce => 1,
+        );
+    }
+
+    my $loc = Gorch->new( loc => "he_IL" )->loc;
+
+    isa_ok( $loc, "DateTime::Locale::he" );
+
+    dies_ok { Gorch->new( loc => "not_a_place_or_a_locale" ) } "bad locale name";
+
+    SKIP: {
+        skip "No Locale::Maketext", 2 unless eval { require Locale::Maketext };
+        
+        {
+            package Some::L10N;
+            our @ISA = qw(Locale::Maketext);
+
+            package Some::L10N::ja;
+            our @ISA = qw(Some::L10N);
+
+            our %Lexicon = (
+                goodbye => "sayonara",
+            );
+        }
+
+        my $handle = Some::L10N->get_handle("ja");
+
+        isa_ok( $handle, "Some::L10N", "maketext handle" );
+
+        isa_ok( Gorch->new( loc => $handle )->loc, "DateTime::Locale::ja", "coerced from maketext" );;
+    }
+}