2 package Mouse::Meta::Attribute;
7 use Scalar::Util 'blessed';
13 my $name = $args{name};
15 $args{init_arg} = $name
16 unless exists $args{init_arg};
20 if ($args{lazy_build}) {
21 confess("You can not use lazy_build and default for the same attribute $name")
22 if exists $args{default};
25 $args{builder} = "_build_${name}"
26 if !exists($args{builder});
28 $args{clearer} = "_clear${name}" if !exists($args{clearer});
29 $args{predicate} = "_has${name}" if !exists($args{predicate});
32 $args{clearer} = "clear_${name}" if !exists($args{clearer});
33 $args{predicate} = "has_${name}" if !exists($args{predicate});
40 sub name { $_[0]->{name} }
41 sub class { $_[0]->{class} }
42 sub _is_metadata { $_[0]->{is} }
43 sub is_required { $_[0]->{required} }
44 sub default { $_[0]->{default} }
45 sub is_lazy { $_[0]->{lazy} }
46 sub is_lazy_build { $_[0]->{lazy_build} }
47 sub predicate { $_[0]->{predicate} }
48 sub clearer { $_[0]->{clearer} }
49 sub handles { $_[0]->{handles} }
50 sub is_weak_ref { $_[0]->{weak_ref} }
51 sub init_arg { $_[0]->{init_arg} }
52 sub type_constraint { $_[0]->{type_constraint} }
53 sub trigger { $_[0]->{trigger} }
54 sub builder { $_[0]->{builder} }
55 sub should_auto_deref { $_[0]->{auto_deref} }
57 sub has_default { exists $_[0]->{default} }
58 sub has_predicate { exists $_[0]->{predicate} }
59 sub has_clearer { exists $_[0]->{clearer} }
60 sub has_handles { exists $_[0]->{handles} }
61 sub has_type_constraint { exists $_[0]->{type_constraint} }
62 sub has_trigger { exists $_[0]->{trigger} }
63 sub has_builder { exists $_[0]->{builder} }
66 $_[0]->{_create_args} = $_[1] if @_ > 1;
70 sub generate_accessor {
71 my $attribute = shift;
73 my $name = $attribute->name;
75 my $default = $attribute->default;
76 my $trigger = $attribute->trigger;
77 my $type = $attribute->type_constraint;
78 my $constraint = $attribute->find_type_constraint;
79 my $builder = $attribute->builder;
84 if ($attribute->_is_metadata eq 'rw') {
85 $accessor .= 'if (@_) {
90 my $display = defined($_) ? overload::StrVal($_) : "undef";
91 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
95 $accessor .= '$self->{$key} = $_;';
97 if ($attribute->is_weak_ref) {
98 $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
102 $accessor .= '$trigger->($self, $_, $attribute);';
108 $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;';
111 if ($attribute->is_lazy) {
112 $accessor .= '$self->{$key} = ';
114 $accessor .= $attribute->has_builder
116 : ref($default) eq 'CODE'
117 ? '$default->($self)'
120 $accessor .= ' if !exists($self->{$key});';
123 if ($attribute->should_auto_deref) {
124 if ($attribute->type_constraint eq 'ArrayRef') {
125 $accessor .= 'if (wantarray) {
126 return @{ $self->{$key} || [] };
130 $accessor .= 'if (wantarray) {
131 return %{ $self->{$key} || {} };
136 $accessor .= 'return $self->{$key};
139 return eval $accessor;
142 sub generate_predicate {
143 my $attribute = shift;
144 my $key = $attribute->name;
146 my $predicate = 'sub { exists($_[0]->{$key}) }';
148 return eval $predicate;
151 sub generate_clearer {
152 my $attribute = shift;
153 my $key = $attribute->name;
155 my $predicate = 'sub { delete($_[0]->{$key}) }';
157 return eval $predicate;
160 sub generate_handles {
161 my $attribute = shift;
162 my $reader = $attribute->name;
163 my %handles = $attribute->_canonicalize_handles($attribute->handles);
167 for my $local_method (keys %handles) {
168 my $remote_method = $handles{$local_method};
172 $self->$reader->$remote_method(@_)
175 $method_map{$local_method} = eval $method;
182 my ($self, $class, $name, %args) = @_;
185 $args{class} = $class;
187 $self->validate_args($name, %args);
189 $args{type_constraint} = delete $args{isa}
190 if exists $args{isa};
192 my $attribute = $self->new(%args);
193 $attribute->_create_args(\%args);
195 my $meta = $class->meta;
197 $meta->add_attribute($attribute);
199 # install an accessor
200 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
201 my $accessor = $attribute->generate_accessor;
203 *{ $class . '::' . $name } = $accessor;
206 for my $method (qw/predicate clearer/) {
207 my $predicate = "has_$method";
208 if ($attribute->$predicate) {
209 my $generator = "generate_$method";
210 my $coderef = $attribute->$generator;
212 *{ $class . '::' . $attribute->$method } = $coderef;
216 if ($attribute->has_handles) {
217 my $method_map = $attribute->generate_handles;
218 for my $method_name (keys %$method_map) {
220 *{ $class . '::' . $method_name } = $method_map->{$method_name};
232 confess "You cannot have lazy attribute ($name) without specifying a default value for it"
233 if $args{lazy} && !exists($args{default}) && !exists($args{builder});
235 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
236 if ref($args{default})
237 && ref($args{default}) ne 'CODE';
239 confess "You cannot auto-dereference without specifying a type constraint on attribute $name"
240 if $args{auto_deref} && !exists($args{isa});
242 confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute $name"
244 && $args{isa} ne 'ArrayRef'
245 && $args{isa} ne 'HashRef';
250 sub find_type_constraint {
252 my $type = $self->type_constraint;
256 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
257 return $checker if $checker;
259 return sub { blessed($_) && blessed($_) eq $type };
262 sub verify_type_constraint {
266 my $type = $self->type_constraint
268 my $constraint = $self->find_type_constraint;
270 return 1 if $constraint->($_);
272 my $name = $self->name;
273 my $display = defined($_) ? overload::StrVal($_) : 'undef';
274 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
277 sub _canonicalize_handles {
281 if (ref($handles) eq 'HASH') {
284 elsif (ref($handles) eq 'ARRAY') {
285 return map { $_ => $_ } @$handles;
288 confess "Unable to canonicalize the 'handles' option with $handles";
296 my %args = ($self->get_parent_args($class, $name), @_);
298 $self->create($class, $name, %args);
301 sub get_parent_args {
306 for my $super ($class->meta->linearized_isa) {
307 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
309 return %{ $super_attr->_create_args };
312 confess "Could not find an attribute by the name of '$name' to inherit from";
321 Mouse::Meta::Attribute - attribute metaclass
325 =head2 new %args -> Mouse::Meta::Attribute
327 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
329 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
331 Creates a new attribute in OwnerClass. Accessors and helper methods are
332 installed. Some error checking is done.
334 =head2 name -> AttributeName
336 =head2 class -> OwnerClass
338 =head2 is_required -> Bool
340 =head2 default -> Item
342 =head2 has_default -> Bool
344 =head2 is_lazy -> Bool
346 =head2 predicate -> MethodName | Undef
348 =head2 has_predicate -> Bool
350 =head2 clearer -> MethodName | Undef
352 =head2 has_clearer -> Bool
354 =head2 handles -> { LocalName => RemoteName }
356 =head2 has_handles -> Bool
358 =head2 is_weak_ref -> Bool
360 =head2 init_arg -> Str
362 =head2 type_constraint -> Str
364 =head2 has_type_constraint -> Bool
366 =head2 trigger => CODE | Undef
368 =head2 has_trigger -> Bool
370 =head2 builder => MethodName | Undef
372 =head2 has_builder -> Bool
374 =head2 is_lazy_build => Bool
376 =head2 should_auto_deref -> Bool
378 Informational methods.
380 =head2 generate_accessor -> CODE
382 Creates a new code reference for the attribute's accessor.
384 =head2 generate_predicate -> CODE
386 Creates a new code reference for the attribute's predicate.
388 =head2 generate_clearer -> CODE
390 Creates a new code reference for the attribute's clearer.
392 =head2 generate_handles -> { MethodName => CODE }
394 Creates a new code reference for each of the attribute's handles methods.
396 =head2 find_type_constraint -> CODE
398 Returns a code reference which can be used to check that a given value passes
399 this attribute's type constraint;
401 =head2 verify_type_constraint Item -> 1 | ERROR
403 Checks that the given value passes this attribute's type constraint. Returns 1
404 on success, otherwise C<confess>es.
406 =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
408 Creates a new attribute in OwnerClass, inheriting options from parent classes.
409 Accessors and helper methods are installed. Some error checking is done.
411 =head2 get_parent_args OwnerClass, AttributeName -> Hash
413 Returns the options that the parent class of C<OwnerClass> used for attribute