Add a test file for throw_error()
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
CommitLineData
306290e8 1package Mouse::Meta::Attribute;
c3398f5b 2use strict;
3use warnings;
4
5use Carp 'confess';
6c169c50 6use Scalar::Util ();
684db121 7use Mouse::Meta::TypeConstraint;
c3398f5b 8
9sub new {
2608b115 10 my ($class, $name, %options) = @_;
c3398f5b 11
2608b115 12 $options{name} = $name;
2e7e86c6 13
2608b115 14 $options{init_arg} = $name
15 unless exists $options{init_arg};
45959ffa 16
2608b115 17 $options{is} ||= '';
c3398f5b 18
2608b115 19 bless \%options, $class;
c3398f5b 20}
21
f6715552 22sub name { $_[0]->{name} }
23sub associated_class { $_[0]->{associated_class} }
24sub _is_metadata { $_[0]->{is} }
25sub is_required { $_[0]->{required} }
26sub default { $_[0]->{default} }
27sub is_lazy { $_[0]->{lazy} }
28sub is_lazy_build { $_[0]->{lazy_build} }
29sub predicate { $_[0]->{predicate} }
30sub clearer { $_[0]->{clearer} }
31sub handles { $_[0]->{handles} }
32sub is_weak_ref { $_[0]->{weak_ref} }
33sub init_arg { $_[0]->{init_arg} }
34sub type_constraint { $_[0]->{type_constraint} }
4c03ed87 35sub find_type_constraint {
36 Carp::carp("This method was deprecated");
37 $_[0]->type_constraint();
38}
f6715552 39sub trigger { $_[0]->{trigger} }
40sub builder { $_[0]->{builder} }
41sub should_auto_deref { $_[0]->{auto_deref} }
42sub should_coerce { $_[0]->{should_coerce} }
c3398f5b 43
f6715552 44sub has_default { exists $_[0]->{default} }
45sub has_predicate { exists $_[0]->{predicate} }
46sub has_clearer { exists $_[0]->{clearer} }
47sub has_handles { exists $_[0]->{handles} }
48sub has_type_constraint { exists $_[0]->{type_constraint} }
49sub has_trigger { exists $_[0]->{trigger} }
50sub has_builder { exists $_[0]->{builder} }
eec1bb49 51
1bfebf5f 52sub _create_args {
53 $_[0]->{_create_args} = $_[1] if @_ > 1;
54 $_[0]->{_create_args}
55}
56
b5bc67d9 57sub _inlined_name {
7f7406a5 58 my $self = shift;
b5bc67d9 59 return sprintf '"%s"', quotemeta $self->name;
7f7406a5 60}
61
b5bc67d9 62sub _generate_accessor{
9c3c9a02 63 my ($attribute) = @_;
64
65 my $name = $attribute->name;
66 my $default = $attribute->default;
67 my $constraint = $attribute->type_constraint;
68 my $builder = $attribute->builder;
69 my $trigger = $attribute->trigger;
70 my $is_weak = $attribute->is_weak_ref;
71 my $should_deref = $attribute->should_auto_deref;
72 my $should_coerce = $attribute->should_coerce;
73
74 my $compiled_type_constraint = $constraint ? $constraint->{_compiled_type_constraint} : undef;
75
76 my $self = '$_[0]';
b5bc67d9 77 my $key = $attribute->_inlined_name;
9c3c9a02 78
79 my $accessor =
80 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
81 "sub {\n";
82 if ($attribute->_is_metadata eq 'rw') {
83 $accessor .=
84 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
85 'if (scalar(@_) >= 2) {' . "\n";
86
87 my $value = '$_[1]';
88
89 if ($constraint) {
90 if ($should_coerce) {
91 $accessor .=
92 "\n".
93 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
94 'my $val = Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
95 $value = '$val';
96 }
97 if ($compiled_type_constraint) {
98 $accessor .=
99 "\n".
100 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
101 'unless ($compiled_type_constraint->('.$value.')) {
102 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
103 }' . "\n";
104 } else {
105 $accessor .=
106 "\n".
107 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
108 'unless ($constraint->check('.$value.')) {
109 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
110 }' . "\n";
111 }
112 }
113
114 # if there's nothing left to do for the attribute we can return during
115 # this setter
116 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
117
118 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
119
120 if ($is_weak) {
121 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
122 }
123
124 if ($trigger) {
125 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
126 }
127
128 $accessor .= "}\n";
129 }
130 else {
131 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
132 }
133
134 if ($attribute->is_lazy) {
135 $accessor .= $self.'->{'.$key.'} = ';
136
137 $accessor .= $attribute->has_builder
138 ? $self.'->$builder'
139 : ref($default) eq 'CODE'
140 ? '$default->('.$self.')'
141 : '$default';
142 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
143 }
144
145 if ($should_deref) {
146 if (ref($constraint) && $constraint->name =~ '^ArrayRef\b') {
147 $accessor .= 'if (wantarray) {
148 return @{ '.$self.'->{'.$key.'} || [] };
149 }';
150 }
151 else {
152 $accessor .= 'if (wantarray) {
153 return %{ '.$self.'->{'.$key.'} || {} };
154 }';
155 }
156 }
157
158 $accessor .= 'return '.$self.'->{'.$key.'};
159 }';
160
161 my $sub = eval $accessor;
fce211ae 162 $attribute->throw_error($@) if $@;
9c3c9a02 163 return $sub;
164}
165
166
b5bc67d9 167sub _generate_predicate {
c3398f5b 168 my $attribute = shift;
b5bc67d9 169 my $key = $attribute->_inlined_name;
c3398f5b 170
d10fd510 171 my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
c3398f5b 172
71b948d1 173 my $sub = eval $predicate;
fce211ae 174 $attribute->throw_error($@) if $@;
71b948d1 175 return $sub;
c3398f5b 176}
177
b5bc67d9 178sub _generate_clearer {
c3398f5b 179 my $attribute = shift;
b5bc67d9 180 my $key = $attribute->_inlined_name;
c3398f5b 181
d10fd510 182 my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
c3398f5b 183
71b948d1 184 my $sub = eval $clearer;
fce211ae 185 $attribute->throw_error($@) if $@;
71b948d1 186 return $sub;
c3398f5b 187}
188
b5bc67d9 189sub _generate_handles {
c3398f5b 190 my $attribute = shift;
2434d21b 191 my $reader = $attribute->name;
c3cc3642 192 my %handles = $attribute->_canonicalize_handles($attribute->handles);
c3398f5b 193
194 my %method_map;
195
c3cc3642 196 for my $local_method (keys %handles) {
197 my $remote_method = $handles{$local_method};
c3398f5b 198
199 my $method = 'sub {
200 my $self = shift;
0ecefe82 201 $self->'.$reader.'->'.$remote_method.'(@_)
c3398f5b 202 }';
203
204 $method_map{$local_method} = eval $method;
fce211ae 205 $attribute->throw_error($@) if $@;
c3398f5b 206 }
207
208 return \%method_map;
209}
210
211sub create {
212 my ($self, $class, $name, %args) = @_;
213
1bfebf5f 214 $args{name} = $name;
181502b9 215 $args{associated_class} = $class;
1bfebf5f 216
93d190e0 217 %args = $self->canonicalize_args($name, %args);
1bbaa8ed 218 $self->validate_args($name, \%args);
45ea8620 219
32af3489 220 $args{should_coerce} = delete $args{coerce}
4188b837 221 if exists $args{coerce};
222
eec1bb49 223 if (exists $args{isa}) {
224 my $type_constraint = delete $args{isa};
684db121 225 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
eec1bb49 226 }
186657a9 227
2608b115 228 my $attribute = $self->new($name, %args);
1bfebf5f 229
724c77c0 230 $attribute->_create_args(\%args);
c3398f5b 231
724c77c0 232 $class->add_attribute($attribute);
b2500191 233
74be9f76 234 my $associated_methods = 0;
235
236 my $is_metadata = $attribute->_is_metadata || '';
237
c3398f5b 238 # install an accessor
74be9f76 239 if ($is_metadata eq 'rw' || $is_metadata eq 'ro') {
b5bc67d9 240 my $code = $attribute->_generate_accessor();
ca5a9ec1 241 $class->add_method($name => $code);
74be9f76 242 $associated_methods++;
c3398f5b 243 }
244
c3398f5b 245 for my $method (qw/predicate clearer/) {
2434d21b 246 my $predicate = "has_$method";
247 if ($attribute->$predicate) {
b5bc67d9 248 my $generator = "_generate_$method";
c3398f5b 249 my $coderef = $attribute->$generator;
724c77c0 250 $class->add_method($attribute->$method => $coderef);
74be9f76 251 $associated_methods++;
c3398f5b 252 }
253 }
254
2434d21b 255 if ($attribute->has_handles) {
b5bc67d9 256 my $method_map = $attribute->_generate_handles;
c3398f5b 257 for my $method_name (keys %$method_map) {
724c77c0 258 $class->add_method($method_name => $method_map->{$method_name});
74be9f76 259 $associated_methods++;
c3398f5b 260 }
261 }
262
74be9f76 263 if($associated_methods == 0 && $is_metadata ne 'bare'){
c8c1aeaf 264 Carp::cluck(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
a7d31de0 265
74be9f76 266 }
267
c3398f5b 268 return $attribute;
269}
270
93d190e0 271sub canonicalize_args {
272 my $self = shift;
273 my $name = shift;
274 my %args = @_;
275
276 if ($args{lazy_build}) {
277 $args{lazy} = 1;
278 $args{required} = 1;
279 $args{builder} = "_build_${name}"
280 if !exists($args{builder});
281 if ($name =~ /^_/) {
282 $args{clearer} = "_clear${name}" if !exists($args{clearer});
283 $args{predicate} = "_has${name}" if !exists($args{predicate});
284 }
285 else {
286 $args{clearer} = "clear_${name}" if !exists($args{clearer});
287 $args{predicate} = "has_${name}" if !exists($args{predicate});
288 }
289 }
290
291 return %args;
292}
293
8fd9e611 294sub validate_args {
295 my $self = shift;
296 my $name = shift;
1bbaa8ed 297 my $args = shift;
8fd9e611 298
fce211ae 299 $self->throw_error("You can not use lazy_build and default for the same attribute ($name)")
1bbaa8ed 300 if $args->{lazy_build} && exists $args->{default};
93d190e0 301
fce211ae 302 $self->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it")
1bbaa8ed 303 if $args->{lazy}
304 && !exists($args->{default})
305 && !exists($args->{builder});
8fd9e611 306
fce211ae 307 $self->throw_error("References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
1bbaa8ed 308 if ref($args->{default})
309 && ref($args->{default}) ne 'CODE';
8fd9e611 310
fce211ae 311 $self->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)")
1bbaa8ed 312 if $args->{auto_deref} && !exists($args->{isa});
8fd9e611 313
fce211ae 314 $self->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)")
1bbaa8ed 315 if $args->{auto_deref}
a3f4f68e 316 && $args->{isa} !~ /^(?:ArrayRef|HashRef)(?:\[.*\])?$/;
8fd9e611 317
506db557 318 if ($args->{trigger}) {
a08e715f 319 if (ref($args->{trigger}) eq 'HASH') {
fce211ae 320 $self->throw_error("HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.");
844fa049 321 }
506db557 322
fce211ae 323 $self->throw_error("Trigger must be a CODE ref on attribute ($name)")
a08e715f 324 if ref($args->{trigger}) ne 'CODE';
506db557 325 }
6c5498d0 326
8fd9e611 327 return 1;
328}
329
20e25eb9 330sub verify_against_type_constraint {
f55f60dd 331 my ($self, $value) = @_;
332 my $tc = $self->type_constraint;
333 return 1 unless $tc;
5aa30ced 334
f55f60dd 335 local $_ = $value;
336 return 1 if $tc->check($value);
5aa30ced 337
f55f60dd 338 $self->verify_type_constraint_error($self->name, $value, $tc);
b3b74cc6 339}
5aa30ced 340
b3b74cc6 341sub verify_type_constraint_error {
342 my($self, $name, $value, $type) = @_;
fce211ae 343 $self->throw_error("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
5aa30ced 344}
345
8a7f2a8a 346sub coerce_constraint { ## my($self, $value) = @_;
347 my $type = $_[0]->{type_constraint}
348 or return $_[1];
684db121 349 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
4188b837 350}
351
af745d5a 352sub _canonicalize_handles {
353 my $self = shift;
354 my $handles = shift;
355
356 if (ref($handles) eq 'HASH') {
357 return %$handles;
358 }
359 elsif (ref($handles) eq 'ARRAY') {
360 return map { $_ => $_ } @$handles;
361 }
362 else {
fce211ae 363 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
af745d5a 364 }
365}
366
1bfebf5f 367sub clone_parent {
368 my $self = shift;
369 my $class = shift;
370 my $name = shift;
371 my %args = ($self->get_parent_args($class, $name), @_);
372
373 $self->create($class, $name, %args);
374}
375
376sub get_parent_args {
377 my $self = shift;
378 my $class = shift;
379 my $name = shift;
380
724c77c0 381 for my $super ($class->linearized_isa) {
bb733405 382 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
1bfebf5f 383 or next;
384 return %{ $super_attr->_create_args };
385 }
386
fce211ae 387 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
388}
389
390sub throw_error{
391 my $self = shift;
392
393 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
394 $metaclass->throw_error(@_, depth => 1);
1bfebf5f 395}
396
c3398f5b 3971;
398
399__END__
400
401=head1 NAME
402
306290e8 403Mouse::Meta::Attribute - attribute metaclass
c3398f5b 404
405=head1 METHODS
406
306290e8 407=head2 new %args -> Mouse::Meta::Attribute
c3398f5b 408
306290e8 409Instantiates a new Mouse::Meta::Attribute. Does nothing else.
c3398f5b 410
306290e8 411=head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
c3398f5b 412
413Creates a new attribute in OwnerClass. Accessors and helper methods are
414installed. Some error checking is done.
415
416=head2 name -> AttributeName
417
181502b9 418=head2 associated_class -> OwnerClass
c3398f5b 419
ab27a55e 420=head2 is_required -> Bool
c3398f5b 421
ab27a55e 422=head2 default -> Item
c3398f5b 423
ab27a55e 424=head2 has_default -> Bool
425
426=head2 is_lazy -> Bool
427
428=head2 predicate -> MethodName | Undef
429
430=head2 has_predicate -> Bool
431
432=head2 clearer -> MethodName | Undef
433
434=head2 has_clearer -> Bool
c3398f5b 435
436=head2 handles -> { LocalName => RemoteName }
437
ab27a55e 438=head2 has_handles -> Bool
439
3645b316 440=head2 is_weak_ref -> Bool
c3398f5b 441
442=head2 init_arg -> Str
443
ab27a55e 444=head2 type_constraint -> Str
445
446=head2 has_type_constraint -> Bool
447
448=head2 trigger => CODE | Undef
449
450=head2 has_trigger -> Bool
451
452=head2 builder => MethodName | Undef
453
454=head2 has_builder -> Bool
455
93f08899 456=head2 is_lazy_build => Bool
457
0fff36e6 458=head2 should_auto_deref -> Bool
459
c3398f5b 460Informational methods.
461
20e25eb9 462=head2 verify_against_type_constraint Item -> 1 | ERROR
fb706f5c 463
464Checks that the given value passes this attribute's type constraint. Returns 1
465on success, otherwise C<confess>es.
466
93d190e0 467=head2 canonicalize_args Name, %args -> %args
468
469Canonicalizes some arguments to create. In particular, C<lazy_build> is
470canonicalized into C<lazy>, C<builder>, etc.
471
1bbaa8ed 472=head2 validate_args Name, \%args -> 1 | ERROR
93d190e0 473
474Checks that the arguments to create the attribute (ie those specified by
475C<has>) are valid.
476
f7b11a21 477=head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
478
479Creates a new attribute in OwnerClass, inheriting options from parent classes.
480Accessors and helper methods are installed. Some error checking is done.
481
482=head2 get_parent_args OwnerClass, AttributeName -> Hash
483
484Returns the options that the parent class of C<OwnerClass> used for attribute
485C<AttributeName>.
486
c3398f5b 487=cut
488