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