fix recreated accessor bug
[gitmo/MooseX-Emulate-Class-Accessor-Fast.git] / lib / MooseX / Emulate / Class / Accessor / Fast.pm
CommitLineData
c5a105b3 1package MooseX::Emulate::Class::Accessor::Fast;
2
e579fc46 3use Moose::Role;
5a6e3389 4use Class::MOP ();
5use Scalar::Util ();
c5a105b3 6
5a6e3389 7our $VERSION = '0.00600';
c5a105b3 8
9=head1 NAME
10
11MooseX::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
32This module attempts to emulate the behavior of L<Class::Accessor::Fast> as
33accurately as possible using the Moose attribute system. The public API of
34C<Class::Accessor::Fast> is wholly supported, but the private methods are not.
35If you are only using the public methods (as you should) migration should be a
e579fc46 36matter of switching your C<use base> line to a C<with> line.
c5a105b3 37
38While I have attempted to emulate the behavior of Class::Accessor::Fast as closely
39as possible bugs may still be lurking in edge-cases.
40
41=head1 BEHAVIOR
42
43Simple documentation is provided here for your convenience, but for more thorough
44documentation please see L<Class::Accessor::Fast> and L<Class::Accessor>.
45
46=head2 A note about introspection
47
48Please note that, at this time, the C<is> flag attribute is not being set. To
49determine the C<reader> and C<writer> methods using introspection in later versions
50of L<Class::MOP> ( > 0.38) please use the C<get_read_method> and C<get_write_method>
51methods 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 67Change the default Moose class building to emulate the behavior of C::A::F and
6b8ba79f 68store arguments in the instance hashref.
69
70=cut
71
5a6e3389 72my $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 78sub 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
93Create read-write accessors. An attribute named C<$field_name> will be created.
94The name of the c<reader> and C<writer> methods will be determined by the return
95value of C<accessor_name_for> and C<mutator_name_for>, which by default return the
96name passed unchanged. If the accessor and mutator names are equal the C<accessor>
97attribute will be passes to Moose, otherwise the C<reader> and C<writer> attributes
98will be passed. Please see L<Class::MOP::Attribute> for more information.
99
100=cut
101
102sub mk_accessors{
103 my $self = shift;
5a6e3389 104 my $meta = $locate_metaclass->($self);
c5a105b3 105 for my $attr_name (@_){
54a5b50a 106 $meta->remove_attribute($attr_name)
107 if $meta->find_attribute_by_name($attr_name);
c5a105b3 108 my $reader = $self->accessor_name_for($attr_name);
109 my $writer = $self->mutator_name_for( $attr_name);
30cbeb5e 110
c5a105b3 111 #dont overwrite existing methods
30cbeb5e 112 if($reader eq $writer){
5a6e3389 113 my %opts = ( $meta->has_method($reader) ? () : (accessor => $reader) );
30cbeb5e 114 my $attr = $meta->add_attribute($attr_name, %opts);
115 if($attr_name eq $reader){
116 my $alias = "_${attr_name}_accessor";
5a6e3389 117 next if $meta->has_method($alias);
118 my @alias_method = $attr->process_accessors(accessor => $alias, 0);
30cbeb5e 119 $meta->add_method(@alias_method);
120 }
121 } else {
5a6e3389 122 my @opts = ( $meta->has_method($writer) ? () : (writer => $writer) );
123 push(@opts, (reader => $reader)) unless $meta->has_method($reader);
30cbeb5e 124 $meta->add_attribute($attr_name, @opts);
125 }
c5a105b3 126 }
127}
128
129=head2 mk_ro_accessors @field_names
130
131Create read-only accessors.
132
133=cut
134
135sub mk_ro_accessors{
136 my $self = shift;
5a6e3389 137 my $meta = $locate_metaclass->($self);
c5a105b3 138 for my $attr_name (@_){
54a5b50a 139 $meta->remove_attribute($attr_name)
140 if $meta->find_attribute_by_name($attr_name);
c5a105b3 141 my $reader = $self->accessor_name_for($attr_name);
5a6e3389 142 my @opts = ($meta->has_method($reader) ? () : (reader => $reader) );
30cbeb5e 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)
5a6e3389 146 unless $meta->has_method("_${attr_name}_accessor");
30cbeb5e 147 }
c5a105b3 148 }
149}
150
151=head2 mk_ro_accessors @field_names
152
153Create write-only accessors.
154
155=cut
156
157#this is retarded.. but we need it for compatibility or whatever.
158sub mk_wo_accessors{
159 my $self = shift;
5a6e3389 160 my $meta = $locate_metaclass->($self);
c5a105b3 161 for my $attr_name (@_){
54a5b50a 162 $meta->remove_attribute($attr_name)
163 if $meta->find_attribute_by_name($attr_name);
c5a105b3 164 my $writer = $self->mutator_name_for($attr_name);
5a6e3389 165 my @opts = ($meta->has_method($writer) ? () : (writer => $writer) );
30cbeb5e 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)
5a6e3389 169 unless $meta->has_method("_${attr_name}_accessor");
30cbeb5e 170 }
c5a105b3 171 }
172}
173
174=head2 follow_best_practices
175
176Preface readers with 'get_' and writers with 'set_'.
177See original L<Class::Accessor> documentation for more information.
178
179=cut
180
181sub follow_best_practice{
182 my $self = shift;
5a6e3389 183 my $meta = $locate_metaclass->($self);
c5a105b3 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
195See original L<Class::Accessor> documentation for more information.
196
197=cut
198
199sub mutator_name_for{ return $_[1] }
200sub accessor_name_for{ return $_[1] }
201
202=head2 set
203
204See original L<Class::Accessor> documentation for more information.
205
206=cut
207
208sub set{
209 my $self = shift;
210 my $k = shift;
211 confess "Wrong number of arguments received" unless scalar @_;
5a6e3389 212 my $meta = $locate_metaclass->($self);
c5a105b3 213
c5a105b3 214 confess "No such attribute '$k'"
5a6e3389 215 unless ( my $attr = $meta->find_attribute_by_name($k) );
216 my $writer = $attr->get_write_method;
c5a105b3 217 $self->$writer(@_ > 1 ? [@_] : @_);
218}
219
220=head2 get
221
222See original L<Class::Accessor> documentation for more information.
223
224=cut
225
226sub get{
227 my $self = shift;
228 confess "Wrong number of arguments received" unless scalar @_;
5a6e3389 229 my $meta = $locate_metaclass->($self);
c5a105b3 230 my @values;
5a6e3389 231
c5a105b3 232 for( @_ ){
233 confess "No such attribute '$_'"
5a6e3389 234 unless ( my $attr = $meta->find_attribute_by_name($_) );
235 my $reader = $attr->get_read_method;
c5a105b3 236 @_ > 1 ? push(@values, $self->$reader) : return $self->$reader;
237 }
238
239 return @values;
240}
241
8eb9108b 242sub make_accessor {
243 my($class, $field) = @_;
5a6e3389 244 my $meta = $locate_metaclass->($class);
8eb9108b 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;
e8abb6ef 250 return $reader->($self) unless @_;
251 return $writer->($self,(@_ > 1 ? [@_] : @_));
8eb9108b 252 }
253}
254
255
256sub make_ro_accessor {
257 my($class, $field) = @_;
5a6e3389 258 my $meta = $locate_metaclass->($class);
8eb9108b 259 my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field);
260 return $attr->get_read_method_ref;
261}
262
263
264sub make_wo_accessor {
265 my($class, $field) = @_;
5a6e3389 266 my $meta = $locate_metaclass->($class);
8eb9108b 267 my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field);
268 return $attr->get_write_method_ref;
269}
270
c5a105b3 2711;
272
273=head2 meta
274
275See L<Moose::Meta::Class>.
276
277=cut
278
279=head1 SEE ALSO
280
281L<Moose>, L<Moose::Meta::Attribute>, L<Class::Accessor>, L<Class::Accessor::Fast>,
282L<Class::MOP::Attribute>, L<MooseX::Adopt::Class::Accessor::Fast>
283
d82bc8be 284=head1 AUTHORS
c5a105b3 285
7ed9430a 286Guillermo Roditi (groditi) E<lt>groditi@cpan.orgE<gt>
c5a105b3 287
d82bc8be 288With contributions from:
289
290=over 4
291
292=item Tomas Doran E<lt>bobtfish@bobtfish.netE<gt>
293
294=back
295
c5a105b3 296=head1 LICENSE
297
298You may distribute this code under the same terms as Perl itself.
299
300=cut