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