1 package Moose::Meta::Role::Attribute;
7 use List::MoreUtils 'all';
8 use Scalar::Util 'blessed', 'weaken';
10 use base 'Moose::Meta::Mixin::AttributeCore', 'Class::MOP::Object';
12 __PACKAGE__->meta->add_attribute(
14 reader => 'metaclass',
18 __PACKAGE__->meta->add_attribute(
19 'associated_role' => (
20 reader => 'associated_role',
24 __PACKAGE__->meta->add_attribute(
30 __PACKAGE__->meta->add_attribute(
31 'original_options' => (
32 reader => 'original_options',
37 my ( $class, $name, %options ) = @_;
40 || confess "You must provide a name for the attribute";
44 original_options => \%options,
50 my ( $self, $role ) = @_;
52 ( blessed($role) && $role->isa('Moose::Meta::Role') )
54 "You must pass a Moose::Meta::Role instance (or a subclass)";
56 weaken( $self->{'associated_role'} = $role );
59 sub attribute_for_class {
61 my $metaclass = shift;
63 return $metaclass->interpolate_class_and_new(
64 $self->name => %{ $self->original_options } );
70 return ( ref $self )->new( $self->name, %{ $self->original_options } );
77 my $self_options = $self->original_options;
78 my $other_options = $attr->original_options;
81 unless ( join q{|}, sort keys %{$self_options} ) eq ( join q{|}, sort keys %{$other_options} );
83 for my $key ( keys %{$self_options} ) {
84 return 0 if defined $self_options->{$key} && ! defined $other_options->{$key};
85 return 0 if ! defined $self_options->{$key} && defined $other_options->{$key};
87 next if all { ! defined } $self_options->{$key}, $other_options->{$key};
89 return 0 unless $self_options->{$key} eq $other_options->{$key};
101 This class implements the API for attributes in roles. Attributes in roles are
102 more like attribute prototypes than full blown attributes. While they are
103 introspectable, they have very little behavior.
107 This class provides the following methods:
111 =item B<< Moose::Meta::Role::Attribute->new(...) >>
113 This method accepts all the options that would be passed to the constructor
114 for L<Moose::Meta::Attribute>.
116 =item B<< $attr->metaclass >>
118 =item B<< $attr->is >>
120 Returns the option as passed to the constructor.
122 =item B<< $attr->associated_role >>
124 Returns the L<Moose::Meta::Role> to which this attribute belongs, if any.
126 =item B<< $attr->original_options >>
128 Returns a hash reference of options passed to the constructor. This is used
129 when creating a L<Moose::Meta::Attribute> object from this object.
131 =item B<< $attr->attach_to_role($role) >>
133 Attaches the attribute to the given L<Moose::Meta::Role>.
135 =item B<< $attr->attribute_for_class($metaclass) >>
137 Given an attribute metaclass name, this method calls C<<
138 $metaclass->interpolate_class_and_new >> to construct an attribute object
139 which can be added to a L<Moose::Meta::Class>.
141 =item B<< $attr->clone >>
143 Creates a new object identical to the object on which the method is called.
145 =item B<< $attr->is_same_as($other_attr) >>
147 Compares two role attributes and returns true if they are identical.
151 In addition, this class implements all informational predicates implements by
152 L<Moose::Meta::Attribute> (and L<Class::MOP::Attribute>).
156 See L<Moose/BUGS> for details on reporting bugs.