1 package MooseX::Types::Structured;
3 use MooseX::Types::Moose qw();
4 use MooseX::Types -declare => [qw( Dict Tuple Optional )];
7 our $AUTHORITY = 'cpan:JJNAPIORK';
11 MooseX::Types::Structured; Structured Type Constraints for Moose
15 The following is example usage for this module
22 What this application does, why I made it etc.
26 This class defines the following types and subtypes.
33 The following modules or resources may be of interest.
35 L<Moose>, L<MooseX::TypeLibrary>, L<Moose::Meta::TypeConstraint>
39 No known or reported bugs.
43 John Napiorkowski, C<< <jjnapiork@cpan.org> >>
45 =head1 COPYRIGHT & LICENSE
47 This program is free software; you can redistribute it and/or modify
48 it under the same terms as Perl itself.
61 class_type "DateTime";
62 class_type "DateTime::Duration";
63 class_type "DateTime::TimeZone";
64 class_type "DateTime::Locale::root" => { name => "DateTime::Locale" };
66 subtype DateTime, as 'DateTime';
67 subtype Duration, as 'DateTime::Duration';
68 subtype TimeZone, as 'DateTime::TimeZone';
69 subtype Locale, as 'DateTime::Locale';
73 where { $_ eq 'now' },
74 Moose::Util::TypeConstraints::optimize_as {
75 no warnings 'uninitialized';
76 !ref($_[0]) and $_[0] eq 'now';
82 from Num, via { 'DateTime'->from_epoch( epoch => $_ ) },
83 from HashRef, via { 'DateTime'->new( %$_ ) },
84 from Now, via { 'DateTime'->now },
86 "DateTime::Duration" => [
87 from Num, via { DateTime::Duration->new( seconds => $_ ) },
88 from HashRef, via { DateTime::Duration->new( %$_ ) },
90 "DateTime::TimeZone" => [
91 from Str, via { DateTime::TimeZone->new( name => $_ ) },
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($_) },
100 for my $type ( "DateTime", DateTime ) {
101 coerce $type => @{ $coercions{DateTime} };
104 for my $type ( "DateTime::Duration", Duration ) {
105 coerce $type => @{ $coercions{"DateTime::Duration"} };
108 for my $type ( "DateTime::TimeZone", TimeZone ) {
109 coerce $type => @{ $coercions{"DateTime::TimeZone"} };
112 for my $type ( "DateTime::Locale", Locale ) {
113 coerce $type => @{ $coercions{"DateTime::Locale"} };
124 MooseX::Types::DateTime - L<DateTime> related constraints and coercions for
131 use MooseX::Types::DateTime qw(TimeZone);
139 Class->new( time_zone => "Africa/Timbuktu" );
143 use MooseX::Types::DateTime;
146 isa => 'DateTime::TimeZone',
151 Class->new( time_zone => "Africa/Timbuktu" );
155 This module packages several L<Moose::Util::TypeConstraints> with coercions,
156 designed to work with the L<DateTime> suite of objects.
164 A class type for L<DateTime>.
170 Uses L<DateTime/from_epoch>. Floating values will be used for subsecond
171 percision, see L<DateTime> for details.
173 =item from C<HashRef>
175 Calls L<DateTime/new> with the hash entries as arguments.
181 A class type for L<DateTime::Duration>
187 Uses L<DateTime::Duration/new> and passes the number as the C<seconds> argument.
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">
194 =item from C<HashRef>
196 Calls L<DateTime::Duration/new> with the hash entries as arguments.
200 =item L<DateTime::Locale>
202 A class type for L<DateTime::Locale::root> with the name L<DateTime::Locale>.
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>.
211 =item from L<Locale::Maktext>
213 The C<Locale::Maketext/language_tag> attribute will be used with L<DateTime::Locale/load>.
215 =item L<DateTime::TimeZone>
217 A class type for L<DateTime::TimeZone>.
223 Treated as a time zone name or offset. See L<DateTime::TimeZone/USAGE> for more
224 details on the allowed values.
226 Delegates to L<DateTime::TimeZone/new> with the string as the C<name> argument.
232 L<MooseX::Types::DateTimeX>
234 L<DateTime>, L<DateTimeX::Easy>
236 =head1 VERSION CONTROL
238 L<http://code2.0beta.co.uk/moose/svn/MooseX-Types-DateTime/trunk>. Ask on
239 #moose for commit bits.
243 Yuval Kogman E<lt>nothingmuch@woobling.orgE<gt>
245 John Napiorkowski E<lt>jjn1056 at yahoo.comE<gt>
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.