1 package Mouse::Meta::Attribute;
7 use Mouse::Meta::TypeConstraint;
10 my ($class, $name, %options) = @_;
12 $options{name} = $name;
14 $options{init_arg} = $name
15 unless exists $options{init_arg};
19 bless \%options, $class;
22 sub name { $_[0]->{name} }
23 sub associated_class { $_[0]->{associated_class} }
24 sub _is_metadata { $_[0]->{is} }
25 sub is_required { $_[0]->{required} }
26 sub default { $_[0]->{default} }
27 sub is_lazy { $_[0]->{lazy} }
28 sub is_lazy_build { $_[0]->{lazy_build} }
29 sub predicate { $_[0]->{predicate} }
30 sub clearer { $_[0]->{clearer} }
31 sub handles { $_[0]->{handles} }
32 sub is_weak_ref { $_[0]->{weak_ref} }
33 sub init_arg { $_[0]->{init_arg} }
34 sub type_constraint { $_[0]->{type_constraint} }
35 sub find_type_constraint {
36 Carp::carp("This method was deprecated");
37 $_[0]->type_constraint();
39 sub trigger { $_[0]->{trigger} }
40 sub builder { $_[0]->{builder} }
41 sub should_auto_deref { $_[0]->{auto_deref} }
42 sub should_coerce { $_[0]->{should_coerce} }
44 sub has_default { exists $_[0]->{default} }
45 sub has_predicate { exists $_[0]->{predicate} }
46 sub has_clearer { exists $_[0]->{clearer} }
47 sub has_handles { exists $_[0]->{handles} }
48 sub has_type_constraint { exists $_[0]->{type_constraint} }
49 sub has_trigger { exists $_[0]->{trigger} }
50 sub has_builder { exists $_[0]->{builder} }
53 $_[0]->{_create_args} = $_[1] if @_ > 1;
59 return sprintf '"%s"', quotemeta $self->name;
62 sub _generate_accessor{
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;
74 my $compiled_type_constraint = $constraint ? $constraint->{_compiled_type_constraint} : undef;
77 my $key = $attribute->_inlined_name;
80 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
82 if ($attribute->_is_metadata eq 'rw') {
84 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
85 'if (scalar(@_) >= 2) {' . "\n";
93 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
94 'my $val = Mouse::Util::TypeConstraints->typecast_constraints("'.$attribute->associated_class->name.'", $attribute->{type_constraint}, '.$value.');';
97 if ($compiled_type_constraint) {
100 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
101 'unless ($compiled_type_constraint->('.$value.')) {
102 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
107 '#line ' . __LINE__ . ' "' . __FILE__ . "\"\n" .
108 'unless ($constraint->check('.$value.')) {
109 $attribute->verify_type_constraint_error($name, '.$value.', $attribute->{type_constraint});
114 # if there's nothing left to do for the attribute we can return during
116 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
118 $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n";
121 $accessor .= 'Scalar::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n";
125 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
131 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
134 if ($attribute->is_lazy) {
135 $accessor .= $self.'->{'.$key.'} = ';
137 $accessor .= $attribute->has_builder
139 : ref($default) eq 'CODE'
140 ? '$default->('.$self.')'
142 $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n";
146 if (ref($constraint) && $constraint->name =~ '^ArrayRef\b') {
147 $accessor .= 'if (wantarray) {
148 return @{ '.$self.'->{'.$key.'} || [] };
152 $accessor .= 'if (wantarray) {
153 return %{ '.$self.'->{'.$key.'} || {} };
158 $accessor .= 'return '.$self.'->{'.$key.'};
161 my $sub = eval $accessor;
162 Carp::confess($@) if $@;
167 sub _generate_predicate {
168 my $attribute = shift;
169 my $key = $attribute->_inlined_name;
171 my $predicate = 'sub { exists($_[0]->{'.$key.'}) }';
173 my $sub = eval $predicate;
178 sub _generate_clearer {
179 my $attribute = shift;
180 my $key = $attribute->_inlined_name;
182 my $clearer = 'sub { delete($_[0]->{'.$key.'}) }';
184 my $sub = eval $clearer;
189 sub _generate_handles {
190 my $attribute = shift;
191 my $reader = $attribute->name;
192 my %handles = $attribute->_canonicalize_handles($attribute->handles);
196 for my $local_method (keys %handles) {
197 my $remote_method = $handles{$local_method};
201 $self->'.$reader.'->'.$remote_method.'(@_)
204 $method_map{$local_method} = eval $method;
212 my ($self, $class, $name, %args) = @_;
215 $args{associated_class} = $class;
217 %args = $self->canonicalize_args($name, %args);
218 $self->validate_args($name, \%args);
220 $args{should_coerce} = delete $args{coerce}
221 if exists $args{coerce};
223 if (exists $args{isa}) {
224 confess "Got isa => $args{isa}, but Mouse does not yet support parameterized types for containers other than ArrayRef and HashRef (rt.cpan.org #39795)"
225 if $args{isa} =~ /^([^\[]+)\[.+\]$/ &&
231 my $type_constraint = delete $args{isa};
232 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
235 my $attribute = $self->new($name, %args);
237 $attribute->_create_args(\%args);
239 $class->add_attribute($attribute);
241 my $associated_methods = 0;
243 my $is_metadata = $attribute->_is_metadata || '';
245 # install an accessor
246 if ($is_metadata eq 'rw' || $is_metadata eq 'ro') {
247 my $code = $attribute->_generate_accessor();
248 $class->add_method($name => $code);
249 $associated_methods++;
252 for my $method (qw/predicate clearer/) {
253 my $predicate = "has_$method";
254 if ($attribute->$predicate) {
255 my $generator = "_generate_$method";
256 my $coderef = $attribute->$generator;
257 $class->add_method($attribute->$method => $coderef);
258 $associated_methods++;
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});
266 $associated_methods++;
270 if($associated_methods == 0 && $is_metadata ne 'bare'){
271 confess(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
277 sub canonicalize_args {
282 if ($args{lazy_build}) {
285 $args{builder} = "_build_${name}"
286 if !exists($args{builder});
288 $args{clearer} = "_clear${name}" if !exists($args{clearer});
289 $args{predicate} = "_has${name}" if !exists($args{predicate});
292 $args{clearer} = "clear_${name}" if !exists($args{clearer});
293 $args{predicate} = "has_${name}" if !exists($args{predicate});
305 confess "You can not use lazy_build and default for the same attribute ($name)"
306 if $args->{lazy_build} && exists $args->{default};
308 confess "You cannot have lazy attribute ($name) without specifying a default value for it"
310 && !exists($args->{default})
311 && !exists($args->{builder});
313 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
314 if ref($args->{default})
315 && ref($args->{default}) ne 'CODE';
317 confess "You cannot auto-dereference without specifying a type constraint on attribute ($name)"
318 if $args->{auto_deref} && !exists($args->{isa});
320 confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)"
321 if $args->{auto_deref}
322 && $args->{isa} !~ /^(?:ArrayRef|HashRef)(?:\[.*\])?$/;
324 if ($args->{trigger}) {
325 if (ref($args->{trigger}) eq 'HASH') {
326 Carp::carp "HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.";
329 confess "Trigger must be a CODE ref on attribute ($name)"
330 if ref($args->{trigger}) ne 'CODE';
336 sub verify_against_type_constraint {
337 my ($self, $value) = @_;
338 my $tc = $self->type_constraint;
342 return 1 if $tc->check($value);
344 $self->verify_type_constraint_error($self->name, $value, $tc);
347 sub verify_type_constraint_error {
348 my($self, $name, $value, $type) = @_;
349 Carp::confess("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
352 sub coerce_constraint { ## my($self, $value) = @_;
353 my $type = $_[0]->{type_constraint}
355 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
358 sub _canonicalize_handles {
362 if (ref($handles) eq 'HASH') {
365 elsif (ref($handles) eq 'ARRAY') {
366 return map { $_ => $_ } @$handles;
369 confess "Unable to canonicalize the 'handles' option with $handles";
377 my %args = ($self->get_parent_args($class, $name), @_);
379 $self->create($class, $name, %args);
382 sub get_parent_args {
387 for my $super ($class->linearized_isa) {
388 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
390 return %{ $super_attr->_create_args };
393 confess "Could not find an attribute by the name of '$name' to inherit from";
402 Mouse::Meta::Attribute - attribute metaclass
406 =head2 new %args -> Mouse::Meta::Attribute
408 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
410 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
412 Creates a new attribute in OwnerClass. Accessors and helper methods are
413 installed. Some error checking is done.
415 =head2 name -> AttributeName
417 =head2 associated_class -> OwnerClass
419 =head2 is_required -> Bool
421 =head2 default -> Item
423 =head2 has_default -> Bool
425 =head2 is_lazy -> Bool
427 =head2 predicate -> MethodName | Undef
429 =head2 has_predicate -> Bool
431 =head2 clearer -> MethodName | Undef
433 =head2 has_clearer -> Bool
435 =head2 handles -> { LocalName => RemoteName }
437 =head2 has_handles -> Bool
439 =head2 is_weak_ref -> Bool
441 =head2 init_arg -> Str
443 =head2 type_constraint -> Str
445 =head2 has_type_constraint -> Bool
447 =head2 trigger => CODE | Undef
449 =head2 has_trigger -> Bool
451 =head2 builder => MethodName | Undef
453 =head2 has_builder -> Bool
455 =head2 is_lazy_build => Bool
457 =head2 should_auto_deref -> Bool
459 Informational methods.
461 =head2 verify_against_type_constraint Item -> 1 | ERROR
463 Checks that the given value passes this attribute's type constraint. Returns 1
464 on success, otherwise C<confess>es.
466 =head2 canonicalize_args Name, %args -> %args
468 Canonicalizes some arguments to create. In particular, C<lazy_build> is
469 canonicalized into C<lazy>, C<builder>, etc.
471 =head2 validate_args Name, \%args -> 1 | ERROR
473 Checks that the arguments to create the attribute (ie those specified by
476 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
478 Creates a new attribute in OwnerClass, inheriting options from parent classes.
479 Accessors and helper methods are installed. Some error checking is done.
481 =head2 get_parent_args OwnerClass, AttributeName -> Hash
483 Returns the options that the parent class of C<OwnerClass> used for attribute