1 package Moose::Meta::Role::Attribute;
7 use List::MoreUtils 'all';
8 use Scalar::Util 'blessed', 'weaken';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Mixin::AttributeCore', 'Class::MOP::Object';
14 __PACKAGE__->meta->add_attribute(
16 reader => 'metaclass',
20 __PACKAGE__->meta->add_attribute(
21 'associated_role' => (
22 reader => 'associated_role',
26 __PACKAGE__->meta->add_attribute(
32 __PACKAGE__->meta->add_attribute(
33 'original_options' => (
34 reader => 'original_options',
39 my ( $class, $name, %options ) = @_;
42 || confess "You must provide a name for the attribute";
46 original_options => \%options,
52 my ( $self, $role ) = @_;
54 ( blessed($role) && $role->isa('Moose::Meta::Role') )
56 "You must pass a Moose::Meta::Role instance (or a subclass)";
58 weaken( $self->{'associated_role'} = $role );
61 sub attribute_for_class {
63 my $metaclass = shift;
65 return $metaclass->interpolate_class_and_new(
66 $self->name => %{ $self->original_options } );
72 return ( ref $self )->new( $self->name, %{ $self->original_options } );
79 my $self_options = $self->original_options;
80 my $other_options = $attr->original_options;
83 unless ( join q{|}, sort keys %{$self_options} ) eq ( join q{|}, sort keys %{$other_options} );
85 for my $key ( keys %{$self_options} ) {
86 return 0 if defined $self_options->{$key} && ! defined $other_options->{$key};
87 return 0 if ! defined $self_options->{$key} && defined $other_options->{$key};
89 next if all { ! defined } $self_options->{$key}, $other_options->{$key};
91 return 0 unless $self_options->{$key} eq $other_options->{$key};
103 This class implements the API for attributes in roles. Attributes in roles are
104 more like attribute prototypes than full blown attributes. While they are
105 introspectable, they have very little behavior.
109 This class provides the following methods:
113 =item B<< Moose::Meta::Role::Attribute->new(...) >>
115 This method accepts all the options that would be passed to the constructor
116 for L<Moose::Meta::Attribute>.
118 =item B<< $attr->metaclass >>
120 =item B<< $attr->is >>
122 Returns the option as passed to the constructor.
124 =item B<< $attr->associated_role >>
126 Returns the L<Moose::Meta::Role> to which this attribute belongs, if any.
128 =item B<< $attr->original_options >>
130 Returns a hash reference of options passed to the constructor. This is used
131 when creating a L<Moose::Meta::Attribute> object from this object.
133 =item B<< $attr->attach_to_role($role) >>
135 Attaches the attribute to the given L<Moose::Meta::Role>.
137 =item B<< $attr->attribute_for_class($metaclass) >>
139 Given an attribute metaclass name, this method calls C<<
140 $metaclass->interpolate_class_and_new >> to construct an attribute object
141 which can be added to a L<Moose::Meta::Class>.
143 =item B<< $attr->clone >>
145 Creates a new object identical to the object on which the method is called.
147 =item B<< $attr->is_same_as($other_attr) >>
149 Compares two role attributes and returns true if they are identical.
153 In addition, this class implements all informational predicates implements by
154 L<Moose::Meta::Attribute> (and L<Class::MOP::Attribute>).
158 See L<Moose/BUGS> for details on reporting bugs.