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