Commit | Line | Data |
d24da8ec |
1 | package MooseX::Types::Structured; |
2 | |
6c2f284c |
3 | use Moose; |
4 | use Moose::Util::TypeConstraints; |
a30fa891 |
5 | use MooseX::Meta::TypeConstraint::Structured; |
6 | use MooseX::Types -declare => [qw(Dict Tuple)]; |
011bacc6 |
7 | |
6c2f284c |
8 | |
d24da8ec |
9 | our $VERSION = '0.01'; |
10 | our $AUTHORITY = 'cpan:JJNAPIORK'; |
11 | |
12 | =head1 NAME |
13 | |
14 | MooseX::Types::Structured; Structured Type Constraints for Moose |
15 | |
16 | =head1 SYNOPSIS |
17 | |
6c2f284c |
18 | The following is example usage for this module. You can define a class that has |
19 | an attribute with a structured type like so: |
20 | |
21 | package MyApp::MyClass; |
22 | |
23 | use Moose; |
24 | use MooseX::Types::Moose qw(Str Int); |
25 | use MooseX::Types::Structured qw(Dict Tuple); |
26 | |
27 | has name => (isa=>Dict[first_name=>Str, last_name=>Str]); |
28 | |
29 | Then you can instantiate this class with something like: |
30 | |
31 | my $instance = MyApp::MyClass->new( |
32 | name=>{first_name=>'John', last_name=>'Napiorkowski'}, |
33 | ); |
d24da8ec |
34 | |
6c2f284c |
35 | But all of these would cause an error: |
36 | |
37 | my $instance = MyApp::MyClass->new(name=>'John'); |
38 | my $instance = MyApp::MyClass->new(name=>{first_name=>'John'}); |
39 | my $instance = MyApp::MyClass->new(name=>{first_name=>'John', age=>39}); |
40 | |
41 | Please see the test cases for more examples. |
d24da8ec |
42 | |
43 | =head1 DESCRIPTION |
44 | |
6c2f284c |
45 | This type library enables structured type constraints. Basically, this is very |
46 | similar to parameterized constraints that are built into the core Moose types, |
47 | except that you are allowed to define the container's entire structure. For |
48 | example, you could define a parameterized constraint like so: |
49 | |
50 | subtype HashOfInts, as Hashref[Int]; |
51 | |
52 | which would constraint a value to something like [1,2,3,...] and so one. A |
53 | structured constraint like so: |
54 | |
55 | subtype StringFollowedByInt, as Tuple[Str,Int]; |
56 | |
57 | would constrain it's value to something like ['hello', 111]; |
58 | |
59 | These structures can be as simple or elaborate as you wish. You can even |
60 | combine various structured, parameterized and simple constraints all together: |
61 | |
62 | subtype crazy, as Tuple[Int, Dict[name=>Str, age=>Int], ArrayRef[Int]]; |
63 | |
64 | Which would match "[1, {name=>'John', age=>25},[10,11,12]]". |
65 | |
66 | You should exercise some care as to whether or not your complex structured |
67 | constraints would be better off contained by a real object as in the following |
68 | example: |
69 | |
70 | { |
71 | package MyApp::MyStruct; |
72 | use Moose; |
73 | |
74 | has $_ for qw(name age); |
75 | |
76 | package MyApp::MyClass; |
77 | use Moose; |
78 | |
79 | has person => (isa=>'MyApp::MyStruct'); |
80 | } |
81 | |
82 | my $instance = MyApp::MyClass |
83 | ->new( person=>MyApp::MyStruct->new(name=>'John', age=>39) ); |
84 | |
85 | This method may take some additional time to setup but will give you more |
86 | flexibility. However, structured constraints are highly compatible with this |
87 | method, granting some interesting possibilities for coercion. Try: |
88 | |
89 | subtype 'MyStruct', |
90 | as 'MyApp::MyStruct'; |
91 | |
92 | coerce 'MyStruct', |
93 | from (Dict[name=>Str, age=>Int]), |
94 | via { |
95 | MyApp::MyStruct->new(%$_); |
96 | }, |
97 | from (Dict[last_name=>Str, first_name=>Str, dob=>DateTime]), |
98 | via { |
99 | my $name = _->{first_name} .' '. $_->{last_name}; |
100 | my $age = $_->{dob} - DateTime->now; |
101 | MyApp::MyStruct->new( |
102 | name=>$name, |
103 | age=>$age->years ); |
104 | }; |
105 | |
d24da8ec |
106 | |
a30fa891 |
107 | =head1 METHODS |
d24da8ec |
108 | |
a30fa891 |
109 | This class defines the following methods |
d24da8ec |
110 | |
a30fa891 |
111 | =cut |
112 | |
67a8bc04 |
113 | Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( |
114 | MooseX::Meta::TypeConstraint::Structured->new( |
115 | name => "MooseX::Types::Structured::Tuple" , |
116 | parent => find_type_constraint('ArrayRef'), |
117 | constraint_generator=> sub { |
118 | ## Get the constraints and values to check |
119 | my @type_constraints = @{shift @_}; |
120 | my @values = @{shift @_}; |
121 | ## Perform the checking |
122 | while(@type_constraints) { |
123 | my $type_constraint = shift @type_constraints; |
a30fa891 |
124 | if(@values) { |
67a8bc04 |
125 | my $value = shift @values; |
126 | unless($type_constraint->check($value)) { |
127 | return; |
128 | } |
129 | } else { |
a30fa891 |
130 | return; |
a30fa891 |
131 | } |
132 | } |
67a8bc04 |
133 | ## Make sure there are no leftovers. |
134 | if(@values) { |
135 | return; |
136 | } elsif(@type_constraints) { |
137 | return; |
138 | }else { |
139 | return 1; |
140 | } |
141 | } |
142 | ) |
143 | ); |
144 | |
145 | Moose::Util::TypeConstraints::get_type_constraint_registry->add_type_constraint( |
146 | MooseX::Meta::TypeConstraint::Structured->new( |
147 | name => "MooseX::Types::Structured::Dict", |
148 | parent => find_type_constraint('HashRef'), |
149 | constraint_generator=> sub { |
150 | ## Get the constraints and values to check |
151 | my %type_constraints = @{shift @_}; |
152 | my %values = %{shift @_}; |
153 | ## Perform the checking |
154 | while(%type_constraints) { |
155 | my($key, $type_constraint) = each %type_constraints; |
156 | delete $type_constraints{$key}; |
157 | if(exists $values{$key}) { |
158 | my $value = $values{$key}; |
159 | delete $values{$key}; |
160 | unless($type_constraint->check($value)) { |
a30fa891 |
161 | return; |
162 | } |
67a8bc04 |
163 | } else { |
a30fa891 |
164 | return; |
a30fa891 |
165 | } |
67a8bc04 |
166 | } |
167 | ## Make sure there are no leftovers. |
168 | if(%values) { |
169 | return; |
170 | } elsif(%type_constraints) { |
171 | return; |
172 | }else { |
173 | return 1; |
174 | } |
175 | }, |
176 | ) |
177 | ); |
d24da8ec |
178 | |
179 | =head1 SEE ALSO |
180 | |
181 | The following modules or resources may be of interest. |
182 | |
a30fa891 |
183 | L<Moose>, L<MooseX::TypeLibrary>, L<Moose::Meta::TypeConstraint>, |
184 | L<MooseX::Meta::TypeConstraint::Structured> |
d24da8ec |
185 | |
186 | =head1 AUTHOR |
187 | |
188 | John Napiorkowski, C<< <jjnapiork@cpan.org> >> |
189 | |
190 | =head1 COPYRIGHT & LICENSE |
191 | |
192 | This program is free software; you can redistribute it and/or modify |
193 | it under the same terms as Perl itself. |
194 | |
195 | =cut |
67a8bc04 |
196 | |
197 | 1; |