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 } (
38 # Moose defines, but Mouse doesn't
39 #'definition_context',
42 # special case for AttributeHelpers
47 our @CARP_NOT = qw(Mouse::Meta::Class);
53 my $args = $class->Mouse::Object::BUILDARGS(@_);
55 $class->_process_options($name, $args);
57 $args->{name} = $name;
61 my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
63 # (2) known by subclasses
64 if(@bad && $class ne __PACKAGE__){
68 map { $_->init_arg() }
69 $class->meta->get_all_attributes()
71 @bad = grep{ !exists $valid_attrs{$_} } @bad;
74 # (3) bad options found
77 "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
78 . Mouse::Util::english_list(@bad));
81 my $self = bless $args, $class;
84 if($class ne __PACKAGE__){
85 $class->meta->_initialize_object($self, $args);
91 sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
92 sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
94 sub interpolate_class{
95 my($class, $args) = @_;
97 if(my $metaclass = delete $args->{metaclass}){
98 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
102 if(my $traits_ref = delete $args->{traits}){
104 for (my $i = 0; $i < @{$traits_ref}; $i++) {
105 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
107 next if $class->does($trait);
109 push @traits, $trait;
112 push @traits, $traits_ref->[++$i]
113 if ref($traits_ref->[$i+1]);
117 $class = Mouse::Meta::Class->create_anon_class(
118 superclasses => [ $class ],
125 return( $class, @traits );
128 sub _coerce_and_verify {
129 #my($self, $value, $instance) = @_;
130 my($self, $value) = @_;
132 my $type_constraint = $self->{type_constraint};
133 return $value if !defined $type_constraint;
135 if ($self->should_coerce && $type_constraint->has_coercion) {
136 $value = $type_constraint->coerce($value);
139 $self->verify_against_type_constraint($value);
144 sub verify_against_type_constraint {
145 my ($self, $value) = @_;
147 my $type_constraint = $self->{type_constraint};
148 return 1 if !$type_constraint;
149 return 1 if $type_constraint->check($value);
151 $self->_throw_type_constraint_error($value, $type_constraint);
154 sub _throw_type_constraint_error {
155 my($self, $value, $type) = @_;
158 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
160 $type->get_message($value),
164 sub illegal_options_for_inheritance {
165 return qw(reader writer accessor clearer predicate);
168 sub clone_and_inherit_options{
170 my $args = $self->Mouse::Object::BUILDARGS(@_);
172 foreach my $illegal($self->illegal_options_for_inheritance) {
173 if(exists $args->{$illegal} and exists $self->{$illegal}) {
174 $self->throw_error("Illegal inherited option: $illegal");
178 foreach my $name(keys %{$self}){
179 if(!exists $args->{$name}){
180 $args->{$name} = $self->{$name}; # inherit from self
184 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
185 $args->{traits} = \@traits if @traits;
187 # remove temporary caches
188 foreach my $attr(keys %{$args}){
189 if($attr =~ /\A _/xms){
190 delete $args->{$attr};
194 # remove default if lazy_build => 1
195 if($args->{lazy_build}) {
196 delete $args->{default};
199 return $attribute_class->new($self->name, $args);
202 sub get_read_method {
203 return $_[0]->reader || $_[0]->accessor
205 sub get_write_method {
206 return $_[0]->writer || $_[0]->accessor
209 sub _get_accessor_method_ref {
210 my($self, $type, $generator) = @_;
212 my $metaclass = $self->associated_class
213 || $self->throw_error('No asocciated class for ' . $self->name);
215 my $accessor = $self->$type();
217 return $metaclass->get_method_body($accessor);
220 return $self->accessor_metaclass->$generator($self, $metaclass);
224 sub get_read_method_ref{
226 return $self->{_read_method_ref} ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
229 sub get_write_method_ref{
231 return $self->{_write_method_ref} ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
235 my($self, $object, $value) = @_;
236 return $self->get_write_method_ref()->($object, $value);
240 my($self, $object) = @_;
241 return $self->get_read_method_ref()->($object);
245 my($self, $object) = @_;
246 my $accessor_ref = $self->{_predicate_ref}
247 ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
249 return $accessor_ref->($object);
253 my($self, $object) = @_;
254 my $accessor_ref = $self->{_crealer_ref}
255 ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
257 return $accessor_ref->($object);
261 sub associate_method{
262 #my($attribute, $method_name) = @_;
264 $attribute->{associated_methods}++;
268 sub install_accessors{
271 my $metaclass = $attribute->associated_class;
272 my $accessor_class = $attribute->accessor_metaclass;
274 foreach my $type(qw(accessor reader writer predicate clearer)){
275 if(exists $attribute->{$type}){
276 my $generator = '_generate_' . $type;
277 my $code = $accessor_class->$generator($attribute, $metaclass);
278 $metaclass->add_method($attribute->{$type} => $code);
279 $attribute->associate_method($attribute->{$type});
284 if(exists $attribute->{handles}){
285 my %handles = $attribute->_canonicalize_handles($attribute->{handles});
287 while(my($handle, $method_to_call) = each %handles){
288 if($metaclass->has_method($handle)) {
289 $attribute->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation");
292 $metaclass->add_method($handle =>
293 $attribute->_make_delegation_method(
294 $handle, $method_to_call));
296 $attribute->associate_method($handle);
303 sub delegation_metaclass() { ## no critic
304 'Mouse::Meta::Method::Delegation'
307 sub _canonicalize_handles {
308 my($self, $handles) = @_;
310 if (ref($handles) eq 'HASH') {
313 elsif (ref($handles) eq 'ARRAY') {
314 return map { $_ => $_ } @$handles;
316 elsif ( ref($handles) eq 'CODE' ) {
317 my $class_or_role = ( $self->{isa} || $self->{does} )
318 || $self->throw_error( "Cannot find delegate metaclass for attribute " . $self->name );
319 return $handles->( $self, Mouse::Meta::Class->initialize("$class_or_role"));
321 elsif (ref($handles) eq 'Regexp') {
322 my $class_or_role = ($self->{isa} || $self->{does})
323 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)");
325 my $meta = Mouse::Meta::Class->initialize("$class_or_role"); # "" for stringify
326 return map { $_ => $_ }
327 grep { !Mouse::Object->can($_) && $_ =~ $handles }
328 Mouse::Util::is_a_metarole($meta)
329 ? $meta->get_method_list
330 : $meta->get_all_method_names;
333 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
337 sub _make_delegation_method {
338 my($self, $handle, $method_to_call) = @_;
339 return Mouse::Util::load_class($self->delegation_metaclass)
340 ->_generate_delegation($self, $handle, $method_to_call);
346 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
347 $metaclass->throw_error(@_, depth => 1);
355 Mouse::Meta::Attribute - The Mouse attribute metaclass
359 This document describes Mouse version 0.70
363 This is a meta object protocol for Mouse attributes,
364 which is a subset of Moose::Meta::Attribute.
368 =head2 C<< new(%options) -> Mouse::Meta::Attribute >>
370 Instantiates a new Mouse::Meta::Attribute. Does nothing else.
372 It adds the following options to the constructor:
376 =item C<< is => 'ro', 'rw', 'bare' >>
378 This provides a shorthand for specifying the C<reader>, C<writer>, or
379 C<accessor> names. If the attribute is read-only ('ro') then it will
380 have a C<reader> method with the same attribute as the name.
382 If it is read-write ('rw') then it will have an C<accessor> method
383 with the same name. If you provide an explicit C<writer> for a
384 read-write attribute, then you will have a C<reader> with the same
385 name as the attribute, and a C<writer> with the name you provided.
387 Use 'bare' when you are deliberately not installing any methods
388 (accessor, reader, etc.) associated with this attribute; otherwise,
389 Moose will issue a deprecation warning when this attribute is added to a
392 =item C<< isa => Type >>
394 This option accepts a type. The type can be a string, which should be
395 a type name. If the type name is unknown, it is assumed to be a class
398 This option can also accept a L<Moose::Meta::TypeConstraint> object.
400 If you I<also> provide a C<does> option, then your C<isa> option must
401 be a class name, and that class must do the role specified with
404 =item C<< does => Role >>
406 This is short-hand for saying that the attribute's type must be an
407 object which does the named role.
409 B<This option is not yet supported.>
411 =item C<< coerce => Bool >>
413 This option is only valid for objects with a type constraint
414 (C<isa>). If this is true, then coercions will be applied whenever
415 this attribute is set.
417 You can make both this and the C<weak_ref> option true.
419 =item C<< trigger => CodeRef >>
421 This option accepts a subroutine reference, which will be called after
422 the attribute is set.
424 =item C<< required => Bool >>
426 An attribute which is required must be provided to the constructor. An
427 attribute which is required can also have a C<default> or C<builder>,
428 which will satisfy its required-ness.
430 A required attribute must have a C<default>, C<builder> or a
431 non-C<undef> C<init_arg>
433 =item C<< lazy => Bool >>
435 A lazy attribute must have a C<default> or C<builder>. When an
436 attribute is lazy, the default value will not be calculated until the
439 =item C<< weak_ref => Bool >>
441 If this is true, the attribute's value will be stored as a weak
444 =item C<< auto_deref => Bool >>
446 If this is true, then the reader will dereference the value when it is
447 called. The attribute must have a type constraint which defines the
448 attribute as an array or hash reference.
450 =item C<< lazy_build => Bool >>
452 Setting this to true makes the attribute lazy and provides a number of
460 is equivalent to this:
465 builder => '_build_size',
466 clearer => 'clear_size',
467 predicate => 'has_size',
472 =head2 C<< associate_method(MethodName) >>
474 Associates a method with the attribute. Typically, this is called internally
475 when an attribute generates its accessors.
477 Currently the argument I<MethodName> is ignored in Mouse.
479 =head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >>
481 Checks that the given value passes this attribute's type constraint. Returns C<true>
482 on success, otherwise C<confess>es.
484 =head2 C<< clone_and_inherit_options(options) -> Mouse::Meta::Attribute >>
486 Creates a new attribute in the owner class, inheriting options from parent classes.
487 Accessors and helper methods are installed. Some error checking is done.
489 =head2 C<< get_read_method_ref >>
491 =head2 C<< get_write_method_ref >>
493 Returns the subroutine reference of a method suitable for reading or
494 writing the attribute's value in the associated class. These methods
495 always return a subroutine reference, regardless of whether or not the
496 attribute is read- or write-only.
500 L<Moose::Meta::Attribute>
502 L<Class::MOP::Attribute>