Commit | Line | Data |
a1ec1ff1 |
1 | package MooseX::ClassAttribute::Role::Meta::Attribute; |
bb70fe3a |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use MooseX::ClassAttribute::Meta::Method::Accessor; |
7 | |
a1ec1ff1 |
8 | use 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 |
14 | around 'accessor_metaclass' => sub |
15 | { |
16 | return 'MooseX::ClassAttribute::Meta::Method::Accessor'; |
17 | }; |
bb70fe3a |
18 | |
a1ec1ff1 |
19 | around '_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 |
32 | around 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 |
44 | around '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 | |
55 | sub _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 |
70 | around '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 |
85 | around '_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 |
103 | around '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 |
113 | around '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 |
121 | around '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 |
129 | around '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 |
137 | no Moose::Role; |
bb70fe3a |
138 | |
139 | 1; |
7a4a3b1e |
140 | |
141 | __END__ |
142 | |
143 | =pod |
144 | |
145 | =head1 NAME |
146 | |
147 | MooseX::ClassAttribute::Role::Meta::Attribute - An attribute role for classes with class attributes |
148 | |
149 | =head1 DESCRIPTION |
150 | |
151 | This role modifies the behavior of class attributes in various |
152 | ways. It really should be a subclass of C<Moose::Meta::Attribute>, but |
153 | if it were then it couldn't be combined with other attribute |
154 | metaclasses, like C<MooseX::AttributeHelpers>. |
155 | |
156 | There are no new public methods implemented by this role. All it does |
157 | is change the behavior of a number of existing methods. |
158 | |
159 | =head1 AUTHOR |
160 | |
161 | Dave Rolsky, C<< <autarch@urth.org> >> |
162 | |
163 | =head1 BUGS |
164 | |
165 | See L<MooseX::ClassAttribute> for details. |
166 | |
167 | =head1 COPYRIGHT & LICENSE |
168 | |
169 | Copyright 2007-2008 Dave Rolsky, All Rights Reserved. |
170 | |
171 | This program is free software; you can redistribute it and/or modify |
172 | it under the same terms as Perl itself. |
173 | |
174 | =cut |
175 | |
176 | |