Commit | Line | Data |
c5a105b3 |
1 | package MooseX::Emulate::Class::Accessor::Fast; |
2 | |
e579fc46 |
3 | use Moose::Role; |
5a6e3389 |
4 | use Class::MOP (); |
5 | use Scalar::Util (); |
c5a105b3 |
6 | |
b41ad5fb |
7 | use MooseX::Emulate::Class::Accessor::Fast::Meta::Accessor (); |
8 | |
986ca883 |
9 | our $VERSION = '0.00800'; |
c5a105b3 |
10 | |
11 | =head1 NAME |
12 | |
736d6822 |
13 | MooseX::Emulate::Class::Accessor::Fast - Emulate Class::Accessor::Fast behavior using Moose attributes |
c5a105b3 |
14 | |
15 | =head1 SYNOPSYS |
16 | |
17 | package MyClass; |
144866f7 |
18 | use Moose; |
e579fc46 |
19 | |
20 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
c5a105b3 |
21 | |
c5a105b3 |
22 | |
23 | #fields with readers and writers |
24 | __PACKAGE__->mk_accessors(qw/field1 field2/); |
25 | #fields with readers only |
26 | __PACKAGE__->mk_accessors(qw/field3 field4/); |
27 | #fields with writers only |
28 | __PACKAGE__->mk_accessors(qw/field5 field6/); |
29 | |
30 | |
31 | =head1 DESCRIPTION |
32 | |
33 | This module attempts to emulate the behavior of L<Class::Accessor::Fast> as |
34 | accurately as possible using the Moose attribute system. The public API of |
35 | C<Class::Accessor::Fast> is wholly supported, but the private methods are not. |
36 | If you are only using the public methods (as you should) migration should be a |
e579fc46 |
37 | matter of switching your C<use base> line to a C<with> line. |
c5a105b3 |
38 | |
39 | While I have attempted to emulate the behavior of Class::Accessor::Fast as closely |
40 | as possible bugs may still be lurking in edge-cases. |
41 | |
42 | =head1 BEHAVIOR |
43 | |
44 | Simple documentation is provided here for your convenience, but for more thorough |
45 | documentation please see L<Class::Accessor::Fast> and L<Class::Accessor>. |
46 | |
47 | =head2 A note about introspection |
48 | |
49 | Please note that, at this time, the C<is> flag attribute is not being set. To |
50 | determine the C<reader> and C<writer> methods using introspection in later versions |
51 | of L<Class::MOP> ( > 0.38) please use the C<get_read_method> and C<get_write_method> |
52 | methods in L<Class::MOP::Attribute>. Example |
53 | |
54 | # with Class::MOP <= 0.38 |
55 | my $attr = $self->meta->find_attribute_by_name($field_name); |
56 | my $reader_method = $attr->reader || $attr->accessor; |
57 | my $writer_method = $attr->writer || $attr->accessor; |
58 | |
59 | # with Class::MOP > 0.38 |
60 | my $attr = $self->meta->find_attribute_by_name($field_name); |
61 | my $reader_method = $attr->get_read_method; |
62 | my $writer_method = $attr->get_write_method; |
63 | |
64 | =head1 METHODS |
65 | |
144866f7 |
66 | =head2 BUILD $self %args |
6b8ba79f |
67 | |
144866f7 |
68 | Change the default Moose class building to emulate the behavior of C::A::F and |
6b8ba79f |
69 | store arguments in the instance hashref. |
70 | |
71 | =cut |
72 | |
5a6e3389 |
73 | my $locate_metaclass = sub { |
74 | my $class = Scalar::Util::blessed($_[0]) || $_[0]; |
75 | return Class::MOP::get_metaclass_by_name($class) |
76 | || Moose::Meta::Class->initialize($class); |
77 | }; |
78 | |
144866f7 |
79 | sub BUILD { |
80 | my $self = shift; |
6b8ba79f |
81 | my %args; |
82 | if (scalar @_ == 1 && defined $_[0] && ref($_[0]) eq 'HASH') { |
83 | %args = %{$_[0]}; |
7ed9430a |
84 | } elsif( scalar(@_) ) { |
6b8ba79f |
85 | %args = @_; |
86 | } |
6b8ba79f |
87 | my @extra = grep { !exists($self->{$_}) } keys %args; |
88 | @{$self}{@extra} = @args{@extra}; |
89 | return $self; |
144866f7 |
90 | } |
6b8ba79f |
91 | |
c5a105b3 |
92 | =head2 mk_accessors @field_names |
93 | |
94 | Create read-write accessors. An attribute named C<$field_name> will be created. |
95 | The name of the c<reader> and C<writer> methods will be determined by the return |
96 | value of C<accessor_name_for> and C<mutator_name_for>, which by default return the |
97 | name passed unchanged. If the accessor and mutator names are equal the C<accessor> |
98 | attribute will be passes to Moose, otherwise the C<reader> and C<writer> attributes |
99 | will be passed. Please see L<Class::MOP::Attribute> for more information. |
100 | |
101 | =cut |
102 | |
6b6bc6e8 |
103 | sub mk_accessors { |
c5a105b3 |
104 | my $self = shift; |
5a6e3389 |
105 | my $meta = $locate_metaclass->($self); |
b3050bf2 |
106 | my $class = $meta->name; |
107 | confess("You are trying to modify ${class}, which has been made immutable, this is ". |
108 | "not supported. Try subclassing ${class}, rather than monkeypatching it") |
109 | if $meta->is_immutable; |
110 | |
c5a105b3 |
111 | for my $attr_name (@_){ |
54a5b50a |
112 | $meta->remove_attribute($attr_name) |
113 | if $meta->find_attribute_by_name($attr_name); |
c5a105b3 |
114 | my $reader = $self->accessor_name_for($attr_name); |
115 | my $writer = $self->mutator_name_for( $attr_name); |
30cbeb5e |
116 | |
c5a105b3 |
117 | #dont overwrite existing methods |
30cbeb5e |
118 | if($reader eq $writer){ |
5a6e3389 |
119 | my %opts = ( $meta->has_method($reader) ? () : (accessor => $reader) ); |
b41ad5fb |
120 | my $attr = $meta->find_attribute_by_name($attr_name) || $meta->add_attribute($attr_name, %opts, |
121 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
122 | ); |
30cbeb5e |
123 | if($attr_name eq $reader){ |
124 | my $alias = "_${attr_name}_accessor"; |
5a6e3389 |
125 | next if $meta->has_method($alias); |
18991513 |
126 | $meta->add_method($alias => $attr->get_read_method_ref); |
30cbeb5e |
127 | } |
128 | } else { |
5a6e3389 |
129 | my @opts = ( $meta->has_method($writer) ? () : (writer => $writer) ); |
130 | push(@opts, (reader => $reader)) unless $meta->has_method($reader); |
b41ad5fb |
131 | my $attr = $meta->find_attribute_by_name($attr_name) || $meta->add_attribute($attr_name, @opts, |
132 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
133 | ); |
30cbeb5e |
134 | } |
c5a105b3 |
135 | } |
136 | } |
137 | |
138 | =head2 mk_ro_accessors @field_names |
139 | |
140 | Create read-only accessors. |
141 | |
142 | =cut |
143 | |
6b6bc6e8 |
144 | sub mk_ro_accessors { |
c5a105b3 |
145 | my $self = shift; |
5a6e3389 |
146 | my $meta = $locate_metaclass->($self); |
b3050bf2 |
147 | my $class = $meta->name; |
148 | confess("You are trying to modify ${class}, which has been made immutable, this is ". |
149 | "not supported. Try subclassing ${class}, rather than monkeypatching it") |
150 | if $meta->is_immutable; |
c5a105b3 |
151 | for my $attr_name (@_){ |
54a5b50a |
152 | $meta->remove_attribute($attr_name) |
153 | if $meta->find_attribute_by_name($attr_name); |
c5a105b3 |
154 | my $reader = $self->accessor_name_for($attr_name); |
5a6e3389 |
155 | my @opts = ($meta->has_method($reader) ? () : (reader => $reader) ); |
b41ad5fb |
156 | my $attr = $meta->add_attribute($attr_name, @opts, |
157 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
158 | ) if scalar(@opts); |
30cbeb5e |
159 | if($reader eq $attr_name && $reader eq $self->mutator_name_for($attr_name)){ |
160 | $meta->add_method("_${attr_name}_accessor" => $attr->get_read_method_ref) |
5a6e3389 |
161 | unless $meta->has_method("_${attr_name}_accessor"); |
30cbeb5e |
162 | } |
c5a105b3 |
163 | } |
164 | } |
165 | |
166 | =head2 mk_ro_accessors @field_names |
167 | |
168 | Create write-only accessors. |
169 | |
170 | =cut |
171 | |
172 | #this is retarded.. but we need it for compatibility or whatever. |
6b6bc6e8 |
173 | sub mk_wo_accessors { |
c5a105b3 |
174 | my $self = shift; |
5a6e3389 |
175 | my $meta = $locate_metaclass->($self); |
b3050bf2 |
176 | my $class = $meta->name; |
177 | confess("You are trying to modify ${class}, which has been made immutable, this is ". |
178 | "not supported. Try subclassing ${class}, rather than monkeypatching it") |
179 | if $meta->is_immutable; |
c5a105b3 |
180 | for my $attr_name (@_){ |
54a5b50a |
181 | $meta->remove_attribute($attr_name) |
182 | if $meta->find_attribute_by_name($attr_name); |
c5a105b3 |
183 | my $writer = $self->mutator_name_for($attr_name); |
5a6e3389 |
184 | my @opts = ($meta->has_method($writer) ? () : (writer => $writer) ); |
b41ad5fb |
185 | my $attr = $meta->add_attribute($attr_name, @opts, |
186 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
187 | ) if scalar(@opts); |
30cbeb5e |
188 | if($writer eq $attr_name && $writer eq $self->accessor_name_for($attr_name)){ |
189 | $meta->add_method("_${attr_name}_accessor" => $attr->get_write_method_ref) |
5a6e3389 |
190 | unless $meta->has_method("_${attr_name}_accessor"); |
30cbeb5e |
191 | } |
c5a105b3 |
192 | } |
193 | } |
194 | |
195 | =head2 follow_best_practices |
196 | |
197 | Preface readers with 'get_' and writers with 'set_'. |
198 | See original L<Class::Accessor> documentation for more information. |
199 | |
200 | =cut |
201 | |
6b6bc6e8 |
202 | sub follow_best_practice { |
c5a105b3 |
203 | my $self = shift; |
5a6e3389 |
204 | my $meta = $locate_metaclass->($self); |
c5a105b3 |
205 | |
206 | $meta->remove_method('mutator_name_for'); |
207 | $meta->remove_method('accessor_name_for'); |
208 | $meta->add_method('mutator_name_for', sub{ return "set_".$_[1] }); |
209 | $meta->add_method('accessor_name_for', sub{ return "get_".$_[1] }); |
210 | } |
211 | |
212 | =head2 mutator_name_for |
213 | |
214 | =head2 accessor_name_for |
215 | |
216 | See original L<Class::Accessor> documentation for more information. |
217 | |
218 | =cut |
219 | |
6b6bc6e8 |
220 | sub mutator_name_for { return $_[1] } |
221 | sub accessor_name_for { return $_[1] } |
c5a105b3 |
222 | |
223 | =head2 set |
224 | |
225 | See original L<Class::Accessor> documentation for more information. |
226 | |
227 | =cut |
228 | |
6b6bc6e8 |
229 | sub set { |
c5a105b3 |
230 | my $self = shift; |
231 | my $k = shift; |
232 | confess "Wrong number of arguments received" unless scalar @_; |
5a6e3389 |
233 | my $meta = $locate_metaclass->($self); |
c5a105b3 |
234 | |
c5a105b3 |
235 | confess "No such attribute '$k'" |
5a6e3389 |
236 | unless ( my $attr = $meta->find_attribute_by_name($k) ); |
237 | my $writer = $attr->get_write_method; |
c5a105b3 |
238 | $self->$writer(@_ > 1 ? [@_] : @_); |
239 | } |
240 | |
241 | =head2 get |
242 | |
243 | See original L<Class::Accessor> documentation for more information. |
244 | |
245 | =cut |
246 | |
6b6bc6e8 |
247 | sub get { |
c5a105b3 |
248 | my $self = shift; |
249 | confess "Wrong number of arguments received" unless scalar @_; |
5a6e3389 |
250 | my $meta = $locate_metaclass->($self); |
c5a105b3 |
251 | my @values; |
5a6e3389 |
252 | |
c5a105b3 |
253 | for( @_ ){ |
254 | confess "No such attribute '$_'" |
5a6e3389 |
255 | unless ( my $attr = $meta->find_attribute_by_name($_) ); |
256 | my $reader = $attr->get_read_method; |
c5a105b3 |
257 | @_ > 1 ? push(@values, $self->$reader) : return $self->$reader; |
258 | } |
259 | |
260 | return @values; |
261 | } |
262 | |
8eb9108b |
263 | sub make_accessor { |
264 | my($class, $field) = @_; |
5a6e3389 |
265 | my $meta = $locate_metaclass->($class); |
b41ad5fb |
266 | my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field, |
267 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
268 | ); |
8eb9108b |
269 | my $reader = $attr->get_read_method_ref; |
270 | my $writer = $attr->get_write_method_ref; |
271 | return sub { |
272 | my $self = shift; |
e8abb6ef |
273 | return $reader->($self) unless @_; |
274 | return $writer->($self,(@_ > 1 ? [@_] : @_)); |
8eb9108b |
275 | } |
276 | } |
277 | |
278 | |
279 | sub make_ro_accessor { |
280 | my($class, $field) = @_; |
5a6e3389 |
281 | my $meta = $locate_metaclass->($class); |
b41ad5fb |
282 | my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field, |
283 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
284 | ); |
8eb9108b |
285 | return $attr->get_read_method_ref; |
286 | } |
287 | |
288 | |
289 | sub make_wo_accessor { |
290 | my($class, $field) = @_; |
5a6e3389 |
291 | my $meta = $locate_metaclass->($class); |
b41ad5fb |
292 | my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field, |
293 | traits => ['MooseX::Emulate::Class::Accessor::Fast::Meta::Role::Attribute'] |
294 | ); |
8eb9108b |
295 | return $attr->get_write_method_ref; |
296 | } |
297 | |
c5a105b3 |
298 | 1; |
299 | |
300 | =head2 meta |
301 | |
302 | See L<Moose::Meta::Class>. |
303 | |
304 | =cut |
305 | |
306 | =head1 SEE ALSO |
307 | |
308 | L<Moose>, L<Moose::Meta::Attribute>, L<Class::Accessor>, L<Class::Accessor::Fast>, |
309 | L<Class::MOP::Attribute>, L<MooseX::Adopt::Class::Accessor::Fast> |
310 | |
d82bc8be |
311 | =head1 AUTHORS |
c5a105b3 |
312 | |
7ed9430a |
313 | Guillermo Roditi (groditi) E<lt>groditi@cpan.orgE<gt> |
c5a105b3 |
314 | |
d82bc8be |
315 | With contributions from: |
316 | |
317 | =over 4 |
318 | |
986ca883 |
319 | =item Tomas Doran (t0m) E<lt>bobtfish@bobtfish.netE<gt> |
320 | |
321 | =item Florian Ragwitz (rafl) E<lt>rafl@debian.orgE<gt> |
d82bc8be |
322 | |
323 | =back |
324 | |
c5a105b3 |
325 | =head1 LICENSE |
326 | |
327 | You may distribute this code under the same terms as Perl itself. |
328 | |
329 | =cut |