1 package Mouse::Meta::Attribute;
2 use Mouse::Util qw(:meta); # enables strict and warnings
6 use Mouse::Meta::TypeConstraint;
13 my $args = $class->Mouse::Object::BUILDARGS(@_);
15 # XXX: for backward compatibility (with method modifiers)
16 if($class->can('canonicalize_args') != \&canonicalize_args){
17 %{$args} = $class->canonicalize_args($name, %{$args});
20 $class->_process_options($name, $args);
22 $args->{name} = $name;
24 my $self = bless $args, $class;
27 if($class ne __PACKAGE__){
28 $class->meta->_initialize_object($self, $args);
31 # XXX: there is no fast way to check attribute validity
35 # Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad");
41 sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
42 sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
44 sub _create_args { # DEPRECATED
45 $_[0]->{_create_args} = $_[1] if @_ > 1;
49 sub interpolate_class{
50 my($class, $args) = @_;
52 if(my $metaclass = delete $args->{metaclass}){
53 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
57 if(my $traits_ref = delete $args->{traits}){
59 for (my $i = 0; $i < @{$traits_ref}; $i++) {
60 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
62 next if $class->does($trait);
67 push @traits, $traits_ref->[++$i]
68 if ref($traits_ref->[$i+1]);
72 $class = Mouse::Meta::Class->create_anon_class(
73 superclasses => [ $class ],
80 return( $class, @traits );
83 sub canonicalize_args{ # DEPRECATED
84 my ($self, $name, %args) = @_;
86 Carp::cluck("$self->canonicalize_args has been deprecated."
87 . "Use \$self->_process_options instead.");
92 sub create { # DEPRECATED
93 my ($self, $class, $name, %args) = @_;
95 Carp::cluck("$self->create has been deprecated."
96 . "Use \$meta->add_attribute and \$attr->install_accessors instead.");
102 sub _coerce_and_verify {
103 my($self, $value, $instance) = @_;
105 my $type_constraint = $self->{type_constraint};
106 return $value if !defined $type_constraint;
108 if ($self->should_coerce && $type_constraint->has_coercion) {
109 $value = $type_constraint->coerce($value);
112 $self->verify_against_type_constraint($value);
117 sub verify_against_type_constraint {
118 my ($self, $value) = @_;
120 my $type_constraint = $self->{type_constraint};
121 return 1 if !$type_constraint;
122 return 1 if $type_constraint->check($value);
124 $self->_throw_type_constraint_error($value, $type_constraint);
127 sub _throw_type_constraint_error {
128 my($self, $value, $type) = @_;
131 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
133 $type->get_message($value),
137 sub clone_and_inherit_options{
139 my $args = $self->Mouse::Object::BUILDARGS(@_);
141 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
143 $args->{traits} = \@traits if @traits;
144 # do not inherit the 'handles' attribute
145 foreach my $name(keys %{$self}){
146 if(!exists $args->{$name} && $name ne 'handles'){
147 $args->{$name} = $self->{$name};
150 return $attribute_class->new($self->name, $args);
153 sub clone_parent { # DEPRECATED
157 my %args = ($self->get_parent_args($class, $name), @_);
159 Carp::cluck("$self->clone_parent has been deprecated."
160 . "Use \$meta->add_attribute and \$attr->install_accessors instead.");
162 $self->clone_and_inherited_args($class, $name, %args);
165 sub get_parent_args { # DEPRECATED
170 for my $super ($class->linearized_isa) {
171 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
173 return %{ $super_attr->_create_args };
176 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
180 sub get_read_method {
181 return $_[0]->reader || $_[0]->accessor
183 sub get_write_method {
184 return $_[0]->writer || $_[0]->accessor
187 sub _get_accessor_method_ref {
188 my($self, $type, $generator) = @_;
190 my $metaclass = $self->associated_class
191 || $self->throw_error('No asocciated class for ' . $self->name);
193 my $accessor = $self->$type();
195 return $metaclass->get_method_body($accessor);
198 return $self->accessor_metaclass->$generator($self, $metaclass);
202 sub get_read_method_ref{
204 return $self->{_read_method_ref} ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
207 sub get_write_method_ref{
209 return $self->{_write_method_ref} ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
213 my($self, $object, $value) = @_;
214 return $self->get_write_method_ref()->($object, $value);
218 my($self, $object) = @_;
219 return $self->get_read_method_ref()->($object);
223 my($self, $object) = @_;
224 my $accessor_ref = $self->{_predicate_ref}
225 ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
227 return $accessor_ref->($object);
231 my($self, $object) = @_;
232 my $accessor_ref = $self->{_crealer_ref}
233 ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
235 return $accessor_ref->($object);
239 sub associate_method{
240 my ($attribute, $method_name) = @_;
241 $attribute->{associated_methods}++;
245 sub install_accessors{
248 my $metaclass = $attribute->associated_class;
249 my $accessor_class = $attribute->accessor_metaclass;
251 foreach my $type(qw(accessor reader writer predicate clearer)){
252 if(exists $attribute->{$type}){
253 my $generator = '_generate_' . $type;
254 my $code = $accessor_class->$generator($attribute, $metaclass);
255 $metaclass->add_method($attribute->{$type} => $code);
256 $attribute->associate_method($attribute->{$type});
261 if(exists $attribute->{handles}){
262 my %handles = $attribute->_canonicalize_handles($attribute->{handles});
264 while(my($handle, $method_to_call) = each %handles){
265 $metaclass->add_method($handle =>
266 $attribute->_make_delegation_method(
267 $handle, $method_to_call));
269 $attribute->associate_method($handle);
273 if($attribute->can('create') != \&create){
274 # backword compatibility
275 $attribute->create($metaclass, $attribute->name, %{$attribute});
281 sub delegation_metaclass() { 'Mouse::Meta::Method::Delegation' }
283 sub _canonicalize_handles {
284 my($self, $handles) = @_;
286 if (ref($handles) eq 'HASH') {
289 elsif (ref($handles) eq 'ARRAY') {
290 return map { $_ => $_ } @$handles;
292 elsif ( ref($handles) eq 'CODE' ) {
293 my $class_or_role = ( $self->{isa} || $self->{does} )
294 || $self->throw_error( "Cannot find delegate metaclass for attribute " . $self->name );
295 return $handles->( $self, Mouse::Meta::Class->initialize("$class_or_role"));
297 elsif (ref($handles) eq 'Regexp') {
298 my $class_or_role = ($self->{isa} || $self->{does})
299 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)");
301 my $meta = Mouse::Meta::Class->initialize("$class_or_role"); # "" for stringify
302 return map { $_ => $_ }
303 grep { !Mouse::Object->can($_) && $_ =~ $handles }
304 Mouse::Util::is_a_metarole($meta)
305 ? $meta->get_method_list
306 : $meta->get_all_method_names;
309 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
313 sub _make_delegation_method {
314 my($self, $handle, $method_to_call) = @_;
315 my $delegator = $self->delegation_metaclass;
316 Mouse::Util::load_class($delegator);
318 return $delegator->_generate_delegation($self, $handle, $method_to_call);
324 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
325 $metaclass->throw_error(@_, depth => 1);
333 Mouse::Meta::Attribute - The Mouse attribute metaclass
337 This document describes Mouse version 0.47
341 =head2 C<< new(%options) -> Mouse::Meta::Attribute >>
343 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
345 It adds the following options to the constructor:
349 =item C<< is => 'ro', 'rw', 'bare' >>
351 This provides a shorthand for specifying the C<reader>, C<writer>, or
352 C<accessor> names. If the attribute is read-only ('ro') then it will
353 have a C<reader> method with the same attribute as the name.
355 If it is read-write ('rw') then it will have an C<accessor> method
356 with the same name. If you provide an explicit C<writer> for a
357 read-write attribute, then you will have a C<reader> with the same
358 name as the attribute, and a C<writer> with the name you provided.
360 Use 'bare' when you are deliberately not installing any methods
361 (accessor, reader, etc.) associated with this attribute; otherwise,
362 Moose will issue a deprecation warning when this attribute is added to a
365 =item C<< isa => Type >>
367 This option accepts a type. The type can be a string, which should be
368 a type name. If the type name is unknown, it is assumed to be a class
371 This option can also accept a L<Moose::Meta::TypeConstraint> object.
373 If you I<also> provide a C<does> option, then your C<isa> option must
374 be a class name, and that class must do the role specified with
377 =item C<< does => Role >>
379 This is short-hand for saying that the attribute's type must be an
380 object which does the named role.
382 B<This option is not yet supported.>
384 =item C<< coerce => Bool >>
386 This option is only valid for objects with a type constraint
387 (C<isa>). If this is true, then coercions will be applied whenever
388 this attribute is set.
390 You can make both this and the C<weak_ref> option true.
392 =item C<< trigger => CodeRef >>
394 This option accepts a subroutine reference, which will be called after
395 the attribute is set.
397 =item C<< required => Bool >>
399 An attribute which is required must be provided to the constructor. An
400 attribute which is required can also have a C<default> or C<builder>,
401 which will satisfy its required-ness.
403 A required attribute must have a C<default>, C<builder> or a
404 non-C<undef> C<init_arg>
406 =item C<< lazy => Bool >>
408 A lazy attribute must have a C<default> or C<builder>. When an
409 attribute is lazy, the default value will not be calculated until the
412 =item C<< weak_ref => Bool >>
414 If this is true, the attribute's value will be stored as a weak
417 =item C<< auto_deref => Bool >>
419 If this is true, then the reader will dereference the value when it is
420 called. The attribute must have a type constraint which defines the
421 attribute as an array or hash reference.
423 =item C<< lazy_build => Bool >>
425 Setting this to true makes the attribute lazy and provides a number of
433 is equivalent to this:
438 builder => '_build_size',
439 clearer => 'clear_size',
440 predicate => 'has_size',
445 =head2 C<< associate_method(MethodName) >>
447 Associates a method with the attribute. Typically, this is called internally
448 when an attribute generates its accessors.
450 Currently the argument I<MethodName> is ignored in Mouse.
452 =head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >>
454 Checks that the given value passes this attribute's type constraint. Returns C<true>
455 on success, otherwise C<confess>es.
457 =head2 C<< clone_and_inherit_options(options) -> Mouse::Meta::Attribute >>
459 Creates a new attribute in the owner class, inheriting options from parent classes.
460 Accessors and helper methods are installed. Some error checking is done.
462 =head2 C<< get_read_method_ref >>
464 =head2 C<< get_write_method_ref >>
466 Returns the subroutine reference of a method suitable for reading or
467 writing the attribute's value in the associated class. These methods
468 always return a subroutine reference, regardless of whether or not the
469 attribute is read- or write-only.
473 L<Moose::Meta::Attribute>
475 L<Class::MOP::Attribute>