1 package Moose::Meta::Role::Attribute;
7 use List::MoreUtils 'all';
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '1.05';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Mixin::AttributeCore';
15 __PACKAGE__->meta->add_attribute(
17 reader => 'metaclass',
21 __PACKAGE__->meta->add_attribute(
22 'associated_role' => (
23 reader => 'associated_role',
27 __PACKAGE__->meta->add_attribute(
33 __PACKAGE__->meta->add_attribute(
34 'original_options' => (
35 reader => 'original_options',
40 my ( $class, $name, %options ) = @_;
43 || confess "You must provide a name for the attribute";
47 original_options => \%options,
53 my ( $self, $role ) = @_;
55 ( blessed($role) && $role->isa('Moose::Meta::Role') )
57 "You must pass a Moose::Meta::Role instance (or a subclass)";
59 weaken( $self->{'associated_role'} = $role );
62 sub attribute_for_class {
64 my $metaclass = shift;
66 return $metaclass->interpolate_class_and_new(
67 $self->name => %{ $self->original_options } );
73 return ( ref $self )->new( $self->name, %{ $self->original_options } );
80 my $self_options = $self->original_options;
81 my $other_options = $attr->original_options;
84 unless ( join q{|}, sort keys %{$self_options} ) eq ( join q{|}, sort keys %{$other_options} );
86 for my $key ( keys %{$self_options} ) {
87 return 0 if defined $self_options->{$key} && ! defined $other_options->{$key};
88 return 0 if ! defined $self_options->{$key} && defined $other_options->{$key};
90 next if all { ! defined } $self_options->{$key}, $other_options->{$key};
92 return 0 unless $self_options->{$key} eq $other_options->{$key};
104 Moose::Meta::Role::Attribute - A Moose Attribute metaclass for Roles
108 This class implements the API for attributes in roles. Attributes in roles are
109 more like attribute prototypes than full blown attributes. While they are
110 introspectable, they have very little behavior.
114 This class provides the following methods:
118 =item B<< Moose::Meta::Role::Attribute->new(...) >>
120 This method accepts all the options that would be passed to the constructor
121 for L<Moose::Meta::Attribute>.
123 =item B<< $attr->metaclass >>
125 =item B<< $attr->is >>
127 Returns the option as passed to the constructor.
129 =item B<< $attr->associated_role >>
131 Returns the L<Moose::Meta::Role> to which this attribute belongs, if any.
133 =item B<< $attr->original_options >>
135 Returns a hash reference of options passed to the constructor. This is used
136 when creating a L<Moose::Meta::Attribute> object from this object.
138 =item B<< $attr->attach_to_role($role) >>
140 Attaches the attribute to the given L<Moose::Meta::Role>.
142 =item B<< $attr->attribute_for_class($metaclass) >>
144 Given an attribute metaclass name, this method calls C<<
145 $metaclass->interpolate_class_and_new >> to construct an attribute object
146 which can be added to a L<Moose::Meta::Class>.
148 =item B<< $attr->clone >>
150 Creates a new object identical to the object on which the method is called.
152 =item B<< $attr->is_same_as($other_attr) >>
154 Compares two role attributes and returns true if they are identical.
158 In addition, this class implements all informational predicates implements by
159 L<Moose::Meta::Attribute> (and L<Class::MOP::Attribute>).
163 See L<Moose/BUGS> for details on reporting bugs.
167 Dave Rolsky E<lt>autarch@urth.orgE<gt>
169 =head1 COPYRIGHT AND LICENSE
171 Copyright 2006-2010 by Infinity Interactive, Inc.
173 L<http://www.iinteractive.com>
175 This library is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself.