File Coverage

blib/lib/MooseX/Types/Date/Piece.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package MooseX::Types::Date::Piece;
2              
3 1     1   32364 use strict;
  1         3  
  1         58  
4 1     1   6 use warnings;
  1         3  
  1         35  
5 1     1   1068 use namespace::autoclean;
  1         32845  
  1         7  
6              
7             our $VERSION = '0.03';
8              
9 1     1   79 use Carp 'croak';
  1         2  
  1         64  
10 1     1   428 use Date::Piece qw( date years months weeks days );
  0            
  0            
11             use MooseX::Types::Moose qw( ArrayRef Int Str );
12              
13             my %DATE_DURATION = (
14             days => \&days,
15             weeks => \&weeks,
16             months => \&months,
17             years => \&years,
18             );
19              
20             use MooseX::Types -declare => [qw( Date Duration )];
21              
22             class_type 'Date::Piece';
23             class_type 'Date::Piece::Duration', { class => 'Date::Piece::unit_base' };
24              
25             subtype Date, as 'Date::Piece';
26             subtype Duration, as 'Date::Piece::Duration';
27              
28             for my $type ('Date::Piece', Date) {
29             coerce $type,
30             from Str, via { date($_) },
31             from ArrayRef, via { date($_) };
32             }
33              
34             for my $type ('Date::Piece::Duration', Duration) {
35             coerce $type,
36             from Int, via { $_ * days },
37             from Str, via {
38             my $str = lc $_;
39             $str =~ s/([^s])$/$1s/; # ensure there is an 's' at the end
40              
41             my ( $val, $unit ) = $str =~ m/^([+-]*\d+)\s*(\w+)$/;
42              
43             ( defined $val && defined $unit && defined $DATE_DURATION{$unit} )
44             || croak "invalid duration '$str'";
45              
46             return $val * $DATE_DURATION{$unit}->();
47             };
48             }
49              
50             1;
51              
52             __END__
53              
54             =head1 NAME
55              
56             MooseX::Types::Date::Piece - Date::Piece type and coercions for Moose.
57              
58             =head1 SYNOPSIS
59              
60             package Foo;
61              
62             use Moose;
63             use MooseX::Types::Date::Piece qw( Date Duration );
64              
65             has 'date' => (
66             is => 'ro',
67             isa => Date,
68             coerce => 1,
69             );
70              
71             has 'duration' => (
72             is => 'ro',
73             isa => Duration,
74             coerce => 1,
75             );
76              
77             # ...
78              
79             my $f = Foo->new(
80             date => '2012-07-09',
81             duration => '1day',
82             );
83              
84             =head1 DESCRIPTION
85              
86             This module provides L<Moose> type constraints and coercions for using
87             L<Date::Piece> objects as Moose attributes.
88              
89             =head1 EXPORTS
90              
91             The following type constants provided by L<MooseX::Types> must be explicitly
92             imported. The full class name may also be used (as strings with quotes) without
93             importing the constant declarations.
94              
95             =over
96              
97             =item Date
98              
99             A class type for L<Date::Piece>.
100              
101             =over
102              
103             =item coerce from C<Str>
104              
105             Uses L<Date::Piece/date>, where the string is formatted as C<'2012-12-31'> or C<'20121231'>.
106              
107             =item coerce from C<ArrayRef>
108              
109             Uses L<Date::Piece/date>, where the array is formatted as C<[2012, 12, 31]>.
110              
111             =back
112              
113             An exception is thrown if the value to be coerced is not in a valid format
114             or if the date is invalid.
115              
116             =item Duration
117              
118             A class type for C<Date::Piece::Duration>. Subtypes include C<day_unit>,
119             C<week_unit>, C<month_unit> and C<year_unit>.
120             These objects are normally created using the C<days>, C<weeks>, C<months>
121             and C<years> constants and may be multiplied by an integer. They may also be
122             used for date math by adding (or subtracting) them to C<Date::Piece> objects.
123             See L<Date::Piece/Year-Month-and-etc-units> for more information.
124              
125             =over
126              
127             =item coerce from C<Str>
128              
129             The string must specify the number and unit,
130             e.g. C<'1day'>, C<'2weeks'>, C<'3 months'>, C<'4 YEARS'>.
131              
132             =item coerce from C<Int>
133              
134             The integer value will be interpreted as the number of C<days>.
135              
136             =back
137              
138             =back
139              
140             =head1 SEE ALSO
141              
142             L<Date::Piece>, L<Moose::Util::TypeConstraints>, L<MooseX::Types>
143              
144             =head1 AUTHOR
145              
146             Steven Lee, C<< <stevenl at cpan.org> >>
147              
148             =head1 LICENSE AND COPYRIGHT
149              
150             Copyright E<copy> 2012 Steven Lee. All rights reserved.
151              
152             This program is free software; you can redistribute it and/or modify it
153             under the same terms as Perl itself.
154              
155             =cut