| Moose::Meta::Attribute - The Moose attribute metaclass |
Moose::Meta::Attribute - The Moose attribute metaclass
This class is a subclass of the Class::MOP::Attribute manpage that provides additional Moose-specific functionality.
To really understand this class, you will need to start with the the Class::MOP::Attribute manpage documentation. This class can be understood as a set of additional features on top of the basic feature provided by that parent class.
Moose::Meta::Attribute is a subclass of the Class::MOP::Attribute manpage.
Many of the documented below override methods in the Class::MOP::Attribute manpage and add Moose specific features.
new(%options) >>
This method overrides the the Class::MOP::Attribute manpage constructor.
Many of the options below are described in more detail in the the Moose::Manual::Attributes manpage document.
It adds the following options to the constructor:
This provides a shorthand for specifying the reader, writer, or
accessor names. If the attribute is read-only ('ro') then it will
have a reader method with the same attribute as the name.
If it is read-write ('rw') then it will have an accessor method
with the same name. If you provide an explicit writer for a
read-write attribute, then you will have a reader with the same
name as the attribute, and a writer with the name you provided.
This option accepts a type. The type can be a string, which should be a type name. If the type name is unknown, it is assumed to be a class name.
This option can also accept a the Moose::Meta::TypeConstraint manpage object.
If you also provide a does option, then your isa option must
be a class name, and that class must do the role specified with
does.
This is short-hand for saying that the attribute's type must be an object which does the named role.
This option is only valid for objects with a type constraint
(isa). If this is true, then coercions will be applied whenever
this attribute is set.
You can make both this and the weak_ref option true.
This option accepts a subroutine reference, which will be called after the attribute is set.
An attribute which is required must be provided to the constructor. An
attribute which is required can also have a default or builder,
which will satisfy its required-ness.
A required attribute must have a default, builder or a
non-undef init_arg
A lazy attribute must have a default or builder. When an
attribute is lazy, the default value will not be calculated until the
attribute is read.
If this is true, the attribute's value will be stored as a weak reference.
If this is true, then the reader will dereference the value when it is called. The attribute must have a type constraint which defines the attribute as an array or hash reference.
Setting this to true makes the attribute lazy and provides a number of default methods.
has 'size' => ( is => 'ro', lazy_build => 1, );
is equivalent to this:
has 'size' => ( is => 'ro', lazy => 1, builder => '_build_size', clearer => 'clear_size', predicate => 'has_size', );
An arbitrary string that can be retrieved later by calling <
$attr-documentation >>.
clone(%options) >>
This creates a new attribute based on attribute being cloned. You must
supply a name option to provide a new name for the attribute.
The %options can only specify options handled by
the Class::MOP::Attribute manpage.
This method is used internally to initialize the attribute's slot in
the object $instance.
This overrides the the Class::MOP::Attribute manpage method to handle lazy attributes, weak references, and type constraints.
eval { $point->meta->get_attribute('x')->set_value($point, 'forty-two') }; if($@) { print "Oops: $@\n"; }
Attribute (x) does not pass the type constraint (Int) with 'forty-two'
Before setting the value, a check is made on the type constraint of the attribute, if it has one, to see if the value passes it. If the value fails to pass, the set operation dies with a throw_error.
Any coercion to convert values is done before checking the type constraint.
To check a value against a type constraint before setting it, fetch the attribute instance using find_attribute_by_name in the Class::MOP::Class manpage, fetch the type_constraint from the attribute using type_constraint in the Moose::Meta::Attribute manpage and call check in the Moose::Meta::TypeConstraint manpage. See the Moose::Cookbook::Basics::Recipe4 manpage for an example.
This method overrides the parent to also install delegation methods.
This method overrides the parent to also remove delegation methods.
This method adds its delegation methods to the attribute's associated class, if it has any to add.
This method remove its delegation methods from the attribute's associated class.
Returns the accessor metaclass name, which defaults to the Moose::Meta::Method::Accessor manpage.
Returns the delegation metaclass name, which defaults to the Moose::Meta::Method::Delegation manpage.
These methods are not found in the superclass. They support features provided by Moose.
does($role) >>
This indicates whether the attribute itself does the given role. The role can be given as a full class name, or as a resolvable trait name.
Note that this checks the attribute itself, not its type constraint, so it is checking the attribute's metaclass and any traits applied to the attribute.
This is an alternate constructor that handles the metaclass and
traits options.
Effectively, this method is a factory that finds or creates the
appropriate class for the given metaclass and/or traits.
Once it has the appropriate class, it will call $class->new($name,
%options) on that class.
clone_and_inherit_options(%options) >>
This method supports the has '+foo' feature. It does various bits
of processing on the supplied %options before ultimately calling
the clone method.
One of its main tasks is to make sure that the %options provided
only includes the options returned by the
legal_options_for_inheritance method.
This returns a whitelist of options that can be overridden in a subclass's attribute definition.
This exists to allow a custom metaclass to change or add to the list of options which can be changed.
Returns the the Moose::Meta::TypeConstraint manpage object for this attribute, if it has one.
Returns true if this attribute has a type constraint.
verify_against_type_constraint($value) >>
Given a value, this method returns true if the value is valid for the attribute's type constraint. If the value is not valid, it throws an error.
This returns the value of the handles option passed to the
constructor.
Returns true if this attribute performs delegation.
Returns true if this attribute stores its value as a weak reference.
Returns true if this attribute is required to have a value.
Returns true if this attribute is lazy.
Returns true if the lazy_build option was true when passed to the
constructor.
Returns true if the coerce option passed to the constructor was
true.
Returns true if the auto_deref option passed to the constructor was
true.
This is the subroutine reference that was in the trigger option
passed to the constructor, if any.
Returns true if this attribute has a trigger set.
Returns the value that was in the documentation option passed to
the constructor, if any.
Returns true if this attribute has any documentation.
This returns an array reference of all the traits which were applied
to this attribute. If none were applied, this returns undef.
Returns true if this attribute has any traits applied.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
Copyright 2006-2009 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Moose::Meta::Attribute - The Moose attribute metaclass |