1 package Mouse::Meta::Attribute;
2 use Mouse::Util qw(:meta); # enables strict and warnings
6 use Mouse::Meta::TypeConstraint;
8 my %valid_options = map { $_ => undef } (
39 # Moose defines, but Mouse doesn't
40 #'definition_context',
43 # special case for AttributeHelpers
48 our @CARP_NOT = qw(Mouse::Meta::Class);
54 my $args = $class->Mouse::Object::BUILDARGS(@_);
56 $class->_process_options($name, $args);
58 $args->{name} = $name;
62 my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
64 # (2) known by subclasses
65 if(@bad && $class ne __PACKAGE__){
69 map { $_->init_arg() }
70 $class->meta->get_all_attributes()
72 @bad = grep{ !exists $valid_attrs{$_} } @bad;
75 # (3) bad options found
78 "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
79 . Mouse::Util::english_list(@bad));
82 my $self = bless $args, $class;
83 if($class ne __PACKAGE__){
84 $class->meta->_initialize_object($self, $args);
89 sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
90 sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
92 sub get_read_method { $_[0]->reader || $_[0]->accessor }
93 sub get_write_method { $_[0]->writer || $_[0]->accessor }
95 sub get_read_method_ref{
97 return $self->{_mouse_cache_read_method_ref}
98 ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
101 sub get_write_method_ref{
103 return $self->{_mouse_cache_write_method_ref}
104 ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
107 sub interpolate_class{
108 my($class, $args) = @_;
110 if(my $metaclass = delete $args->{metaclass}){
111 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
115 if(my $traits_ref = delete $args->{traits}){
117 for (my $i = 0; $i < @{$traits_ref}; $i++) {
118 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
120 next if $class->does($trait);
122 push @traits, $trait;
125 push @traits, $traits_ref->[++$i]
126 if ref($traits_ref->[$i+1]);
130 $class = Mouse::Meta::Class->create_anon_class(
131 superclasses => [ $class ],
138 return( $class, @traits );
141 sub verify_against_type_constraint {
142 my ($self, $value) = @_;
144 my $type_constraint = $self->{type_constraint};
145 return 1 if !$type_constraint;
146 return 1 if $type_constraint->check($value);
148 $self->_throw_type_constraint_error($value, $type_constraint);
151 sub _throw_type_constraint_error {
152 my($self, $value, $type) = @_;
155 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
157 $type->get_message($value),
161 sub illegal_options_for_inheritance {
162 return qw(reader writer accessor clearer predicate);
165 sub clone_and_inherit_options{
167 my $args = $self->Mouse::Object::BUILDARGS(@_);
169 foreach my $illegal($self->illegal_options_for_inheritance) {
170 if(exists $args->{$illegal} and exists $self->{$illegal}) {
171 $self->throw_error("Illegal inherited option: $illegal");
175 foreach my $name(keys %{$self}){
176 if(!exists $args->{$name}){
177 $args->{$name} = $self->{$name}; # inherit from self
181 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
182 $args->{traits} = \@traits if @traits;
184 # remove temporary caches
185 foreach my $attr(keys %{$args}){
186 if($attr =~ /\A _mouse_cache_/xms){
187 delete $args->{$attr};
191 # remove default if lazy_build => 1
192 if($args->{lazy_build}) {
193 delete $args->{default};
196 return $attribute_class->new($self->name, $args);
200 sub _get_accessor_method_ref {
201 my($self, $type, $generator) = @_;
203 my $metaclass = $self->associated_class
204 || $self->throw_error('No asocciated class for ' . $self->name);
206 my $accessor = $self->$type();
208 return $metaclass->get_method_body($accessor);
211 return $self->accessor_metaclass->$generator($self, $metaclass);
216 my($self, $object, $value) = @_;
217 return $self->get_write_method_ref()->($object, $value);
221 my($self, $object) = @_;
222 return $self->get_read_method_ref()->($object);
226 my($self, $object) = @_;
227 my $accessor_ref = $self->{_mouse_cache_predicate_ref}
228 ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
230 return $accessor_ref->($object);
234 my($self, $object) = @_;
235 my $accessor_ref = $self->{_mouse_cache_crealer_ref}
236 ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
238 return $accessor_ref->($object);
241 sub associate_method{
242 #my($attribute, $method_name) = @_;
244 $attribute->{associated_methods}++;
248 sub install_accessors{
251 my $metaclass = $attribute->associated_class;
252 my $accessor_class = $attribute->accessor_metaclass;
254 foreach my $type(qw(accessor reader writer predicate clearer)){
255 if(exists $attribute->{$type}){
256 my $generator = '_generate_' . $type;
257 my $code = $accessor_class->$generator($attribute, $metaclass);
258 $metaclass->add_method($attribute->{$type} => $code);
259 $attribute->associate_method($attribute->{$type});
264 if(exists $attribute->{handles}){
265 my %handles = $attribute->_canonicalize_handles();
266 while(my($handle, $method_to_call) = each %handles){
267 next if Mouse::Object->can($handle);
269 if($metaclass->has_method($handle)) {
270 $attribute->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation");
273 $metaclass->add_method($handle =>
274 $attribute->_make_delegation_method(
275 $handle, $method_to_call));
277 $attribute->associate_method($handle);
284 sub delegation_metaclass() { ## no critic
285 'Mouse::Meta::Method::Delegation'
288 sub _canonicalize_handles {
290 my $handles = $self->{handles};
292 my $handle_type = ref $handles;
293 if ($handle_type eq 'HASH') {
296 elsif ($handle_type eq 'ARRAY') {
297 return map { $_ => $_ } @$handles;
299 elsif ($handle_type eq 'Regexp') {
300 my $meta = $self->_find_delegate_metaclass();
301 return map { $_ => $_ }
303 Mouse::Util::is_a_metarole($meta)
304 ? $meta->get_method_list
305 : $meta->get_all_method_names;
307 elsif ($handle_type eq 'CODE') {
308 return $handles->( $self, $self->_find_delegate_metaclass() );
311 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
315 sub _find_delegate_metaclass {
319 $meta = Mouse::Meta::Class->initialize("$self->{isa}");
321 elsif($self->{does}) {
322 $meta = Mouse::Util::get_metaclass_by_name("$self->{does}");
324 defined($meta) or $self->throw_error(
325 "Cannot find delegate metaclass for attribute " . $self->name);
330 sub _make_delegation_method {
331 my($self, $handle, $method_to_call) = @_;
332 return Mouse::Util::load_class($self->delegation_metaclass)
333 ->_generate_delegation($self, $handle, $method_to_call);
341 Mouse::Meta::Attribute - The Mouse attribute metaclass
345 This document describes Mouse version 0.87
349 This is a meta object protocol for Mouse attributes,
350 which is a subset of Moose::Meta::Attribute.
354 =head2 C<< new(%options) -> Mouse::Meta::Attribute >>
356 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
358 It adds the following options to the constructor:
362 =item C<< is => 'ro', 'rw', 'bare' >>
364 This provides a shorthand for specifying the C<reader>, C<writer>, or
365 C<accessor> names. If the attribute is read-only ('ro') then it will
366 have a C<reader> method with the same attribute as the name.
368 If it is read-write ('rw') then it will have an C<accessor> method
369 with the same name. If you provide an explicit C<writer> for a
370 read-write attribute, then you will have a C<reader> with the same
371 name as the attribute, and a C<writer> with the name you provided.
373 Use 'bare' when you are deliberately not installing any methods
374 (accessor, reader, etc.) associated with this attribute; otherwise,
375 Moose will issue a deprecation warning when this attribute is added to a
378 =item C<< isa => Type >>
380 This option accepts a type. The type can be a string, which should be
381 a type name. If the type name is unknown, it is assumed to be a class
384 This option can also accept a L<Moose::Meta::TypeConstraint> object.
386 If you I<also> provide a C<does> option, then your C<isa> option must
387 be a class name, and that class must do the role specified with
390 =item C<< does => Role >>
392 This is short-hand for saying that the attribute's type must be an
393 object which does the named role.
395 B<This option is not yet supported.>
397 =item C<< coerce => Bool >>
399 This option is only valid for objects with a type constraint
400 (C<isa>). If this is true, then coercions will be applied whenever
401 this attribute is set.
403 You can make both this and the C<weak_ref> option true.
405 =item C<< trigger => CodeRef >>
407 This option accepts a subroutine reference, which will be called after
408 the attribute is set.
410 =item C<< required => Bool >>
412 An attribute which is required must be provided to the constructor. An
413 attribute which is required can also have a C<default> or C<builder>,
414 which will satisfy its required-ness.
416 A required attribute must have a C<default>, C<builder> or a
417 non-C<undef> C<init_arg>
419 =item C<< lazy => Bool >>
421 A lazy attribute must have a C<default> or C<builder>. When an
422 attribute is lazy, the default value will not be calculated until the
425 =item C<< weak_ref => Bool >>
427 If this is true, the attribute's value will be stored as a weak
430 =item C<< auto_deref => Bool >>
432 If this is true, then the reader will dereference the value when it is
433 called. The attribute must have a type constraint which defines the
434 attribute as an array or hash reference.
436 =item C<< lazy_build => Bool >>
438 Setting this to true makes the attribute lazy and provides a number of
446 is equivalent to this:
451 builder => '_build_size',
452 clearer => 'clear_size',
453 predicate => 'has_size',
458 =head2 C<< associate_method(MethodName) >>
460 Associates a method with the attribute. Typically, this is called internally
461 when an attribute generates its accessors.
463 Currently the argument I<MethodName> is ignored in Mouse.
465 =head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >>
467 Checks that the given value passes this attribute's type constraint. Returns C<true>
468 on success, otherwise C<confess>es.
470 =head2 C<< clone_and_inherit_options(options) -> Mouse::Meta::Attribute >>
472 Creates a new attribute in the owner class, inheriting options from parent classes.
473 Accessors and helper methods are installed. Some error checking is done.
475 =head2 C<< get_read_method_ref >>
477 =head2 C<< get_write_method_ref >>
479 Returns the subroutine reference of a method suitable for reading or
480 writing the attribute's value in the associated class. These methods
481 always return a subroutine reference, regardless of whether or not the
482 attribute is read- or write-only.
486 L<Moose::Meta::Attribute>
488 L<Class::MOP::Attribute>