File Coverage

blib/lib/MooseX/Types/DateTime/MoreCoercions.pm
Criterion Covered Total %
statement 37 37 100.0
branch n/a
condition n/a
subroutine 13 13 100.0
pod n/a
total 50 50 100.0


line stmt bran cond sub pod time code
1 1     1   229879 use strict;
  1         2  
  1         26  
2 1     1   4 use warnings;
  1         2  
  1         46  
3             package MooseX::Types::DateTime::MoreCoercions;
4             BEGIN {
5 1     1   27 $MooseX::Types::DateTime::MoreCoercions::AUTHORITY = 'cpan:ILMARI';
6             }
7             # git description: v0.13-2-g38c0d44
8             $MooseX::Types::DateTime::MoreCoercions::VERSION = '0.14';
9             # ABSTRACT: Extensions to L<MooseX::Types::DateTime>
10             # KEYWORDS: moose types constraints coercions date time datetime timestamp
11              
12 1     1   807 use Moose 0.41 ();
  1         436568  
  1         41  
13 1     1   12 use DateTime 0.4302 ();
  1         34  
  1         24  
14 1     1   8 use DateTime::Duration 0.4302 ();
  1         21  
  1         19  
15 1     1   1098 use DateTimeX::Easy 0.085 ();
  1         427890  
  1         189  
16 1     1   1083 use Time::Duration::Parse 0.06 qw(parse_duration);
  1         2457  
  1         8  
17 1     1   993 use MooseX::Types::DateTime 0.07 ();
  1         121389  
  1         57  
18 1     1   12 use MooseX::Types::Moose 0.04 qw/Num HashRef Str/;
  1         15  
  1         19  
19 1     1   5954 use namespace::clean 0.08;
  1         22  
  1         9  
20              
21 1     1   240 use MooseX::Types 0.04 -declare => [qw( DateTime Duration)];
  1         14  
  1         10  
22 1     1   4884 use if MooseX::Types->VERSION >= 0.42, 'namespace::autoclean';
  1         2  
  1         20  
23              
24             subtype DateTime, as MooseX::Types::DateTime::DateTime;
25             coerce( DateTime,
26             @{ MooseX::Types::DateTime::DateTime->coercion->type_coercion_map },
27             from Str, via { DateTimeX::Easy->new($_) },
28             );
29              
30             subtype Duration, as MooseX::Types::DateTime::Duration;
31             coerce( Duration,
32             @{ MooseX::Types::DateTime::Duration->coercion->type_coercion_map },
33             from Str, via {
34             DateTime::Duration->new(
35             seconds => parse_duration($_)
36             );
37             },
38             );
39              
40             1;
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             MooseX::Types::DateTime::MoreCoercions - Extensions to L<MooseX::Types::DateTime>
51              
52             =head1 VERSION
53              
54             version 0.14
55              
56             =head1 SYNOPSIS
57              
58             package MyApp::MyClass;
59              
60             use MooseX::Types::DateTime::MoreCoercions qw( DateTime );
61              
62             has created => (
63             isa => DateTime,
64             is => "rw",
65             coerce => 1,
66             );
67              
68             my $instance = MyApp::MyClass->new(created=>'January 1, 1980');
69             print $instance->created->year; # is 1980
70              
71             ## Coercions from the base type continue to work as normal.
72             my $instance = MyApp::MyClass->new(created=>{year=>2000,month=>1,day=>10});
73              
74             Please see the test case for more example usage.
75              
76             =head1 DESCRIPTION
77              
78             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.
79              
80             =head1 SUBTYPES
81              
82             This module defines the following additional subtypes.
83              
84             =head2 DateTime
85              
86             Subtype of L<MooseX::Types::DateTime/DateTime>. Adds an additional coercion from strings.
87              
88             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!
89              
90             =head2 Duration
91              
92             Subtype of L<MooseX::Types::DateTime/Duration> that coerces from a string. We use the module L<Time::Duration::Parse> to attempt this.
93              
94             =head1 CAVEATS
95              
96             Firstly, this module uses L<DateTimeX::Easy> which is way more DWIM than any sane person would desire. L<DateTimeX::Easy> works by falling back until something makes sense, this is variable. Furthermore, all the modules that L<DateTimeX::Easy> *can* use aren't required for "proper" function of L<DateTimeX::Easy>. What does this mean? Simple, your mileage may vary in your coercions because L<DateTimeX::Easy> is installation specific.
97              
98             =head1 SEE ALSO
99              
100             =over 4
101              
102             =item * L<MooseX::Types::DateTime> Replacement for this module -- coercions with less voodoo
103              
104             =item * L<DateTimeX::Easy> Backend of this module
105              
106             =item * L<Time::Duration::Parse> Duration parsing backend for this module
107              
108             =back
109              
110             =head1 AUTHOR
111              
112             John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
113              
114             Broken into a separate package from L<MooseX::Types::DateTime> by Evan Carroll.
115              
116             Forked from L<MooseX::Types::DateTimeX> and ported back to use
117             L<MooseX::Types::DateTime> by Dagfinn Ilmari MannsÃ¥ker
118             E<lt>ilmari@ilmari.orgE<gt>.
119              
120             =head1 AUTHORS
121              
122             =over 4
123              
124             =item *
125              
126             John Napiorkowski <jjn1056@yahoo.com>
127              
128             =item *
129              
130             Dagfinn Ilmari MannsÃ¥ker <ilmari@ilmari.org>
131              
132             =back
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2008 by John Napiorkowski.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =head1 CONTRIBUTORS
142              
143             =for stopwords Dagfinn Ilmari MannsÃ¥ker Evan Carroll Karen Etheridge gregor herrmann root
144              
145             =over 4
146              
147             =item *
148              
149             Dagfinn Ilmari MannsÃ¥ker <ilmari@photobox.com>
150              
151             =item *
152              
153             Evan Carroll <me+cpan@evancarroll.com>
154              
155             =item *
156              
157             Evan Carroll <me@evancarroll.com>
158              
159             =item *
160              
161             Karen Etheridge <ether@cpan.org>
162              
163             =item *
164              
165             gregor herrmann <gregoa@debian.org>
166              
167             =item *
168              
169             root <root@x60s.(none)>
170              
171             =back
172              
173             =cut