Commit | Line | Data |
c3398f5b |
1 | #!/usr/bin/env perl |
2 | package Mouse::Attribute; |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Carp 'confess'; |
7 | |
8 | sub new { |
9 | my $class = shift; |
10 | my %args = @_; |
11 | |
7ee01d77 |
12 | $args{init_arg} = $args{name} |
13 | unless exists $args{init_arg}; |
c3398f5b |
14 | $args{is} ||= ''; |
15 | |
16 | bless \%args, $class; |
17 | } |
18 | |
186657a9 |
19 | sub name { $_[0]->{name} } |
20 | sub class { $_[0]->{class} } |
de9a434a |
21 | sub required { $_[0]->{required} } |
186657a9 |
22 | sub default { $_[0]->{default} } |
de9a434a |
23 | sub lazy { $_[0]->{lazy} } |
186657a9 |
24 | sub predicate { $_[0]->{predicate} } |
25 | sub clearer { $_[0]->{clearer} } |
26 | sub handles { $_[0]->{handles} } |
27 | sub weak_ref { $_[0]->{weak_ref} } |
28 | sub init_arg { $_[0]->{init_arg} } |
29 | sub type_constraint { $_[0]->{type_constraint} } |
de9a434a |
30 | sub trigger { $_[0]->{trigger} } |
31 | sub builder { $_[0]->{builder} } |
c3398f5b |
32 | |
ccea8101 |
33 | sub has_default { exists $_[0]->{default} } |
34 | sub has_predicate { exists $_[0]->{predicate} } |
35 | sub has_clearer { exists $_[0]->{clearer} } |
36 | sub has_handles { exists $_[0]->{handles} } |
37 | sub has_weak_ref { exists $_[0]->{weak_ref} } |
38 | sub has_init_arg { exists $_[0]->{init_arg} } |
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 | |
a707f587 |
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 | |
56 | if ($attribute->{is} eq 'rw') { |
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 | |
69 | if ($attribute->{weak_ref}) { |
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 | |
82 | if ($attribute->{lazy}) { |
83 | $accessor .= '$self->{$key} = '; |
84 | $accessor .= ref($attribute->{default}) eq 'CODE' |
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; |
98 | my $key = $attribute->{init_arg}; |
99 | |
100 | my $predicate = 'sub { exists($_[0]->{$key}) }'; |
101 | |
102 | return eval $predicate; |
103 | } |
104 | |
105 | sub generate_clearer { |
106 | my $attribute = shift; |
107 | my $key = $attribute->{init_arg}; |
108 | |
109 | my $predicate = 'sub { delete($_[0]->{$key}) }'; |
110 | |
111 | return eval $predicate; |
112 | } |
113 | |
114 | sub generate_handles { |
115 | my $attribute = shift; |
116 | my $reader = $attribute->{name}; |
117 | |
118 | my %method_map; |
119 | |
120 | for my $local_method (keys %{ $attribute->{handles} }) { |
121 | my $remote_method = $attribute->{handles}{$local_method}; |
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 | |
147 | $args{handles} = { map { $_ => $_ } @{ $args{handles} } } |
148 | if $args{handles} |
149 | && ref($args{handles}) eq 'ARRAY'; |
150 | |
151 | confess "You must pass a HASH or ARRAY to handles" |
152 | if exists($args{handles}) |
153 | && ref($args{handles}) ne 'HASH'; |
154 | |
186657a9 |
155 | $args{type_constraint} = delete $args{isa}; |
156 | |
c3398f5b |
157 | my $attribute = $self->new(%args, name => $name, class => $class); |
158 | my $meta = $class->meta; |
159 | |
b2500191 |
160 | $meta->add_attribute($attribute); |
161 | |
c3398f5b |
162 | # install an accessor |
163 | if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') { |
164 | my $accessor = $attribute->generate_accessor; |
165 | no strict 'refs'; |
166 | *{ $class . '::' . $name } = $accessor; |
167 | } |
168 | |
c3398f5b |
169 | for my $method (qw/predicate clearer/) { |
170 | if (exists $attribute->{$method}) { |
171 | my $generator = "generate_$method"; |
172 | my $coderef = $attribute->$generator; |
173 | no strict 'refs'; |
174 | *{ $class . '::' . $attribute->{$method} } = $coderef; |
175 | } |
176 | } |
177 | |
178 | if ($attribute->{handles}) { |
179 | my $method_map = $attribute->generate_handles; |
180 | for my $method_name (keys %$method_map) { |
181 | no strict 'refs'; |
182 | *{ $class . '::' . $method_name } = $method_map->{$method_name}; |
183 | } |
184 | } |
185 | |
186 | return $attribute; |
187 | } |
188 | |
5aa30ced |
189 | sub find_type_constraint { |
190 | my $self = shift; |
191 | my $type = $self->type_constraint; |
192 | |
193 | return unless $type; |
194 | |
195 | my $checker = Mouse::TypeRegistry->optimized_constraints->{$type}; |
196 | return $checker if $checker; |
197 | |
198 | confess "Unable to parse type constraint '$type'"; |
199 | } |
200 | |
201 | sub verify_type_constraint { |
202 | my $self = shift; |
203 | local $_ = shift; |
204 | |
205 | my $type = $self->type_constraint |
206 | or return 1; |
207 | my $constraint = $self->find_type_constraint |
208 | or return 1; |
209 | |
210 | return 1 if $constraint->($_); |
211 | |
212 | my $name = $self->name; |
1f2b4780 |
213 | local $_ = "undef" unless defined($_); |
5aa30ced |
214 | Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_"); |
215 | } |
216 | |
c3398f5b |
217 | 1; |
218 | |
219 | __END__ |
220 | |
221 | =head1 NAME |
222 | |
223 | Mouse::Attribute - attribute metaclass |
224 | |
225 | =head1 METHODS |
226 | |
227 | =head2 new %args -> Mouse::Attribute |
228 | |
229 | Instantiates a new Mouse::Attribute. Does nothing else. |
230 | |
231 | =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute |
232 | |
233 | Creates a new attribute in OwnerClass. Accessors and helper methods are |
234 | installed. Some error checking is done. |
235 | |
236 | =head2 name -> AttributeName |
237 | |
238 | =head2 class -> OwnerClass |
239 | |
240 | =head2 default -> Value |
241 | |
242 | =head2 predicate -> MethodName |
243 | |
244 | =head2 clearer -> MethodName |
245 | |
246 | =head2 handles -> { LocalName => RemoteName } |
247 | |
248 | =head2 weak_ref -> Bool |
249 | |
250 | =head2 init_arg -> Str |
251 | |
252 | Informational methods. |
253 | |
254 | =head2 generate_accessor -> CODE |
255 | |
256 | Creates a new code reference for the attribute's accessor. |
257 | |
258 | =head2 generate_predicate -> CODE |
259 | |
260 | Creates a new code reference for the attribute's predicate. |
261 | |
262 | =head2 generate_clearer -> CODE |
263 | |
264 | Creates a new code reference for the attribute's clearer. |
265 | |
266 | =head2 generate_handles -> { MethodName => CODE } |
267 | |
268 | Creates a new code reference for each of the attribute's handles methods. |
269 | |
270 | =cut |
271 | |