Commit | Line | Data |
d24da8ec |
1 | package MooseX::Types::Structured; |
2 | |
3 | use MooseX::Types::Moose qw(); |
4 | use MooseX::Types -declare => [qw( Dict Tuple Optional )]; |
5 | |
6 | our $VERSION = '0.01'; |
7 | our $AUTHORITY = 'cpan:JJNAPIORK'; |
8 | |
9 | =head1 NAME |
10 | |
11 | MooseX::Types::Structured; Structured Type Constraints for Moose |
12 | |
13 | =head1 SYNOPSIS |
14 | |
15 | The following is example usage for this module |
16 | |
17 | package MyApp::Types |
18 | TBD |
19 | |
20 | =head1 DESCRIPTION |
21 | |
22 | What this application does, why I made it etc. |
23 | |
24 | =head1 TYPES |
25 | |
26 | This class defines the following types and subtypes. |
27 | |
28 | =cut |
29 | |
30 | |
31 | =head1 SEE ALSO |
32 | |
33 | The following modules or resources may be of interest. |
34 | |
35 | L<Moose>, L<MooseX::TypeLibrary>, L<Moose::Meta::TypeConstraint> |
36 | |
37 | =head1 BUGS |
38 | |
39 | No known or reported bugs. |
40 | |
41 | =head1 AUTHOR |
42 | |
43 | John Napiorkowski, C<< <jjnapiork@cpan.org> >> |
44 | |
45 | =head1 COPYRIGHT & LICENSE |
46 | |
47 | This program is free software; you can redistribute it and/or modify |
48 | it under the same terms as Perl itself. |
49 | |
50 | =cut |
51 | |
52 | 1; |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | class_type "DateTime"; |
62 | class_type "DateTime::Duration"; |
63 | class_type "DateTime::TimeZone"; |
64 | class_type "DateTime::Locale::root" => { name => "DateTime::Locale" }; |
65 | |
66 | subtype DateTime, as 'DateTime'; |
67 | subtype Duration, as 'DateTime::Duration'; |
68 | subtype TimeZone, as 'DateTime::TimeZone'; |
69 | subtype Locale, as 'DateTime::Locale'; |
70 | |
71 | subtype( Now, |
72 | as Str, |
73 | where { $_ eq 'now' }, |
74 | Moose::Util::TypeConstraints::optimize_as { |
75 | no warnings 'uninitialized'; |
76 | !ref($_[0]) and $_[0] eq 'now'; |
77 | }, |
78 | ); |
79 | |
80 | our %coercions = ( |
81 | DateTime => [ |
82 | from Num, via { 'DateTime'->from_epoch( epoch => $_ ) }, |
83 | from HashRef, via { 'DateTime'->new( %$_ ) }, |
84 | from Now, via { 'DateTime'->now }, |
85 | ], |
86 | "DateTime::Duration" => [ |
87 | from Num, via { DateTime::Duration->new( seconds => $_ ) }, |
88 | from HashRef, via { DateTime::Duration->new( %$_ ) }, |
89 | ], |
90 | "DateTime::TimeZone" => [ |
91 | from Str, via { DateTime::TimeZone->new( name => $_ ) }, |
92 | ], |
93 | "DateTime::Locale" => [ |
94 | from Moose::Util::TypeConstraints::find_or_create_isa_type_constraint("Locale::Maketext"), |
95 | via { DateTime::Locale->load($_->language_tag) }, |
96 | from Str, via { DateTime::Locale->load($_) }, |
97 | ], |
98 | ); |
99 | |
100 | for my $type ( "DateTime", DateTime ) { |
101 | coerce $type => @{ $coercions{DateTime} }; |
102 | } |
103 | |
104 | for my $type ( "DateTime::Duration", Duration ) { |
105 | coerce $type => @{ $coercions{"DateTime::Duration"} }; |
106 | } |
107 | |
108 | for my $type ( "DateTime::TimeZone", TimeZone ) { |
109 | coerce $type => @{ $coercions{"DateTime::TimeZone"} }; |
110 | } |
111 | |
112 | for my $type ( "DateTime::Locale", Locale ) { |
113 | coerce $type => @{ $coercions{"DateTime::Locale"} }; |
114 | } |
115 | |
116 | __PACKAGE__ |
117 | |
118 | __END__ |
119 | |
120 | =pod |
121 | |
122 | =head1 NAME |
123 | |
124 | MooseX::Types::DateTime - L<DateTime> related constraints and coercions for |
125 | Moose |
126 | |
127 | =head1 SYNOPSIS |
128 | |
129 | Export Example: |
130 | |
131 | use MooseX::Types::DateTime qw(TimeZone); |
132 | |
133 | has time_zone => ( |
134 | isa => TimeZone, |
135 | is => "rw", |
136 | coerce => 1, |
137 | ); |
138 | |
139 | Class->new( time_zone => "Africa/Timbuktu" ); |
140 | |
141 | Namespaced Example: |
142 | |
143 | use MooseX::Types::DateTime; |
144 | |
145 | has time_zone => ( |
146 | isa => 'DateTime::TimeZone', |
147 | is => "rw", |
148 | coerce => 1, |
149 | ); |
150 | |
151 | Class->new( time_zone => "Africa/Timbuktu" ); |
152 | |
153 | =head1 DESCRIPTION |
154 | |
155 | This module packages several L<Moose::Util::TypeConstraints> with coercions, |
156 | designed to work with the L<DateTime> suite of objects. |
157 | |
158 | =head1 CONSTRAINTS |
159 | |
160 | =over 4 |
161 | |
162 | =item L<DateTime> |
163 | |
164 | A class type for L<DateTime>. |
165 | |
166 | =over 4 |
167 | |
168 | =item from C<Num> |
169 | |
170 | Uses L<DateTime/from_epoch>. Floating values will be used for subsecond |
171 | percision, see L<DateTime> for details. |
172 | |
173 | =item from C<HashRef> |
174 | |
175 | Calls L<DateTime/new> with the hash entries as arguments. |
176 | |
177 | =back |
178 | |
179 | =item L<Duration> |
180 | |
181 | A class type for L<DateTime::Duration> |
182 | |
183 | =over 4 |
184 | |
185 | =item from C<Num> |
186 | |
187 | Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument. |
188 | |
189 | Note that due to leap seconds, DST changes etc this may not do what you expect. |
190 | For instance passing in C<86400> is not always equivalent to one day, although |
191 | there are that many seconds in a day. See L<DateTime/"How Date Math is Done"> |
192 | for more details. |
193 | |
194 | =item from C<HashRef> |
195 | |
196 | Calls L<DateTime::Duration/new> with the hash entries as arguments. |
197 | |
198 | =back |
199 | |
200 | =item L<DateTime::Locale> |
201 | |
202 | A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>. |
203 | |
204 | =over 4 |
205 | |
206 | =item from C<Str> |
207 | |
208 | The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to |
209 | L<DateTime::Locale/load>. |
210 | |
211 | =item from L<Locale::Maktext> |
212 | |
213 | The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>. |
214 | |
215 | =item L<DateTime::TimeZone> |
216 | |
217 | A class type for L<DateTime::TimeZone>. |
218 | |
219 | =over 4 |
220 | |
221 | =item from C<Str> |
222 | |
223 | Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more |
224 | details on the allowed values. |
225 | |
226 | Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument. |
227 | |
228 | =back |
229 | |
230 | =head1 SEE ALSO |
231 | |
232 | L<MooseX::Types::DateTimeX> |
233 | |
234 | L<DateTime>, L<DateTimeX::Easy> |
235 | |
236 | =head1 VERSION CONTROL |
237 | |
238 | L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on |
239 | #moose for commit bits. |
240 | |
241 | =head1 AUTHOR |
242 | |
243 | Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt> |
244 | |
245 | John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt> |
246 | |
247 | =head1 COPYRIGHT |
248 | |
249 | Copyright (c) 2008 Yuval Kogman. All rights reserved |
250 | This program is free software; you can redistribute |
251 | it and/or modify it under the same terms as Perl itself. |
252 | |
253 | =cut |