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}";
27 $args{clearer} ||= "_clear${name}";
28 $args{predicate} ||= "_has${name}";
31 $args{clearer} ||= "clear_${name}";
32 $args{predicate} ||= "has_${name}";
39 sub name { $_[0]->{name} }
40 sub class { $_[0]->{class} }
41 sub _is_metadata { $_[0]->{is} }
42 sub is_required { $_[0]->{required} }
43 sub default { $_[0]->{default} }
44 sub is_lazy { $_[0]->{lazy} }
45 sub is_lazy_build { $_[0]->{lazy_build} }
46 sub predicate { $_[0]->{predicate} }
47 sub clearer { $_[0]->{clearer} }
48 sub handles { $_[0]->{handles} }
49 sub is_weak_ref { $_[0]->{weak_ref} }
50 sub init_arg { $_[0]->{init_arg} }
51 sub type_constraint { $_[0]->{type_constraint} }
52 sub trigger { $_[0]->{trigger} }
53 sub builder { $_[0]->{builder} }
54 sub should_auto_deref { $_[0]->{auto_deref} }
56 sub has_default { exists $_[0]->{default} }
57 sub has_predicate { exists $_[0]->{predicate} }
58 sub has_clearer { exists $_[0]->{clearer} }
59 sub has_handles { exists $_[0]->{handles} }
60 sub has_type_constraint { exists $_[0]->{type_constraint} }
61 sub has_trigger { exists $_[0]->{trigger} }
62 sub has_builder { exists $_[0]->{builder} }
65 $_[0]->{_create_args} = $_[1] if @_ > 1;
69 sub generate_accessor {
70 my $attribute = shift;
72 my $name = $attribute->name;
74 my $default = $attribute->default;
75 my $trigger = $attribute->trigger;
76 my $type = $attribute->type_constraint;
77 my $constraint = $attribute->find_type_constraint;
78 my $builder = $attribute->builder;
83 if ($attribute->_is_metadata eq 'rw') {
84 $accessor .= 'if (@_) {
89 my $display = defined($_) ? overload::StrVal($_) : "undef";
90 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
94 $accessor .= '$self->{$key} = $_;';
96 if ($attribute->is_weak_ref) {
97 $accessor .= 'Scalar::Util::weaken($self->{$key}) if ref($self->{$key});';
101 $accessor .= '$trigger->($self, $_, $attribute);';
107 $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;';
110 if ($attribute->is_lazy) {
111 $accessor .= '$self->{$key} = ';
113 $accessor .= $attribute->has_builder
115 : ref($default) eq 'CODE'
116 ? '$default->($self)'
119 $accessor .= ' if !exists($self->{$key});';
122 if ($attribute->should_auto_deref) {
123 if ($attribute->type_constraint eq 'ArrayRef') {
124 $accessor .= 'if (wantarray) {
125 return @{ $self->{$key} || [] };
129 $accessor .= 'if (wantarray) {
130 return %{ $self->{$key} || {} };
135 $accessor .= 'return $self->{$key};
138 return eval $accessor;
141 sub generate_predicate {
142 my $attribute = shift;
143 my $key = $attribute->name;
145 my $predicate = 'sub { exists($_[0]->{$key}) }';
147 return eval $predicate;
150 sub generate_clearer {
151 my $attribute = shift;
152 my $key = $attribute->name;
154 my $predicate = 'sub { delete($_[0]->{$key}) }';
156 return eval $predicate;
159 sub generate_handles {
160 my $attribute = shift;
161 my $reader = $attribute->name;
162 my %handles = $attribute->_canonicalize_handles($attribute->handles);
166 for my $local_method (keys %handles) {
167 my $remote_method = $handles{$local_method};
171 $self->$reader->$remote_method(@_)
174 $method_map{$local_method} = eval $method;
181 my ($self, $class, $name, %args) = @_;
184 $args{class} = $class;
186 $self->validate_args($name, %args);
188 $args{type_constraint} = delete $args{isa}
189 if exists $args{isa};
191 my $attribute = $self->new(%args);
192 $attribute->_create_args(\%args);
194 my $meta = $class->meta;
196 $meta->add_attribute($attribute);
198 # install an accessor
199 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
200 my $accessor = $attribute->generate_accessor;
202 *{ $class . '::' . $name } = $accessor;
205 for my $method (qw/predicate clearer/) {
206 my $predicate = "has_$method";
207 if ($attribute->$predicate) {
208 my $generator = "generate_$method";
209 my $coderef = $attribute->$generator;
211 *{ $class . '::' . $attribute->$method } = $coderef;
215 if ($attribute->has_handles) {
216 my $method_map = $attribute->generate_handles;
217 for my $method_name (keys %$method_map) {
219 *{ $class . '::' . $method_name } = $method_map->{$method_name};
231 confess "You cannot have lazy attribute ($name) without specifying a default value for it"
232 if $args{lazy} && !exists($args{default}) && !exists($args{builder});
234 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
235 if ref($args{default})
236 && ref($args{default}) ne 'CODE';
238 confess "You cannot auto-dereference without specifying a type constraint on attribute $name"
239 if $args{auto_deref} && !exists($args{isa});
241 confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute $name"
243 && $args{isa} ne 'ArrayRef'
244 && $args{isa} ne 'HashRef';
249 sub find_type_constraint {
251 my $type = $self->type_constraint;
255 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
256 return $checker if $checker;
258 return sub { blessed($_) && blessed($_) eq $type };
261 sub verify_type_constraint {
265 my $type = $self->type_constraint
267 my $constraint = $self->find_type_constraint;
269 return 1 if $constraint->($_);
271 my $name = $self->name;
272 my $display = defined($_) ? overload::StrVal($_) : 'undef';
273 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display");
276 sub _canonicalize_handles {
280 if (ref($handles) eq 'HASH') {
283 elsif (ref($handles) eq 'ARRAY') {
284 return map { $_ => $_ } @$handles;
287 confess "Unable to canonicalize the 'handles' option with $handles";
295 my %args = ($self->get_parent_args($class, $name), @_);
297 $self->create($class, $name, %args);
300 sub get_parent_args {
305 for my $super ($class->meta->linearized_isa) {
306 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
308 return %{ $super_attr->_create_args };
311 confess "Could not find an attribute by the name of '$name' to inherit from";
320 Mouse::Meta::Attribute - attribute metaclass
324 =head2 new %args -> Mouse::Meta::Attribute
326 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
328 =head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
330 Creates a new attribute in OwnerClass. Accessors and helper methods are
331 installed. Some error checking is done.
333 =head2 name -> AttributeName
335 =head2 class -> OwnerClass
337 =head2 is_required -> Bool
339 =head2 default -> Item
341 =head2 has_default -> Bool
343 =head2 is_lazy -> Bool
345 =head2 predicate -> MethodName | Undef
347 =head2 has_predicate -> Bool
349 =head2 clearer -> MethodName | Undef
351 =head2 has_clearer -> Bool
353 =head2 handles -> { LocalName => RemoteName }
355 =head2 has_handles -> Bool
357 =head2 is_weak_ref -> Bool
359 =head2 init_arg -> Str
361 =head2 type_constraint -> Str
363 =head2 has_type_constraint -> Bool
365 =head2 trigger => CODE | Undef
367 =head2 has_trigger -> Bool
369 =head2 builder => MethodName | Undef
371 =head2 has_builder -> Bool
373 =head2 should_auto_deref -> Bool
375 Informational methods.
377 =head2 generate_accessor -> CODE
379 Creates a new code reference for the attribute's accessor.
381 =head2 generate_predicate -> CODE
383 Creates a new code reference for the attribute's predicate.
385 =head2 generate_clearer -> CODE
387 Creates a new code reference for the attribute's clearer.
389 =head2 generate_handles -> { MethodName => CODE }
391 Creates a new code reference for each of the attribute's handles methods.
393 =head2 find_type_constraint -> CODE
395 Returns a code reference which can be used to check that a given value passes
396 this attribute's type constraint;
398 =head2 verify_type_constraint Item -> 1 | ERROR
400 Checks that the given value passes this attribute's type constraint. Returns 1
401 on success, otherwise C<confess>es.