Provide optimize_as for pre-2.0100 Moose versions
[gitmo/MooseX-Types-DateTime.git] / lib / MooseX / Types / DateTime.pm
1 #!/usr/bin/perl
2
3 package MooseX::Types::DateTime;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = "0.06";
9
10 use DateTime ();
11 use DateTime::Locale ();
12 use DateTime::TimeZone ();
13
14 use MooseX::Types::Moose qw/Num HashRef Str/;
15
16 use namespace::clean;
17
18 use MooseX::Types -declare => [qw( DateTime Duration TimeZone Locale Now )];
19
20 class_type "DateTime";
21 class_type "DateTime::Duration";
22 class_type "DateTime::TimeZone";
23 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
24
25 subtype DateTime, as 'DateTime';
26 subtype Duration, as 'DateTime::Duration';
27 subtype TimeZone, as 'DateTime::TimeZone';
28 subtype Locale,   as 'DateTime::Locale';
29
30 subtype( Now,
31     as Str,
32     where { $_ eq 'now' },
33     ($Moose::VERSION >= 2.0100
34         ? Moose::Util::TypeConstraints::inline_as {
35            'no warnings "uninitialized";'.
36            '!ref(' . $_[1] . ') and '. $_[1] .' eq "now"';
37         }
38         : Moose::Util::TypeConstraints::optimize_as {
39             no warnings 'uninitialized';
40             !ref($_[0]) and $_[0] eq 'now';
41         }
42     ),
43 );
44
45 our %coercions = (
46     DateTime => [
47                 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
48                 from HashRef, via { 'DateTime'->new( %$_ ) },
49                 from Now, via { 'DateTime'->now },
50     ],
51     "DateTime::Duration" => [
52                 from Num, via { DateTime::Duration->new( seconds => $_ ) },
53                 from HashRef, via { DateTime::Duration->new( %$_ ) },
54     ],
55     "DateTime::TimeZone" => [
56                 from Str, via { DateTime::TimeZone->new( name => $_ ) },
57     ],
58     "DateTime::Locale" => [
59         from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"),
60         via { DateTime::Locale->load($_->language_tag) },
61         from Str, via { DateTime::Locale->load($_) },
62     ],
63 );
64
65 for my $type ( "DateTime", DateTime ) {
66     coerce $type => @{ $coercions{DateTime} };
67 }
68
69 for my $type ( "DateTime::Duration", Duration ) {
70     coerce $type => @{ $coercions{"DateTime::Duration"} };
71 }
72
73 for my $type ( "DateTime::TimeZone", TimeZone ) {
74         coerce $type => @{ $coercions{"DateTime::TimeZone"} };
75 }
76
77 for my $type ( "DateTime::Locale", Locale ) {
78         coerce $type => @{ $coercions{"DateTime::Locale"} };
79 }
80
81 __PACKAGE__
82
83 __END__
84
85 =pod
86
87 =head1 NAME
88
89 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
90 Moose
91
92 =head1 SYNOPSIS
93
94 Export Example:
95
96         use MooseX::Types::DateTime qw(TimeZone);
97
98     has time_zone => (
99         isa => TimeZone,
100         is => "rw",
101         coerce => 1,
102     );
103
104     Class->new( time_zone => "Africa/Timbuktu" );
105
106 Namespaced Example:
107
108         use MooseX::Types::DateTime;
109
110     has time_zone => (
111         isa => 'DateTime::TimeZone',
112         is => "rw",
113         coerce => 1,
114     );
115
116     Class->new( time_zone => "Africa/Timbuktu" );
117
118 =head1 DESCRIPTION
119
120 This module packages several L<Moose::Util::TypeConstraints> with coercions,
121 designed to work with the L<DateTime> suite of objects.
122
123 =head1 CONSTRAINTS
124
125 =over 4
126
127 =item L<DateTime>
128
129 A class type for L<DateTime>.
130
131 =over 4
132
133 =item from C<Num>
134
135 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
136 percision, see L<DateTime> for details.
137
138 =item from C<HashRef>
139
140 Calls L<DateTime/new> with the hash entries as arguments.
141
142 =back
143
144 =item L<Duration>
145
146 A class type for L<DateTime::Duration>
147
148 =over 4
149
150 =item from C<Num>
151
152 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
153
154 Note that due to leap seconds, DST changes etc this may not do what you expect.
155 For instance passing in C<86400> is not always equivalent to one day, although
156 there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
157 for more details.
158
159 =item from C<HashRef>
160
161 Calls L<DateTime::Duration/new> with the hash entries as arguments.
162
163 =back
164
165 =item L<DateTime::Locale>
166
167 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
168
169 =over 4
170
171 =item from C<Str>
172
173 The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
174 L<DateTime::Locale/load>.
175
176 =item from L<Locale::Maktext>
177
178 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
179
180 =item L<DateTime::TimeZone>
181
182 A class type for L<DateTime::TimeZone>.
183
184 =over 4
185
186 =item from C<Str>
187
188 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
189 details on the allowed values.
190
191 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
192
193 =back
194
195 =back
196
197 =back
198
199 =head1 SEE ALSO
200
201 L<MooseX::Types::DateTimeX>
202
203 L<DateTime>, L<DateTimeX::Easy>
204
205 =head1 VERSION CONTROL
206
207 This module is maintained using git. You can get the latest version from
208 L<git://github.com/nothingmuch/moosex-types-datetime.git>.
209
210 =head1 AUTHOR
211
212 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
213
214 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
215
216 =head1 COPYRIGHT
217
218         Copyright (c) 2008 Yuval Kogman. All rights reserved
219         This program is free software; you can redistribute
220         it and/or modify it under the same terms as Perl itself.
221
222 =cut