File Coverage

blib/lib/MooseX/Types/DateTime/MoreCoercions.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 48 48 100.0


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