a start on making the structured constraints real
[gitmo/MooseX-Types-Structured.git] / lib / MooseX / Types / Structured.pm
CommitLineData
d24da8ec 1package MooseX::Types::Structured;
2
3use MooseX::Types::Moose qw();
4use MooseX::Types -declare => [qw( Dict Tuple Optional )];
5
6our $VERSION = '0.01';
7our $AUTHORITY = 'cpan:JJNAPIORK';
8
9=head1 NAME
10
11MooseX::Types::Structured; Structured Type Constraints for Moose
12
13=head1 SYNOPSIS
14
15The following is example usage for this module
16
17 package MyApp::Types
18 TBD
19
20=head1 DESCRIPTION
21
22What this application does, why I made it etc.
23
24=head1 TYPES
25
26This class defines the following types and subtypes.
27
28=cut
29
30
31=head1 SEE ALSO
32
33The following modules or resources may be of interest.
34
35L<Moose>, L<MooseX::TypeLibrary>, L<Moose::Meta::TypeConstraint>
36
37=head1 BUGS
38
39No known or reported bugs.
40
41=head1 AUTHOR
42
43John Napiorkowski, C<< <jjnapiork@cpan.org> >>
44
45=head1 COPYRIGHT & LICENSE
46
47This program is free software; you can redistribute it and/or modify
48it under the same terms as Perl itself.
49
50=cut
51
521;
53
54
55
56
57
58
59
60
61class_type "DateTime";
62class_type "DateTime::Duration";
63class_type "DateTime::TimeZone";
64class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
65
66subtype DateTime, as 'DateTime';
67subtype Duration, as 'DateTime::Duration';
68subtype TimeZone, as 'DateTime::TimeZone';
69subtype Locale, as 'DateTime::Locale';
70
71subtype( 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
80our %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
100for my $type ( "DateTime", DateTime ) {
101 coerce $type => @{ $coercions{DateTime} };
102}
103
104for my $type ( "DateTime::Duration", Duration ) {
105 coerce $type => @{ $coercions{"DateTime::Duration"} };
106}
107
108for my $type ( "DateTime::TimeZone", TimeZone ) {
109 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
110}
111
112for 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
124MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
125Moose
126
127=head1 SYNOPSIS
128
129Export 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
141Namespaced 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
155This module packages several L<Moose::Util::TypeConstraints> with coercions,
156designed to work with the L<DateTime> suite of objects.
157
158=head1 CONSTRAINTS
159
160=over 4
161
162=item L<DateTime>
163
164A class type for L<DateTime>.
165
166=over 4
167
168=item from C<Num>
169
170Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
171percision, see L<DateTime> for details.
172
173=item from C<HashRef>
174
175Calls L<DateTime/new> with the hash entries as arguments.
176
177=back
178
179=item L<Duration>
180
181A class type for L<DateTime::Duration>
182
183=over 4
184
185=item from C<Num>
186
187Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
188
189Note that due to leap seconds, DST changes etc this may not do what you expect.
190For instance passing in C<86400> is not always equivalent to one day, although
191there are that many seconds in a day. See L<DateTime/"How Date Math is Done">
192for more details.
193
194=item from C<HashRef>
195
196Calls L<DateTime::Duration/new> with the hash entries as arguments.
197
198=back
199
200=item L<DateTime::Locale>
201
202A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
203
204=over 4
205
206=item from C<Str>
207
208The string is treated as a language tag (e.g. C<en> or C<he_IL>) and given to
209L<DateTime::Locale/load>.
210
211=item from L<Locale::Maktext>
212
213The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
214
215=item L<DateTime::TimeZone>
216
217A class type for L<DateTime::TimeZone>.
218
219=over 4
220
221=item from C<Str>
222
223Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
224details on the allowed values.
225
226Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
227
228=back
229
230=head1 SEE ALSO
231
232L<MooseX::Types::DateTimeX>
233
234L<DateTime>, L<DateTimeX::Easy>
235
236=head1 VERSION CONTROL
237
238L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
239#moose for commit bits.
240
241=head1 AUTHOR
242
243Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
244
245John 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