bug fixed. test is very important things!
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
CommitLineData
306290e8 1package Mouse::Meta::Attribute;
c3398f5b 2use strict;
3use warnings;
3b8fe109 4require overload;
c3398f5b 5
6use Carp 'confess';
6c169c50 7use Scalar::Util ();
684db121 8use Mouse::Meta::TypeConstraint;
ca5a9ec1 9use Mouse::Meta::Method::Accessor;
c3398f5b 10
11sub new {
2608b115 12 my ($class, $name, %options) = @_;
c3398f5b 13
2608b115 14 $options{name} = $name;
2e7e86c6 15
2608b115 16 $options{init_arg} = $name
17 unless exists $options{init_arg};
45959ffa 18
2608b115 19 $options{is} ||= '';
c3398f5b 20
2608b115 21 bless \%options, $class;
c3398f5b 22}
23
f6715552 24sub name { $_[0]->{name} }
25sub associated_class { $_[0]->{associated_class} }
26sub _is_metadata { $_[0]->{is} }
27sub is_required { $_[0]->{required} }
28sub default { $_[0]->{default} }
29sub is_lazy { $_[0]->{lazy} }
30sub is_lazy_build { $_[0]->{lazy_build} }
31sub predicate { $_[0]->{predicate} }
32sub clearer { $_[0]->{clearer} }
33sub handles { $_[0]->{handles} }
34sub is_weak_ref { $_[0]->{weak_ref} }
35sub init_arg { $_[0]->{init_arg} }
36sub type_constraint { $_[0]->{type_constraint} }
4c03ed87 37sub find_type_constraint {
38 Carp::carp("This method was deprecated");
39 $_[0]->type_constraint();
40}
f6715552 41sub trigger { $_[0]->{trigger} }
42sub builder { $_[0]->{builder} }
43sub should_auto_deref { $_[0]->{auto_deref} }
44sub should_coerce { $_[0]->{should_coerce} }
c3398f5b 45
f6715552 46sub has_default { exists $_[0]->{default} }
47sub has_predicate { exists $_[0]->{predicate} }
48sub has_clearer { exists $_[0]->{clearer} }
49sub has_handles { exists $_[0]->{handles} }
50sub has_type_constraint { exists $_[0]->{type_constraint} }
51sub has_trigger { exists $_[0]->{trigger} }
52sub has_builder { exists $_[0]->{builder} }
eec1bb49 53
1bfebf5f 54sub _create_args {
55 $_[0]->{_create_args} = $_[1] if @_ > 1;
56 $_[0]->{_create_args}
57}
58
7f7406a5 59sub inlined_name {
60 my $self = shift;
61 my $name = $self->name;
62 my $key = "'" . $name . "'";
63 return $key;
64}
65
c3398f5b 66sub generate_predicate {
67 my $attribute = shift;
d10fd510 68 my $key = $attribute->inlined_name;
c3398f5b 69
d10fd510 70 my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
c3398f5b 71
71b948d1 72 my $sub = eval $predicate;
73 confess $@ if $@;
74 return $sub;
c3398f5b 75}
76
77sub generate_clearer {
78 my $attribute = shift;
d10fd510 79 my $key = $attribute->inlined_name;
c3398f5b 80
d10fd510 81 my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
c3398f5b 82
71b948d1 83 my $sub = eval $clearer;
84 confess $@ if $@;
85 return $sub;
c3398f5b 86}
87
88sub generate_handles {
89 my $attribute = shift;
2434d21b 90 my $reader = $attribute->name;
c3cc3642 91 my %handles = $attribute->_canonicalize_handles($attribute->handles);
c3398f5b 92
93 my %method_map;
94
c3cc3642 95 for my $local_method (keys %handles) {
96 my $remote_method = $handles{$local_method};
c3398f5b 97
98 my $method = 'sub {
99 my $self = shift;
0ecefe82 100 $self->'.$reader.'->'.$remote_method.'(@_)
c3398f5b 101 }';
102
103 $method_map{$local_method} = eval $method;
71b948d1 104 confess $@ if $@;
c3398f5b 105 }
106
107 return \%method_map;
108}
109
110sub create {
111 my ($self, $class, $name, %args) = @_;
112
1bfebf5f 113 $args{name} = $name;
181502b9 114 $args{associated_class} = $class;
1bfebf5f 115
93d190e0 116 %args = $self->canonicalize_args($name, %args);
1bbaa8ed 117 $self->validate_args($name, \%args);
45ea8620 118
32af3489 119 $args{should_coerce} = delete $args{coerce}
4188b837 120 if exists $args{coerce};
121
eec1bb49 122 if (exists $args{isa}) {
c1c94293 123 confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)"
315b97c2 124 if $args{isa} =~ /^([^\[]+)\[.+\]$/ &&
125 $1 ne 'ArrayRef' &&
a510e63c 126 $1 ne 'HashRef' &&
127 $1 ne 'Maybe'
128 ;
5fa003bf 129
eec1bb49 130 my $type_constraint = delete $args{isa};
684db121 131 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
eec1bb49 132 }
186657a9 133
2608b115 134 my $attribute = $self->new($name, %args);
1bfebf5f 135
724c77c0 136 $attribute->_create_args(\%args);
c3398f5b 137
724c77c0 138 $class->add_attribute($attribute);
b2500191 139
c3398f5b 140 # install an accessor
2434d21b 141 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
ca5a9ec1 142 my $code = Mouse::Meta::Method::Accessor->generate_accessor_method_inline(
143 $attribute,
144 );
145 $class->add_method($name => $code);
c3398f5b 146 }
147
c3398f5b 148 for my $method (qw/predicate clearer/) {
2434d21b 149 my $predicate = "has_$method";
150 if ($attribute->$predicate) {
c3398f5b 151 my $generator = "generate_$method";
152 my $coderef = $attribute->$generator;
724c77c0 153 $class->add_method($attribute->$method => $coderef);
c3398f5b 154 }
155 }
156
2434d21b 157 if ($attribute->has_handles) {
c3398f5b 158 my $method_map = $attribute->generate_handles;
159 for my $method_name (keys %$method_map) {
724c77c0 160 $class->add_method($method_name => $method_map->{$method_name});
c3398f5b 161 }
162 }
163
164 return $attribute;
165}
166
93d190e0 167sub canonicalize_args {
168 my $self = shift;
169 my $name = shift;
170 my %args = @_;
171
172 if ($args{lazy_build}) {
173 $args{lazy} = 1;
174 $args{required} = 1;
175 $args{builder} = "_build_${name}"
176 if !exists($args{builder});
177 if ($name =~ /^_/) {
178 $args{clearer} = "_clear${name}" if !exists($args{clearer});
179 $args{predicate} = "_has${name}" if !exists($args{predicate});
180 }
181 else {
182 $args{clearer} = "clear_${name}" if !exists($args{clearer});
183 $args{predicate} = "has_${name}" if !exists($args{predicate});
184 }
185 }
186
187 return %args;
188}
189
8fd9e611 190sub validate_args {
191 my $self = shift;
192 my $name = shift;
1bbaa8ed 193 my $args = shift;
8fd9e611 194
93d190e0 195 confess "You can not use lazy_build and default for the same attribute ($name)"
1bbaa8ed 196 if $args->{lazy_build} && exists $args->{default};
93d190e0 197
8fd9e611 198 confess "You cannot have lazy attribute ($name) without specifying a default value for it"
1bbaa8ed 199 if $args->{lazy}
200 && !exists($args->{default})
201 && !exists($args->{builder});
8fd9e611 202
203 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
1bbaa8ed 204 if ref($args->{default})
205 && ref($args->{default}) ne 'CODE';
8fd9e611 206
97661b77 207 confess "You cannot auto-dereference without specifying a type constraint on attribute ($name)"
1bbaa8ed 208 if $args->{auto_deref} && !exists($args->{isa});
8fd9e611 209
97661b77 210 confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)"
1bbaa8ed 211 if $args->{auto_deref}
212 && $args->{isa} ne 'ArrayRef'
213 && $args->{isa} ne 'HashRef';
8fd9e611 214
506db557 215 if ($args->{trigger}) {
a08e715f 216 if (ref($args->{trigger}) eq 'HASH') {
217 Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.";
844fa049 218 }
506db557 219
844fa049 220 confess "Trigger must be a CODE ref on attribute ($name)"
a08e715f 221 if ref($args->{trigger}) ne 'CODE';
506db557 222 }
6c5498d0 223
8fd9e611 224 return 1;
225}
226
20e25eb9 227sub verify_against_type_constraint {
8a7f2a8a 228 return 1 unless $_[0]->{type_constraint};
5aa30ced 229
8a7f2a8a 230 local $_ = $_[1];
684db121 231 return 1 if $_[0]->{type_constraint}->check($_);
5aa30ced 232
8a7f2a8a 233 my $self = shift;
684db121 234 $self->verify_type_constraint_error($self->name, $_, $self->{type_constraint});
b3b74cc6 235}
5aa30ced 236
b3b74cc6 237sub verify_type_constraint_error {
238 my($self, $name, $value, $type) = @_;
29607c02 239 Carp::confess("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
5aa30ced 240}
241
8a7f2a8a 242sub coerce_constraint { ## my($self, $value) = @_;
243 my $type = $_[0]->{type_constraint}
244 or return $_[1];
684db121 245 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
4188b837 246}
247
af745d5a 248sub _canonicalize_handles {
249 my $self = shift;
250 my $handles = shift;
251
252 if (ref($handles) eq 'HASH') {
253 return %$handles;
254 }
255 elsif (ref($handles) eq 'ARRAY') {
256 return map { $_ => $_ } @$handles;
257 }
258 else {
259 confess "Unable to canonicalize the 'handles' option with $handles";
260 }
261}
262
1bfebf5f 263sub clone_parent {
264 my $self = shift;
265 my $class = shift;
266 my $name = shift;
267 my %args = ($self->get_parent_args($class, $name), @_);
268
269 $self->create($class, $name, %args);
270}
271
272sub get_parent_args {
273 my $self = shift;
274 my $class = shift;
275 my $name = shift;
276
724c77c0 277 for my $super ($class->linearized_isa) {
bb733405 278 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
1bfebf5f 279 or next;
280 return %{ $super_attr->_create_args };
281 }
282
283 confess "Could not find an attribute by the name of '$name' to inherit from";
284}
285
c3398f5b 2861;
287
288__END__
289
290=head1 NAME
291
306290e8 292Mouse::Meta::Attribute - attribute metaclass
c3398f5b 293
294=head1 METHODS
295
306290e8 296=head2 new %args -> Mouse::Meta::Attribute
c3398f5b 297
306290e8 298Instantiates a new Mouse::Meta::Attribute. Does nothing else.
c3398f5b 299
306290e8 300=head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
c3398f5b 301
302Creates a new attribute in OwnerClass. Accessors and helper methods are
303installed. Some error checking is done.
304
305=head2 name -> AttributeName
306
181502b9 307=head2 associated_class -> OwnerClass
c3398f5b 308
ab27a55e 309=head2 is_required -> Bool
c3398f5b 310
ab27a55e 311=head2 default -> Item
c3398f5b 312
ab27a55e 313=head2 has_default -> Bool
314
315=head2 is_lazy -> Bool
316
317=head2 predicate -> MethodName | Undef
318
319=head2 has_predicate -> Bool
320
321=head2 clearer -> MethodName | Undef
322
323=head2 has_clearer -> Bool
c3398f5b 324
325=head2 handles -> { LocalName => RemoteName }
326
ab27a55e 327=head2 has_handles -> Bool
328
3645b316 329=head2 is_weak_ref -> Bool
c3398f5b 330
331=head2 init_arg -> Str
332
ab27a55e 333=head2 type_constraint -> Str
334
335=head2 has_type_constraint -> Bool
336
337=head2 trigger => CODE | Undef
338
339=head2 has_trigger -> Bool
340
341=head2 builder => MethodName | Undef
342
343=head2 has_builder -> Bool
344
93f08899 345=head2 is_lazy_build => Bool
346
0fff36e6 347=head2 should_auto_deref -> Bool
348
c3398f5b 349Informational methods.
350
351=head2 generate_accessor -> CODE
352
353Creates a new code reference for the attribute's accessor.
354
355=head2 generate_predicate -> CODE
356
357Creates a new code reference for the attribute's predicate.
358
359=head2 generate_clearer -> CODE
360
361Creates a new code reference for the attribute's clearer.
362
363=head2 generate_handles -> { MethodName => CODE }
364
365Creates a new code reference for each of the attribute's handles methods.
366
20e25eb9 367=head2 verify_against_type_constraint Item -> 1 | ERROR
fb706f5c 368
369Checks that the given value passes this attribute's type constraint. Returns 1
370on success, otherwise C<confess>es.
371
93d190e0 372=head2 canonicalize_args Name, %args -> %args
373
374Canonicalizes some arguments to create. In particular, C<lazy_build> is
375canonicalized into C<lazy>, C<builder>, etc.
376
1bbaa8ed 377=head2 validate_args Name, \%args -> 1 | ERROR
93d190e0 378
379Checks that the arguments to create the attribute (ie those specified by
380C<has>) are valid.
381
f7b11a21 382=head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
383
384Creates a new attribute in OwnerClass, inheriting options from parent classes.
385Accessors and helper methods are installed. Some error checking is done.
386
387=head2 get_parent_args OwnerClass, AttributeName -> Hash
388
389Returns the options that the parent class of C<OwnerClass> used for attribute
390C<AttributeName>.
391
c3398f5b 392=cut
393