Commit | Line | Data |
fb583a23 |
1 | |
2 | ## MyApp/Types.pm; |
3 | |
4 | use MooseX::Declare::Types; |
5 | use MooseX::Types::Moose qw(Str Int); |
6 | use MooseX::Types::Structured qw(Dict Optional); |
7 | |
8 | ## Possible interface for facets, with are a bit like roles for types. This |
9 | ## just a sketch, doesn't represent a lot of thought. |
10 | use MooseX::Types::Facets { |
11 | Str => [qw/ hasMinimumLength hasMaximumLength isWords matchesRegex/ ], |
12 | }; |
13 | |
14 | subtype FullName { |
15 | as Str; |
16 | having hasMinimumLength(2), |
17 | matchesPattern( |
18 | strategy=>Regexp, |
19 | pattern=>qr/ |
20 | ^ |
21 | \w{2,35} |
22 | \s |
23 | \w{0,25} |
24 | \s* |
25 | \w{2,50} |
26 | $ |
27 | /x |
28 | ); |
29 | |
30 | parameter length => ( |
31 | isa=>Int, |
32 | default=>100 |
33 | ); |
34 | |
35 | parameterizes($p Parameters) { |
36 | having hasMaximumLength($p->length); |
37 | } |
38 | } |
39 | |
40 | subtype Name { |
41 | |
42 | parameter total_length => ( |
43 | isa=>Int, |
44 | default=>60, |
45 | ); |
46 | |
47 | parameter part_max_lengths => ( |
48 | isa=>Dict[ |
49 | first=>Optional[Int], |
50 | middle=>Optional[Int], |
51 | last=>Optional[Int], |
52 | ], |
53 | ); |
54 | |
55 | my $Namepart = subtype { |
56 | as Str; |
57 | having isWords(maximum_words=>1), |
58 | hasMinimumLength(2); |
59 | |
60 | parameter max_length => ( |
61 | isa=>int, |
62 | default=>20 |
63 | ); |
64 | |
65 | parameterize($p Parameters) { |
66 | having hasMaximumLength($p->max_length); |
67 | } |
68 | } |
69 | |
70 | parameterize($p Parameters) { |
71 | |
72 | as Dict[ |
73 | first => $NamePart( |
74 | max_length=>$p->part_max_lengths->{first}, |
75 | ), |
76 | middle => Optional[ |
77 | $NamePart( |
78 | max_length=>$p->part_max_lengths->{middle} |
79 | ), |
80 | ], |
81 | last => $NamePart( |
82 | max_length=>$p->part_max_lengths->{last}, |
83 | ), |
84 | ]; |
85 | |
86 | having hasMaximumLength($p->total_length); |
87 | } |
88 | } |
89 | |
90 | |
91 | ## MyApp/MyClass/Foo.pm |
92 | |
93 | use MooseX::Declare; |
94 | use MooseX::Types qw(EmailAddress); |
95 | use MyApp::Types qw(FullName Name); |
96 | |
97 | Class Foo { |
98 | |
99 | with Class::DBIx::Role::Result => ( |
100 | resultset_class => '', |
101 | fields => { |
102 | fullname => ..., |
103 | name => ..., |
104 | }, |
105 | ); |
106 | |
107 | ## fullname would have a minimum of 2 characters, a maximum of 45 and |
108 | ## would be composed of a maximum of 3 words separated by whitespace |
109 | |
110 | has fullname => ( |
111 | is=>'rw', |
112 | isa=>FullName(length=>45), |
113 | ); |
114 | |
115 | ## name would be a HashRef with two required keys 'first' and 'last' |
116 | ## and a single optional key called 'middle'. each key value must be a |
117 | ## str composed of a single word with a minimum character length of 2 |
118 | ## and maximum lengths of first=>35, middle=>25 and last=>50. Also the |
119 | ## overall total length. |
120 | |
121 | has name => ( |
122 | is=>'rw', |
123 | isa=>Name( |
124 | total_length=>95, |
125 | part_max_lengths=>{ |
126 | first=>35, |
127 | middle=>25, |
128 | last=>50, |
129 | }, |
130 | ), |
131 | ); |
132 | |
133 | method email($recipient Email) { |
134 | .... |
135 | } |
136 | } |
137 | |
138 | |
139 | use aliased MyApp::MyClass::Foo; |
140 | |
141 | my $foo = Foo->new( |
142 | fullname=>'Vanessa Li', |
143 | name=>{ |
144 | first=>'John', |
145 | middle=>'James', |
146 | last=>'Napiorkowski', |
147 | }, |
148 | ); |
149 | |
150 | my $foo_set = Foo->search( name => sub { $self->search ... }); |
151 | |
152 | |
153 | ## COERCSIONS |
154 | |
155 | |
156 | coercion fixPerson($person Person) { |
157 | from Tuple($name Str, $age Int) { |
158 | $person->XXXXX(name=>$name, age=>$age); |
159 | } |
160 | from Dict( |
161 | :$fullname Fullname, |
162 | :$dob DateTime |
163 | ) { |
164 | my $name = "$fullname->{first} $fullname->{last}"; |
165 | my $age = ($dob - DateTime-now)->years; |
166 | return $person->XXX(name=>$name, age=>$age); |
167 | } |
168 | } |
169 | |
170 | |
171 | has 'attribute' => ( |
172 | is=>'rw', |
173 | isa=> Person->coercions(fixPerson, MorePeople), |
174 | ); |
175 | |
176 | |
177 | |
178 | subtype myDelegate, |
179 | as Delegate[Signature]; |
180 | |
181 | |
182 | type Signature, |
183 | as Optional[ |
184 | Int, |
185 | Pairs[ |
186 | Pair[name=>Int], |
187 | Pair[key=>Str], |
188 | ], |
189 | |
190 | |
191 | |
192 | |