c0c1b6a9e9b23a8f4172a96a1ca1099bfcab22da
[gitmo/MooseX-Emulate-Class-Accessor-Fast.git] / lib / MooseX / Emulate / Class / Accessor / Fast.pm
1 package MooseX::Emulate::Class::Accessor::Fast;
2
3 use Moose::Role;
4 use Class::MOP ();
5 use Scalar::Util ();
6
7 our $VERSION = '0.00700';
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;
17     use Moose;
18
19     with 'MooseX::Emulate::Class::Accessor::Fast';
20
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
36 matter of switching your C<use base> line to a C<with> line.
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
65 =head2 BUILD $self %args
66
67 Change the default Moose class building to emulate the behavior of C::A::F and
68 store arguments in the instance hashref.
69
70 =cut
71
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
78 sub BUILD {
79   my $self = shift;
80   my %args;
81   if (scalar @_ == 1 && defined $_[0] && ref($_[0]) eq 'HASH') {
82     %args = %{$_[0]};
83   } elsif( scalar(@_) ) {
84     %args = @_;
85   }
86   my @extra = grep { !exists($self->{$_}) } keys %args;
87   @{$self}{@extra} = @args{@extra};
88   return $self;
89 }
90
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;
104   my $meta = $locate_metaclass->($self);
105   for my $attr_name (@_){
106     $meta->remove_attribute($attr_name)
107       if $meta->find_attribute_by_name($attr_name);
108     my $reader = $self->accessor_name_for($attr_name);
109     my $writer = $self->mutator_name_for( $attr_name);
110
111     #dont overwrite existing methods
112     if($reader eq $writer){
113       my %opts = ( $meta->has_method($reader) ? () : (accessor => $reader) );
114       my $attr = $meta->add_attribute($attr_name, %opts);
115       if($attr_name eq $reader){
116         my $alias = "_${attr_name}_accessor";
117         next if $meta->has_method($alias);
118         my @alias_method = $attr->process_accessors(accessor => $alias, 0);
119         $meta->add_method(@alias_method);
120       }
121     } else {
122       my @opts = ( $meta->has_method($writer) ? () : (writer => $writer) );
123       push(@opts, (reader => $reader)) unless $meta->has_method($reader);
124       $meta->add_attribute($attr_name, @opts);
125     }
126   }
127 }
128
129 =head2 mk_ro_accessors @field_names
130
131 Create read-only accessors.
132
133 =cut
134
135 sub mk_ro_accessors{
136   my $self = shift;
137   my $meta = $locate_metaclass->($self);
138   for my $attr_name (@_){
139     $meta->remove_attribute($attr_name)
140       if $meta->find_attribute_by_name($attr_name);
141     my $reader = $self->accessor_name_for($attr_name);
142     my @opts = ($meta->has_method($reader) ? () : (reader => $reader) );
143     my $attr = $meta->add_attribute($attr_name, @opts);
144     if($reader eq $attr_name && $reader eq $self->mutator_name_for($attr_name)){
145       $meta->add_method("_${attr_name}_accessor" => $attr->get_read_method_ref)
146         unless $meta->has_method("_${attr_name}_accessor");
147     }
148   }
149 }
150
151 =head2 mk_ro_accessors @field_names
152
153 Create write-only accessors.
154
155 =cut
156
157 #this is retarded.. but we need it for compatibility or whatever.
158 sub mk_wo_accessors{
159   my $self = shift;
160   my $meta = $locate_metaclass->($self);
161   for my $attr_name (@_){
162     $meta->remove_attribute($attr_name)
163       if $meta->find_attribute_by_name($attr_name);
164     my $writer = $self->mutator_name_for($attr_name);
165     my @opts = ($meta->has_method($writer) ? () : (writer => $writer) );
166     my $attr = $meta->add_attribute($attr_name, @opts);
167     if($writer eq $attr_name && $writer eq $self->accessor_name_for($attr_name)){
168       $meta->add_method("_${attr_name}_accessor" => $attr->get_write_method_ref)
169         unless $meta->has_method("_${attr_name}_accessor");
170     }
171   }
172 }
173
174 =head2 follow_best_practices
175
176 Preface readers with 'get_' and writers with 'set_'.
177 See original L<Class::Accessor> documentation for more information.
178
179 =cut
180
181 sub follow_best_practice{
182   my $self = shift;
183   my $meta = $locate_metaclass->($self);
184
185   $meta->remove_method('mutator_name_for');
186   $meta->remove_method('accessor_name_for');
187   $meta->add_method('mutator_name_for',  sub{ return "set_".$_[1] });
188   $meta->add_method('accessor_name_for', sub{ return "get_".$_[1] });
189 }
190
191 =head2 mutator_name_for
192
193 =head2 accessor_name_for
194
195 See original L<Class::Accessor> documentation for more information.
196
197 =cut
198
199 sub mutator_name_for{  return $_[1] }
200 sub accessor_name_for{ return $_[1] }
201
202 =head2 set
203
204 See original L<Class::Accessor> documentation for more information.
205
206 =cut
207
208 sub set{
209   my $self = shift;
210   my $k = shift;
211   confess "Wrong number of arguments received" unless scalar @_;
212   my $meta = $locate_metaclass->($self);
213
214   confess "No such attribute  '$k'"
215     unless ( my $attr = $meta->find_attribute_by_name($k) );
216   my $writer = $attr->get_write_method;
217   $self->$writer(@_ > 1 ? [@_] : @_);
218 }
219
220 =head2 get
221
222 See original L<Class::Accessor> documentation for more information.
223
224 =cut
225
226 sub get{
227   my $self = shift;
228   confess "Wrong number of arguments received" unless scalar @_;
229   my $meta = $locate_metaclass->($self);
230   my @values;
231
232   for( @_ ){
233     confess "No such attribute  '$_'"
234       unless ( my $attr = $meta->find_attribute_by_name($_) );
235     my $reader = $attr->get_read_method;
236     @_ > 1 ? push(@values, $self->$reader) : return $self->$reader;
237   }
238
239   return @values;
240 }
241
242 sub make_accessor {
243   my($class, $field) = @_;
244   my $meta = $locate_metaclass->($class);
245   my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); 
246   my $reader = $attr->get_read_method_ref;
247   my $writer = $attr->get_write_method_ref;
248   return sub {
249     my $self = shift;
250     return $reader->($self) unless @_;
251     return $writer->($self,(@_ > 1 ? [@_] : @_));
252   }
253 }
254
255
256 sub make_ro_accessor {
257   my($class, $field) = @_;
258   my $meta = $locate_metaclass->($class);
259   my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); 
260   return $attr->get_read_method_ref;
261 }
262
263
264 sub make_wo_accessor {
265   my($class, $field) = @_;
266   my $meta = $locate_metaclass->($class);
267   my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); 
268   return $attr->get_write_method_ref;
269 }
270
271 1;
272
273 =head2 meta
274
275 See L<Moose::Meta::Class>.
276
277 =cut
278
279 =head1 SEE ALSO
280
281 L<Moose>, L<Moose::Meta::Attribute>, L<Class::Accessor>, L<Class::Accessor::Fast>,
282 L<Class::MOP::Attribute>, L<MooseX::Adopt::Class::Accessor::Fast>
283
284 =head1 AUTHORS
285
286 Guillermo Roditi (groditi) E<lt>groditi@cpan.orgE<gt>
287
288 With contributions from:
289
290 =over 4
291
292 =item Tomas Doran E<lt>bobtfish@bobtfish.netE<gt>
293
294 =back
295
296 =head1 LICENSE
297
298 You may distribute this code under the same terms as Perl itself.
299
300 =cut