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