Clarify comment
[gitmo/MooseX-ClassAttribute.git] / lib / MooseX / ClassAttribute / Role / Meta / Attribute.pm
CommitLineData
a1ec1ff1 1package MooseX::ClassAttribute::Role::Meta::Attribute;
bb70fe3a 2
3use strict;
4use warnings;
5
6use MooseX::ClassAttribute::Meta::Method::Accessor;
7
a1ec1ff1 8use Moose::Role;
bb70fe3a 9
a1ec1ff1 10# This is the worst role evar! Really, this should be a subclass,
11# because it overrides a lot of behavior. However, as a subclass it
8e988dc6 12# won't cooperate with _other_ subclasses.
bb70fe3a 13
a1ec1ff1 14around 'accessor_metaclass' => sub
15{
16 return 'MooseX::ClassAttribute::Meta::Method::Accessor';
17};
bb70fe3a 18
a1ec1ff1 19around '_process_options' => sub
bb70fe3a 20{
a1ec1ff1 21 my $orig = shift;
bb70fe3a 22 my $class = shift;
23 my $name = shift;
24 my $options = shift;
25
26 confess 'A class attribute cannot be required'
27 if $options->{required};
28
a1ec1ff1 29 return $class->$orig( $name, $options );
30};
bb70fe3a 31
a1ec1ff1 32around attach_to_class => sub
bb70fe3a 33{
a1ec1ff1 34 my $orig = shift;
bb70fe3a 35 my $self = shift;
36 my $meta = shift;
37
a1ec1ff1 38 $self->$orig($meta);
bb70fe3a 39
40 $self->_initialize($meta)
41 unless $self->is_lazy();
a1ec1ff1 42};
bb70fe3a 43
a1ec1ff1 44around 'detach_from_class' => sub
bb70fe3a 45{
a1ec1ff1 46 my $orig = shift;
bb70fe3a 47 my $self = shift;
48 my $meta = shift;
49
50 $self->clear_value($meta);
51
a1ec1ff1 52 $self->$orig($meta);
53};
bb70fe3a 54
55sub _initialize
56{
6048a053 57 my $self = shift;
58 my $metaclass = shift;
bb70fe3a 59
60 if ( $self->has_default() )
61 {
d0785271 62 $self->set_value( undef, $self->default() );
bb70fe3a 63 }
64 elsif ( $self->has_builder() )
65 {
6048a053 66 $self->set_value( undef, $self->_call_builder( $metaclass->name() ) );
bb70fe3a 67 }
68}
69
a1ec1ff1 70around 'default' => sub
bb70fe3a 71{
a1ec1ff1 72 my $orig = shift;
bb70fe3a 73 my $self = shift;
74
a1ec1ff1 75 my $default = $self->$orig();
bb70fe3a 76
77 if ( $self->is_default_a_coderef() )
78 {
79 return $default->( $self->associated_class() );
80 }
81
82 return $default;
a1ec1ff1 83};
bb70fe3a 84
a1ec1ff1 85around '_call_builder' => sub
bb70fe3a 86{
a1ec1ff1 87 shift;
bb70fe3a 88 my $self = shift;
89 my $class = shift;
90
91 my $builder = $self->builder();
92
93 return $class->$builder()
94 if $class->can( $self->builder );
95
96 confess( "$class does not support builder method '"
97 . $self->builder
98 . "' for attribute '"
99 . $self->name
100 . "'" );
a1ec1ff1 101};
bb70fe3a 102
a1ec1ff1 103around 'set_value' => sub
bb70fe3a 104{
a1ec1ff1 105 shift;
0c2f7a7a 106 my $self = shift;
d0785271 107 shift; # ignoring instance or class name
0c2f7a7a 108 my $value = shift;
bb70fe3a 109
110 $self->associated_class()->set_class_attribute_value( $self->name() => $value );
a1ec1ff1 111};
bb70fe3a 112
a1ec1ff1 113around 'get_value' => sub
bb70fe3a 114{
a1ec1ff1 115 shift;
bb70fe3a 116 my $self = shift;
117
118 return $self->associated_class()->get_class_attribute_value( $self->name() );
a1ec1ff1 119};
bb70fe3a 120
a1ec1ff1 121around 'has_value' => sub
bb70fe3a 122{
a1ec1ff1 123 shift;
bb70fe3a 124 my $self = shift;
125
126 return $self->associated_class()->has_class_attribute_value( $self->name() );
a1ec1ff1 127};
bb70fe3a 128
a1ec1ff1 129around 'clear_value' => sub
bb70fe3a 130{
a1ec1ff1 131 shift;
bb70fe3a 132 my $self = shift;
133
134 return $self->associated_class()->clear_class_attribute_value( $self->name() );
a1ec1ff1 135};
bb70fe3a 136
a1ec1ff1 137no Moose::Role;
bb70fe3a 138
1391;
7a4a3b1e 140
141__END__
142
143=pod
144
145=head1 NAME
146
147MooseX::ClassAttribute::Role::Meta::Attribute - An attribute role for classes with class attributes
148
149=head1 DESCRIPTION
150
151This role modifies the behavior of class attributes in various
152ways. It really should be a subclass of C<Moose::Meta::Attribute>, but
153if it were then it couldn't be combined with other attribute
154metaclasses, like C<MooseX::AttributeHelpers>.
155
156There are no new public methods implemented by this role. All it does
157is change the behavior of a number of existing methods.
158
159=head1 AUTHOR
160
161Dave Rolsky, C<< <autarch@urth.org> >>
162
163=head1 BUGS
164
165See L<MooseX::ClassAttribute> for details.
166
167=head1 COPYRIGHT & LICENSE
168
169Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
170
171This program is free software; you can redistribute it and/or modify
172it under the same terms as Perl itself.
173
174=cut
175
176