1 package MooseX::Emulate::Class::Accessor::Fast;
5 our $VERSION = '0.00200';
9 MooseX::Emulate::Class::Accessor::Fast -
10 Emulate Class::Accessor::Fast behavior using Moose attributes
17 with 'MooseX::Emulate::Class::Accessor::Fast';
20 #fields with readers and writers
21 __PACKAGE__->mk_accessors(qw/field1 field2/);
22 #fields with readers only
23 __PACKAGE__->mk_accessors(qw/field3 field4/);
24 #fields with writers only
25 __PACKAGE__->mk_accessors(qw/field5 field6/);
30 This module attempts to emulate the behavior of L<Class::Accessor::Fast> as
31 accurately as possible using the Moose attribute system. The public API of
32 C<Class::Accessor::Fast> is wholly supported, but the private methods are not.
33 If you are only using the public methods (as you should) migration should be a
34 matter of switching your C<use base> line to a C<with> line.
36 While I have attempted to emulate the behavior of Class::Accessor::Fast as closely
37 as possible bugs may still be lurking in edge-cases.
41 Simple documentation is provided here for your convenience, but for more thorough
42 documentation please see L<Class::Accessor::Fast> and L<Class::Accessor>.
44 =head2 A note about introspection
46 Please note that, at this time, the C<is> flag attribute is not being set. To
47 determine the C<reader> and C<writer> methods using introspection in later versions
48 of L<Class::MOP> ( > 0.38) please use the C<get_read_method> and C<get_write_method>
49 methods in L<Class::MOP::Attribute>. Example
51 # with Class::MOP <= 0.38
52 my $attr = $self->meta->find_attribute_by_name($field_name);
53 my $reader_method = $attr->reader || $attr->accessor;
54 my $writer_method = $attr->writer || $attr->accessor;
56 # with Class::MOP > 0.38
57 my $attr = $self->meta->find_attribute_by_name($field_name);
58 my $reader_method = $attr->get_read_method;
59 my $writer_method = $attr->get_write_method;
65 Extend the default Moose constructor to emulate the behavior of C::A::F and
66 store arguments in the instance hashref.
74 if (scalar @_ == 1 && defined $_[0] && ref($_[0]) eq 'HASH') {
79 my $self = $class->$orig(@_);
80 my @extra = grep { !exists($self->{$_}) } keys %args;
81 @{$self}{@extra} = @args{@extra};
85 =head2 mk_accessors @field_names
87 Create read-write accessors. An attribute named C<$field_name> will be created.
88 The name of the c<reader> and C<writer> methods will be determined by the return
89 value of C<accessor_name_for> and C<mutator_name_for>, which by default return the
90 name passed unchanged. If the accessor and mutator names are equal the C<accessor>
91 attribute will be passes to Moose, otherwise the C<reader> and C<writer> attributes
92 will be passed. Please see L<Class::MOP::Attribute> for more information.
98 my $meta = $self->meta;
99 for my $attr_name (@_){
100 my $reader = $self->accessor_name_for($attr_name);
101 my $writer = $self->mutator_name_for( $attr_name);
102 #dont overwrite existing methods
103 my @opts = $reader eq $writer ?
104 ( $self->can($reader) ? () : (accessor => $reader) ) :
106 ( $self->can($reader) ? () : (reader => $reader) ),
107 ( $self->can($writer) ? () : (writer => $writer) ),
109 $meta->add_attribute($attr_name, @opts);
111 $meta->add_method("_${attr_name}_accessor", $self->can($reader) )
112 if($reader eq $attr_name && !$self->can("_${attr_name}_accessor") );
116 =head2 mk_ro_accessors @field_names
118 Create read-only accessors.
124 my $meta = $self->meta;
125 for my $attr_name (@_){
126 my $reader = $self->accessor_name_for($attr_name);
127 $meta->add_attribute($attr_name,
128 $self->can($reader) ? () : (reader => $reader) );
129 $meta->add_method("_${attr_name}_accessor", $meta->find_method_by_name($reader))
130 if($reader eq $attr_name && !$self->can("_${attr_name}_accessor") );
134 =head2 mk_ro_accessors @field_names
136 Create write-only accessors.
140 #this is retarded.. but we need it for compatibility or whatever.
143 my $meta = $self->meta;
144 for my $attr_name (@_){
145 my $writer = $self->mutator_name_for($attr_name);
146 $meta->add_attribute($attr_name, $self->can($writer) ? () : (writer => $writer) );
147 $meta->add_method("_${attr_name}_accessor", $meta->find_method_by_name($writer))
148 if($writer eq $attr_name && !$self->can("_${attr_name}_accessor") );
152 =head2 follow_best_practices
154 Preface readers with 'get_' and writers with 'set_'.
155 See original L<Class::Accessor> documentation for more information.
159 sub follow_best_practice{
161 my $meta = $self->meta;
163 $meta->remove_method('mutator_name_for');
164 $meta->remove_method('accessor_name_for');
165 $meta->add_method('mutator_name_for', sub{ return "set_".$_[1] });
166 $meta->add_method('accessor_name_for', sub{ return "get_".$_[1] });
169 =head2 mutator_name_for
171 =head2 accessor_name_for
173 See original L<Class::Accessor> documentation for more information.
177 sub mutator_name_for{ return $_[1] }
178 sub accessor_name_for{ return $_[1] }
182 See original L<Class::Accessor> documentation for more information.
189 confess "Wrong number of arguments received" unless scalar @_;
191 #my $writer = $self->mutator_name_for( $k );
192 confess "No such attribute '$k'"
193 unless ( my $attr = $self->meta->find_attribute_by_name($k) );
194 my $writer = $attr->writer || $attr->accessor;
195 $self->$writer(@_ > 1 ? [@_] : @_);
200 See original L<Class::Accessor> documentation for more information.
206 confess "Wrong number of arguments received" unless scalar @_;
209 #while( my $attr = $self->meta->find_attribute_by_name( shift(@_) ){
211 confess "No such attribute '$_'"
212 unless ( my $attr = $self->meta->find_attribute_by_name($_) );
213 my $reader = $attr->reader || $attr->accessor;
214 @_ > 1 ? push(@values, $self->$reader) : return $self->$reader;
224 See L<Moose::Meta::Class>.
230 L<Moose>, L<Moose::Meta::Attribute>, L<Class::Accessor>, L<Class::Accessor::Fast>,
231 L<Class::MOP::Attribute>, L<MooseX::Adopt::Class::Accessor::Fast>
235 Guillermo Roditi (groditi) <groditi@cpan.org>
239 You may distribute this code under the same terms as Perl itself.