Beginning of dzilization
[gitmo/Moose.git] / lib / Moose / Meta / Role / Attribute.pm
CommitLineData
f785aad8 1package Moose::Meta::Role::Attribute;
2
3use strict;
4use warnings;
5
6use Carp 'confess';
7use List::MoreUtils 'all';
8use Scalar::Util 'blessed', 'weaken';
9
f785aad8 10our $AUTHORITY = 'cpan:STEVAN';
11
be83e895 12use base 'Moose::Meta::Mixin::AttributeCore', 'Class::MOP::Object';
f785aad8 13
14__PACKAGE__->meta->add_attribute(
15 'metaclass' => (
16 reader => 'metaclass',
17 )
18);
19
20__PACKAGE__->meta->add_attribute(
21 'associated_role' => (
22 reader => 'associated_role',
23 )
24);
25
26__PACKAGE__->meta->add_attribute(
27 'is' => (
28 reader => 'is',
29 )
30);
31
32__PACKAGE__->meta->add_attribute(
33 'original_options' => (
34 reader => 'original_options',
35 )
36);
37
38sub new {
39 my ( $class, $name, %options ) = @_;
40
41 (defined $name)
42 || confess "You must provide a name for the attribute";
43
44 return bless {
45 name => $name,
46 original_options => \%options,
47 %options,
48 }, $class;
49}
50
51sub attach_to_role {
52 my ( $self, $role ) = @_;
53
54 ( blessed($role) && $role->isa('Moose::Meta::Role') )
55 || confess
56 "You must pass a Moose::Meta::Role instance (or a subclass)";
57
58 weaken( $self->{'associated_role'} = $role );
59}
60
61sub attribute_for_class {
62 my $self = shift;
63 my $metaclass = shift;
64
65 return $metaclass->interpolate_class_and_new(
66 $self->name => %{ $self->original_options } );
67}
68
69sub clone {
70 my $self = shift;
71
72 return ( ref $self )->new( $self->name, %{ $self->original_options } );
73}
74
75sub is_same_as {
76 my $self = shift;
77 my $attr = shift;
78
79 my $self_options = $self->original_options;
80 my $other_options = $attr->original_options;
81
82 return 0
83 unless ( join q{|}, sort keys %{$self_options} ) eq ( join q{|}, sort keys %{$other_options} );
84
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};
88
89 next if all { ! defined } $self_options->{$key}, $other_options->{$key};
90
91 return 0 unless $self_options->{$key} eq $other_options->{$key};
92 }
93
94 return 1;
95}
96
971;
98
99=pod
100
f785aad8 101=head1 DESCRIPTION
102
103This class implements the API for attributes in roles. Attributes in roles are
104more like attribute prototypes than full blown attributes. While they are
105introspectable, they have very little behavior.
106
107=head1 METHODS
108
109This class provides the following methods:
110
111=over 4
112
113=item B<< Moose::Meta::Role::Attribute->new(...) >>
114
115This method accepts all the options that would be passed to the constructor
116for L<Moose::Meta::Attribute>.
117
118=item B<< $attr->metaclass >>
119
120=item B<< $attr->is >>
121
122Returns the option as passed to the constructor.
123
124=item B<< $attr->associated_role >>
125
126Returns the L<Moose::Meta::Role> to which this attribute belongs, if any.
127
128=item B<< $attr->original_options >>
129
130Returns a hash reference of options passed to the constructor. This is used
131when creating a L<Moose::Meta::Attribute> object from this object.
132
133=item B<< $attr->attach_to_role($role) >>
134
135Attaches the attribute to the given L<Moose::Meta::Role>.
136
137=item B<< $attr->attribute_for_class($metaclass) >>
138
139Given an attribute metaclass name, this method calls C<<
140$metaclass->interpolate_class_and_new >> to construct an attribute object
141which can be added to a L<Moose::Meta::Class>.
142
143=item B<< $attr->clone >>
144
145Creates a new object identical to the object on which the method is called.
146
147=item B<< $attr->is_same_as($other_attr) >>
148
149Compares two role attributes and returns true if they are identical.
150
151=back
152
153In addition, this class implements all informational predicates implements by
154L<Moose::Meta::Attribute> (and L<Class::MOP::Attribute>).
155
156=head1 BUGS
157
d4048ef3 158See L<Moose/BUGS> for details on reporting bugs.
f785aad8 159
f785aad8 160=cut