1 package Mouse::Meta::Attribute;
8 use Mouse::Meta::TypeConstraint;
11 my ($class, $name, %options) = @_;
13 $options{name} = $name;
15 $options{init_arg} = $name
16 unless exists $options{init_arg};
20 bless \%options, $class;
23 sub name { $_[0]->{name} }
24 sub associated_class { $_[0]->{associated_class} }
25 sub _is_metadata { $_[0]->{is} }
26 sub is_required { $_[0]->{required} }
27 sub default { $_[0]->{default} }
28 sub is_lazy { $_[0]->{lazy} }
29 sub is_lazy_build { $_[0]->{lazy_build} }
30 sub predicate { $_[0]->{predicate} }
31 sub clearer { $_[0]->{clearer} }
32 sub handles { $_[0]->{handles} }
33 sub is_weak_ref { $_[0]->{weak_ref} }
34 sub init_arg { $_[0]->{init_arg} }
35 sub type_constraint { $_[0]->{type_constraint} }
36 sub find_type_constraint {
37 Carp::carp("This method was deprecated");
38 $_[0]->type_constraint();
40 sub trigger { $_[0]->{trigger} }
41 sub builder { $_[0]->{builder} }
42 sub should_auto_deref { $_[0]->{auto_deref} }
43 sub should_coerce { $_[0]->{should_coerce} }
45 sub has_default { exists $_[0]->{default} }
46 sub has_predicate { exists $_[0]->{predicate} }
47 sub has_clearer { exists $_[0]->{clearer} }
48 sub has_handles { exists $_[0]->{handles} }
49 sub has_type_constraint { exists $_[0]->{type_constraint} }
50 sub has_trigger { exists $_[0]->{trigger} }
51 sub has_builder { exists $_[0]->{builder} }
54 $_[0]->{_create_args} = $_[1] if @_ > 1;
60 my $name = $self->name;
61 my $key = "'" . $name . "'";
65 sub generate_accessor {
66 my $attribute = shift;
68 my $name = $attribute->name;
69 my $default = $attribute->default;
70 my $constraint = $attribute->type_constraint;
71 my $builder = $attribute->builder;
72 my $trigger = $attribute->trigger;
73 my $is_weak = $attribute->is_weak_ref;
74 my $should_deref = $attribute->should_auto_deref;
75 my $should_coerce = $attribute->should_coerce;
77 my $compiled_type_constraint = $constraint ? $constraint->{_compiled_type_constraint} : undef;
80 my $key = $attribute->inlined_name;
83 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
85 if ($attribute->_is_metadata eq 'rw') {
87 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
88 'if (@_ >= 2) {' . "\n";
93 $accessor .= 'my $val = ';
97 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
98 'Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
100 $accessor .= $value.';';
102 if ($compiled_type_constraint) {
105 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
106 'unless ($compiled_type_constraint->($val)) {
107 $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint});
112 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
113 'unless ($constraint->check($val)) {
114 $attribute->verify_type_constraint_error($name, $val, $attribute->{type_constraint});
120 # if there's nothing left to do for the attribute we can return during
122 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
124 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
127 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
131 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
137 $accessor .= 'confess "Cannot assign a value to a read-only accessor" if scalar(@_) >= 2;' . "\n";
140 if ($attribute->is_lazy) {
141 $accessor .= $self.'->{'.$key.'} = ';
143 $accessor .= $attribute->has_builder
145 : ref($default) eq 'CODE'
146 ? '$default->('.$self.')'
148 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
152 my $type_constraint = $attribute->{type_constraint};
153 if (ref($type_constraint) && $type_constraint->name eq 'ArrayRef') {
154 $accessor .= 'if (wantarray) {
155 return @{ '.$self.'->{'.$key.'} || [] };
159 $accessor .= 'if (wantarray) {
160 return %{ '.$self.'->{'.$key.'} || {} };
165 $accessor .= 'return '.$self.'->{'.$key.'};
168 my $sub = eval $accessor;
173 sub generate_predicate {
174 my $attribute = shift;
175 my $key = $attribute->inlined_name;
177 my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
179 my $sub = eval $predicate;
184 sub generate_clearer {
185 my $attribute = shift;
186 my $key = $attribute->inlined_name;
188 my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
190 my $sub = eval $clearer;
195 sub generate_handles {
196 my $attribute = shift;
197 my $reader = $attribute->name;
198 my %handles = $attribute->_canonicalize_handles($attribute->handles);
202 for my $local_method (keys %handles) {
203 my $remote_method = $handles{$local_method};
207 $self->'.$reader.'->'.$remote_method.'(@_)
210 $method_map{$local_method} = eval $method;
218 my ($self, $class, $name, %args) = @_;
221 $args{associated_class} = $class;
223 %args = $self->canonicalize_args($name, %args);
224 $self->validate_args($name, \%args);
226 $args{should_coerce} = delete $args{coerce}
227 if exists $args{coerce};
229 if (exists $args{isa}) {
230 confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)"
231 if $args{isa} =~ /^([^\[]+)\[.+\]$/ &&
237 my $type_constraint = delete $args{isa};
238 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
241 my $attribute = $self->new($name, %args);
243 $attribute->_create_args(\%args);
245 $class->add_attribute($attribute);
247 # install an accessor
248 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
249 my $accessor = $attribute->generate_accessor;
250 $class->add_method($name => $accessor);
253 for my $method (qw/predicate clearer/) {
254 my $predicate = "has_$method";
255 if ($attribute->$predicate) {
256 my $generator = "generate_$method";
257 my $coderef = $attribute->$generator;
258 $class->add_method($attribute->$method => $coderef);
262 if ($attribute->has_handles) {
263 my $method_map = $attribute->generate_handles;
264 for my $method_name (keys %$method_map) {
265 $class->add_method($method_name => $method_map->{$method_name});
272 sub canonicalize_args {
277 if ($args{lazy_build}) {
280 $args{builder} = "_build_${name}"
281 if !exists($args{builder});
283 $args{clearer} = "_clear${name}" if !exists($args{clearer});
284 $args{predicate} = "_has${name}" if !exists($args{predicate});
287 $args{clearer} = "clear_${name}" if !exists($args{clearer});
288 $args{predicate} = "has_${name}" if !exists($args{predicate});
300 confess "You can not use lazy_build and default for the same attribute ($name)"
301 if $args->{lazy_build} && exists $args->{default};
303 confess "You cannot have lazy attribute ($name) without specifying a default value for it"
305 && !exists($args->{default})
306 && !exists($args->{builder});
308 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
309 if ref($args->{default})
310 && ref($args->{default}) ne 'CODE';
312 confess "You cannot auto-dereference without specifying a type constraint on attribute ($name)"
313 if $args->{auto_deref} && !exists($args->{isa});
315 confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)"
316 if $args->{auto_deref}
317 && $args->{isa} ne 'ArrayRef'
318 && $args->{isa} ne 'HashRef';
320 if ($args->{trigger}) {
321 if (ref($args->{trigger}) eq 'HASH') {
322 Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.";
325 confess "Trigger must be a CODE ref on attribute ($name)"
326 if ref($args->{trigger}) ne 'CODE';
332 sub verify_against_type_constraint {
333 return 1 unless $_[0]->{type_constraint};
336 return 1 if $_[0]->{type_constraint}->check($_);
339 $self->verify_type_constraint_error($self->name, $_, $self->{type_constraint});
342 sub verify_type_constraint_error {
343 my($self, $name, $value, $type) = @_;
344 $type = ref($type) eq 'ARRAY' ? join '|', map { $_->name } @{ $type } : $type->name;
345 my $display = defined($value) ? overload::StrVal($value) : 'undef';
346 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
349 sub coerce_constraint { ## my($self, $value) = @_;
350 my $type = $_[0]->{type_constraint}
352 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
355 sub _canonicalize_handles {
359 if (ref($handles) eq 'HASH') {
362 elsif (ref($handles) eq 'ARRAY') {
363 return map { $_ => $_ } @$handles;
366 confess "Unable to canonicalize the 'handles' option with $handles";
374 my %args = ($self->get_parent_args($class, $name), @_);
376 $self->create($class, $name, %args);
379 sub get_parent_args {
384 for my $super ($class->linearized_isa) {
385 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
387 return %{ $super_attr->_create_args };
390 confess "Could not find an attribute by the name of '$name' to inherit from";
399 Mouse::Meta::Attribute - attribute metaclass
403 =head2 new %args -> Mouse::Meta::Attribute
405 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
407 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
409 Creates a new attribute in OwnerClass. Accessors and helper methods are
410 installed. Some error checking is done.
412 =head2 name -> AttributeName
414 =head2 associated_class -> OwnerClass
416 =head2 is_required -> Bool
418 =head2 default -> Item
420 =head2 has_default -> Bool
422 =head2 is_lazy -> Bool
424 =head2 predicate -> MethodName | Undef
426 =head2 has_predicate -> Bool
428 =head2 clearer -> MethodName | Undef
430 =head2 has_clearer -> Bool
432 =head2 handles -> { LocalName => RemoteName }
434 =head2 has_handles -> Bool
436 =head2 is_weak_ref -> Bool
438 =head2 init_arg -> Str
440 =head2 type_constraint -> Str
442 =head2 has_type_constraint -> Bool
444 =head2 trigger => CODE | Undef
446 =head2 has_trigger -> Bool
448 =head2 builder => MethodName | Undef
450 =head2 has_builder -> Bool
452 =head2 is_lazy_build => Bool
454 =head2 should_auto_deref -> Bool
456 Informational methods.
458 =head2 generate_accessor -> CODE
460 Creates a new code reference for the attribute's accessor.
462 =head2 generate_predicate -> CODE
464 Creates a new code reference for the attribute's predicate.
466 =head2 generate_clearer -> CODE
468 Creates a new code reference for the attribute's clearer.
470 =head2 generate_handles -> { MethodName => CODE }
472 Creates a new code reference for each of the attribute's handles methods.
474 =head2 verify_against_type_constraint Item -> 1 | ERROR
476 Checks that the given value passes this attribute's type constraint. Returns 1
477 on success, otherwise C<confess>es.
479 =head2 canonicalize_args Name, %args -> %args
481 Canonicalizes some arguments to create. In particular, C<lazy_build> is
482 canonicalized into C<lazy>, C<builder>, etc.
484 =head2 validate_args Name, \%args -> 1 | ERROR
486 Checks that the arguments to create the attribute (ie those specified by
489 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
491 Creates a new attribute in OwnerClass, inheriting options from parent classes.
492 Accessors and helper methods are installed. Some error checking is done.
494 =head2 get_parent_args OwnerClass, AttributeName -> Hash
496 Returns the options that the parent class of C<OwnerClass> used for attribute