Commit | Line | Data |
c3398f5b |
1 | #!/usr/bin/env perl |
2 | package Mouse::Attribute; |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Carp 'confess'; |
3301fa54 |
7 | use Scalar::Util 'blessed'; |
c3398f5b |
8 | |
9 | sub new { |
10 | my $class = shift; |
11 | my %args = @_; |
12 | |
7ee01d77 |
13 | $args{init_arg} = $args{name} |
14 | unless exists $args{init_arg}; |
c3398f5b |
15 | $args{is} ||= ''; |
16 | |
17 | bless \%args, $class; |
18 | } |
19 | |
186657a9 |
20 | sub name { $_[0]->{name} } |
21 | sub class { $_[0]->{class} } |
2434d21b |
22 | sub _is_metadata { $_[0]->{is} } |
23 | sub is_required { $_[0]->{required} } |
186657a9 |
24 | sub default { $_[0]->{default} } |
2434d21b |
25 | sub is_lazy { $_[0]->{lazy} } |
186657a9 |
26 | sub predicate { $_[0]->{predicate} } |
27 | sub clearer { $_[0]->{clearer} } |
28 | sub handles { $_[0]->{handles} } |
29 | sub weak_ref { $_[0]->{weak_ref} } |
30 | sub init_arg { $_[0]->{init_arg} } |
31 | sub type_constraint { $_[0]->{type_constraint} } |
de9a434a |
32 | sub trigger { $_[0]->{trigger} } |
33 | sub builder { $_[0]->{builder} } |
c3398f5b |
34 | |
ccea8101 |
35 | sub has_default { exists $_[0]->{default} } |
36 | sub has_predicate { exists $_[0]->{predicate} } |
37 | sub has_clearer { exists $_[0]->{clearer} } |
38 | sub has_handles { exists $_[0]->{handles} } |
ccea8101 |
39 | sub has_type_constraint { exists $_[0]->{type_constraint} } |
de9a434a |
40 | sub has_trigger { exists $_[0]->{trigger} } |
41 | sub has_builder { exists $_[0]->{builder} } |
ccea8101 |
42 | |
c3398f5b |
43 | sub generate_accessor { |
44 | my $attribute = shift; |
45 | |
2434d21b |
46 | my $name = $attribute->name; |
47 | my $key = $attribute->init_arg; |
48 | my $default = $attribute->default; |
49 | my $trigger = $attribute->trigger; |
50 | my $type = $attribute->type_constraint; |
5aa30ced |
51 | my $constraint = $attribute->find_type_constraint; |
9367e029 |
52 | my $builder = $attribute->builder; |
c3398f5b |
53 | |
54 | my $accessor = 'sub { |
55 | my $self = shift;'; |
56 | |
2434d21b |
57 | if ($attribute->_is_metadata eq 'rw') { |
c3398f5b |
58 | $accessor .= 'if (@_) { |
a707f587 |
59 | local $_ = $_[0];'; |
60 | |
61 | if ($constraint) { |
1f2b4780 |
62 | $accessor .= 'do { |
63 | my $display = defined($_) ? $_ : "undef"; |
64 | Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->(); |
65 | };' |
a707f587 |
66 | } |
67 | |
68 | $accessor .= '$self->{$key} = $_;'; |
c3398f5b |
69 | |
2434d21b |
70 | if ($attribute->weak_ref) { |
c3398f5b |
71 | $accessor .= 'Scalar::Util::weaken($self->{$key});'; |
72 | } |
73 | |
74 | if ($trigger) { |
a707f587 |
75 | $accessor .= '$trigger->($self, $_, $attribute);'; |
c3398f5b |
76 | } |
77 | |
78 | $accessor .= '}'; |
79 | } |
80 | else { |
81 | } |
82 | |
2434d21b |
83 | if ($attribute->is_lazy) { |
c3398f5b |
84 | $accessor .= '$self->{$key} = '; |
9367e029 |
85 | |
86 | $accessor .= $attribute->has_builder |
87 | ? '$self->$builder' |
88 | : ref($default) eq 'CODE' |
89 | ? '$default->($self)' |
90 | : '$default'; |
91 | |
c3398f5b |
92 | $accessor .= ' if !exists($self->{$key});'; |
93 | } |
94 | |
95 | $accessor .= 'return $self->{$key} |
96 | }'; |
97 | |
98 | return eval $accessor; |
99 | } |
100 | |
101 | sub generate_predicate { |
102 | my $attribute = shift; |
2434d21b |
103 | my $key = $attribute->init_arg; |
c3398f5b |
104 | |
105 | my $predicate = 'sub { exists($_[0]->{$key}) }'; |
106 | |
107 | return eval $predicate; |
108 | } |
109 | |
110 | sub generate_clearer { |
111 | my $attribute = shift; |
2434d21b |
112 | my $key = $attribute->init_arg; |
c3398f5b |
113 | |
114 | my $predicate = 'sub { delete($_[0]->{$key}) }'; |
115 | |
116 | return eval $predicate; |
117 | } |
118 | |
119 | sub generate_handles { |
120 | my $attribute = shift; |
2434d21b |
121 | my $reader = $attribute->name; |
c3398f5b |
122 | |
123 | my %method_map; |
124 | |
2434d21b |
125 | for my $local_method (keys %{ $attribute->handles }) { |
126 | my $remote_method = $attribute->handles->{$local_method}; |
c3398f5b |
127 | |
128 | my $method = 'sub { |
129 | my $self = shift; |
130 | $self->$reader->$remote_method(@_) |
131 | }'; |
132 | |
133 | $method_map{$local_method} = eval $method; |
134 | } |
135 | |
136 | return \%method_map; |
137 | } |
138 | |
139 | sub create { |
140 | my ($self, $class, $name, %args) = @_; |
141 | |
142 | confess "You must specify a default for lazy attribute '$name'" |
9367e029 |
143 | if $args{lazy} && !exists($args{default}) && !exists($args{builder}); |
c3398f5b |
144 | |
145 | confess "Trigger is not allowed on read-only attribute '$name'" |
146 | if $args{trigger} && $args{is} ne 'rw'; |
147 | |
148 | confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])" |
149 | if ref($args{default}) |
150 | && ref($args{default}) ne 'CODE'; |
151 | |
af745d5a |
152 | $args{handles} = { $self->_canonicalize_handles($args{handles}) } |
153 | if $args{handles}; |
c3398f5b |
154 | |
c021207d |
155 | $args{type_constraint} = delete $args{isa} |
156 | if exists $args{isa}; |
186657a9 |
157 | |
c3398f5b |
158 | my $attribute = $self->new(%args, name => $name, class => $class); |
159 | my $meta = $class->meta; |
160 | |
b2500191 |
161 | $meta->add_attribute($attribute); |
162 | |
c3398f5b |
163 | # install an accessor |
2434d21b |
164 | if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') { |
c3398f5b |
165 | my $accessor = $attribute->generate_accessor; |
166 | no strict 'refs'; |
167 | *{ $class . '::' . $name } = $accessor; |
168 | } |
169 | |
c3398f5b |
170 | for my $method (qw/predicate clearer/) { |
2434d21b |
171 | my $predicate = "has_$method"; |
172 | if ($attribute->$predicate) { |
c3398f5b |
173 | my $generator = "generate_$method"; |
174 | my $coderef = $attribute->$generator; |
175 | no strict 'refs'; |
2434d21b |
176 | *{ $class . '::' . $attribute->$method } = $coderef; |
c3398f5b |
177 | } |
178 | } |
179 | |
2434d21b |
180 | if ($attribute->has_handles) { |
c3398f5b |
181 | my $method_map = $attribute->generate_handles; |
182 | for my $method_name (keys %$method_map) { |
183 | no strict 'refs'; |
184 | *{ $class . '::' . $method_name } = $method_map->{$method_name}; |
185 | } |
186 | } |
187 | |
188 | return $attribute; |
189 | } |
190 | |
5aa30ced |
191 | sub find_type_constraint { |
192 | my $self = shift; |
193 | my $type = $self->type_constraint; |
194 | |
195 | return unless $type; |
196 | |
197 | my $checker = Mouse::TypeRegistry->optimized_constraints->{$type}; |
198 | return $checker if $checker; |
199 | |
3301fa54 |
200 | return sub { blessed($_) && blessed($_) eq $type }; |
5aa30ced |
201 | } |
202 | |
203 | sub verify_type_constraint { |
204 | my $self = shift; |
205 | local $_ = shift; |
206 | |
207 | my $type = $self->type_constraint |
208 | or return 1; |
af745d5a |
209 | my $constraint = $self->find_type_constraint; |
5aa30ced |
210 | |
211 | return 1 if $constraint->($_); |
212 | |
213 | my $name = $self->name; |
1f2b4780 |
214 | local $_ = "undef" unless defined($_); |
5aa30ced |
215 | Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_"); |
216 | } |
217 | |
af745d5a |
218 | sub _canonicalize_handles { |
219 | my $self = shift; |
220 | my $handles = shift; |
221 | |
222 | if (ref($handles) eq 'HASH') { |
223 | return %$handles; |
224 | } |
225 | elsif (ref($handles) eq 'ARRAY') { |
226 | return map { $_ => $_ } @$handles; |
227 | } |
228 | else { |
229 | confess "Unable to canonicalize the 'handles' option with $handles"; |
230 | } |
231 | } |
232 | |
c3398f5b |
233 | 1; |
234 | |
235 | __END__ |
236 | |
237 | =head1 NAME |
238 | |
239 | Mouse::Attribute - attribute metaclass |
240 | |
241 | =head1 METHODS |
242 | |
243 | =head2 new %args -> Mouse::Attribute |
244 | |
245 | Instantiates a new Mouse::Attribute. Does nothing else. |
246 | |
247 | =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute |
248 | |
249 | Creates a new attribute in OwnerClass. Accessors and helper methods are |
250 | installed. Some error checking is done. |
251 | |
252 | =head2 name -> AttributeName |
253 | |
254 | =head2 class -> OwnerClass |
255 | |
ab27a55e |
256 | =head2 is_required -> Bool |
c3398f5b |
257 | |
ab27a55e |
258 | =head2 default -> Item |
c3398f5b |
259 | |
ab27a55e |
260 | =head2 has_default -> Bool |
261 | |
262 | =head2 is_lazy -> Bool |
263 | |
264 | =head2 predicate -> MethodName | Undef |
265 | |
266 | =head2 has_predicate -> Bool |
267 | |
268 | =head2 clearer -> MethodName | Undef |
269 | |
270 | =head2 has_clearer -> Bool |
c3398f5b |
271 | |
272 | =head2 handles -> { LocalName => RemoteName } |
273 | |
ab27a55e |
274 | =head2 has_handles -> Bool |
275 | |
c3398f5b |
276 | =head2 weak_ref -> Bool |
277 | |
278 | =head2 init_arg -> Str |
279 | |
ab27a55e |
280 | =head2 type_constraint -> Str |
281 | |
282 | =head2 has_type_constraint -> Bool |
283 | |
284 | =head2 trigger => CODE | Undef |
285 | |
286 | =head2 has_trigger -> Bool |
287 | |
288 | =head2 builder => MethodName | Undef |
289 | |
290 | =head2 has_builder -> Bool |
291 | |
c3398f5b |
292 | Informational methods. |
293 | |
294 | =head2 generate_accessor -> CODE |
295 | |
296 | Creates a new code reference for the attribute's accessor. |
297 | |
298 | =head2 generate_predicate -> CODE |
299 | |
300 | Creates a new code reference for the attribute's predicate. |
301 | |
302 | =head2 generate_clearer -> CODE |
303 | |
304 | Creates a new code reference for the attribute's clearer. |
305 | |
306 | =head2 generate_handles -> { MethodName => CODE } |
307 | |
308 | Creates a new code reference for each of the attribute's handles methods. |
309 | |
fb706f5c |
310 | =head2 find_type_constraint -> CODE |
311 | |
312 | Returns a code reference which can be used to check that a given value passes |
313 | this attribute's type constraint; |
314 | |
315 | =head2 verify_type_constraint Item -> 1 | ERROR |
316 | |
317 | Checks that the given value passes this attribute's type constraint. Returns 1 |
318 | on success, otherwise C<confess>es. |
319 | |
c3398f5b |
320 | =cut |
321 | |