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
'Test::Exception' => 0,
'MooseX::Types' => '0.04',
'DateTimeX::Easy' => '0.082',
+ 'Time::Duration::Parse' => '0.06',
},
);
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($_) },
);
=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,
);
=back
-=item L<DateTime::Duration>
+=item L<Duration>
A class type for L<DateTime::Duration>
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
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.
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';
+
+}
BEGIN {
- use Test::More tests => 26;
+ use Test::More tests => 30;
use Test::Exception;
use DateTime;
=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
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
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