Emulate/Class/Accessor/Fast.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 89.2% 25 / 28 Conditioncond 93.3% 14 / 15 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 27 / 27 Podpod 91.6% 11 / 12 totaltotal 96.4% 189 / 196 CC 24 SCAR 16.3
CC
24
Cov
97%
_make_moocaf_accessor
14.0
get
3.1
__ANON__
3.0
CRAP 5.1
↑ Summary Adopt/Class/Accessor/Fast.pm »
1 package MooX::Emulate::Class::Accessor::Fast;
2 9 our $VERSION = '0.06';
3  
4 =head1 NAME
5  
6 MooX::Emulate::Class::Accessor::Fast - Emulate Class::Accessor::Fast behavior using Moo attributes.
7  
8 =head1 SYNOPSYS
9  
10 package MyClass;
11 use Moo;
12
13 with 'MooX::Emulate::Class::Accessor::Fast';
14
15 # Fields with readers and writers:
16 __PACKAGE__->mk_accessors(qw/field1 field2/);
17
18 # Fields with readers only:
19 __PACKAGE__->mk_ro_accessors(qw/field3 field4/);
20
21 # Fields with writers only:
22 __PACKAGE__->mk_wo_accessors(qw/field5 field6/);
23  
24  
25 =head1 DESCRIPTION
26  
27 This module attempts to emulate the behavior of L<Class::Accessor::Fast> as
28 accurately as possible using the Moo attribute system. The public API of
29 Class::Accessor::Fast is wholly supported, but the private methods are not.
30 If you are only using the public methods (as you should) migration should be a
31 matter of switching your C<use base> line to a C<with> line.
32  
33 This module is a straight fork-and-port of L<MooseX::Emulate::Class::Accessor::Fast>
34 version C<0.00903> for L<Moo>. All tests from the original Moose module pass or
35 were, as little as possible, modified to pass. Much of the documentation, code
36 concepts, and tests are just straight copied from the original module. The core
37 functionality, though, had to be a complete rewrite for Moo.
38  
39 While we 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 =cut
48  
49 10 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 9 ▶use Class::Method::Modifiers qw( install_modifier );
Subroutine BEGIN: called CC=1 SCAR=0.0
51 9 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 9 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 20 ▶sub BUILD { }
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
56  
57 21 ▶around BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 21 my $orig = shift;
59 21 my $self = shift;
60  
61 21 my %args = %{ $_[0] };
62 21 $self->$orig(\%args);
63  
64 21 my @extra = grep { !exists($self->{$_}) } keys %args;
65 21 @{$self}{@extra} = @args{@extra};
66  
67 21 return $self;
68 9 };
69  
70 =head1 METHODS
71  
72 =head2 mk_accessors
73  
74 __PACKAGE__->mk_accessors( @field_names );
75  
76 See L<Class::Accessor/mk_accessors>.
77  
78 =cut
79  
80 11 ▶sub mk_accessors {
Subroutine mk_accessors: called CC=2 SCAR=6.9
Pod documented
81 11 my ($class, @fields) = @_;
82  
83 11 foreach my $field (@fields) {
84 12 $class->make_accessor( $field );
85 }
86  
87 11 return;
88 }
89  
90 =head2 mk_ro_accessors
91  
92 __PACKAGE__->mk_ro_accessors( @field_names );
93  
94 See L<Class::Accessor/mk_ro_accessors>.
95  
96 =cut
97  
98 7 ▶sub mk_ro_accessors {
Subroutine mk_ro_accessors: called CC=2 SCAR=6.9
Pod documented
99 7 my ($class, @fields) = @_;
100  
101 7 foreach my $field (@fields) {
102 7 $class->make_ro_accessor( $field );
103 }
104  
105 7 return;
106 }
107  
108 =head2 mk_wo_accessors
109  
110 __PACKAGE__->mk_wo_accessors( @field_names );
111  
112 See L<Class::Accessor/mk_wo_accessors>.
113  
114 =cut
115  
116 7 ▶sub mk_wo_accessors {
Subroutine mk_wo_accessors: called CC=2 SCAR=6.9
Pod documented
117 7 my ($class, @fields) = @_;
118  
119 7 foreach my $field (@fields) {
120 7 $class->make_wo_accessor( $field );
121 }
122  
123 7 return;
124 }
125  
126 =head2 follow_best_practice
127  
128 __PACKAGE__->follow_best_practice();
129  
130 See L<Class::Accessor/follow_best_practice>.
131  
132 =cut
133  
134 1 ▶sub follow_best_practice {
Subroutine follow_best_practice: called CC=1 SCAR=0.0
Pod documented
135 1 my ($class) = @_;
136  
137 1 my $stash = Package::Stash->new( $class );
138  
139 $stash->add_symbol(
140 '&mutator_name_for',
141 3 ▶ sub{ 'set_' . $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1 );
143  
144 $stash->add_symbol(
145 '&accessor_name_for',
146 3 ▶ sub{ 'get_' . $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 );
148  
149 1 return;
150 }
151  
152 =head2 mutator_name_for
153  
154 sub mutator_name_for { 'change_' . $_[1] }
155  
156 See L<Class::Accessor/MAKING ACCESSORS>.
157  
158 =cut
159  
160 23 ▶sub mutator_name_for { $_[1] }
Subroutine mutator_name_for: called CC=1 SCAR=0.0
Pod documented
161  
162 =head2 accessor_name_for
163  
164 sub accessor_name_for { 'retrieve_' . $_[1] }
165  
166 See L<Class::Accessor/MAKING ACCESSORS>.
167  
168 =cut
169  
170 23 ▶sub accessor_name_for { $_[1] }
Subroutine accessor_name_for: called CC=1 SCAR=0.0
Pod documented
171  
172 =head2 set
173  
174 $object->set( $field => $value );
175  
176 See L<Class::Accessor/set>.
177  
178 =cut
179  
180 1 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
181 1 my $self = shift;
182 1 my $field = shift;
183  
184 1 my $method = "_set_moocaf_$field";
185 1 return $self->$method( @_ );
186 }
187  
188 =head2 get
189  
190 my $value = $object->get( $field );
191 my @values = $object->get( $field1, $field2 );
192  
193 See L<Class::Accessor/get>.
194  
195 =cut
196  
197 2 ▶sub get {
Subroutine get: called CC=3 SCAR=11.3
Pod documented
198 2 my $self = shift;
199  
200 2 my @values;
201 2 foreach my $field (@_) {
202 2 my $method = "_get_moocaf_$field";
203 2 push @values, $self->$method();
204 }
205  
206 2 ▶ return $values[0] if @values==1;
Branch: 1 branch
BranchTrueFalseTotal
if @values == 1 2 0 2
207 0 return @values;
208 }
209  
210 29 ▶sub _make_moocaf_accessor {
Subroutine _make_moocaf_accessor: called CC=14 SCAR=26.4
211 29 my ($class, $field, $type) = @_;
212  
213 9 ▶ if (! do { no strict 'refs'; defined &{"${class}::has"} } ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (do { no strict 'refs'; defined &{"${class}::has";} }) 6 23 29
214 6 require Moo;
215 6 ▶ my $ok = eval "package $class; use Moo; 1";
Subroutine BEGIN: called CC=1 SCAR=0.0
216 6 ▶ croak "Failed to import Moo into $class" if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 6 6
217 }
218  
219 29 my $private_reader = "_get_moocaf_$field";
220 29 my $private_writer = "_set_moocaf_$field";
221  
222 29 ▶ if (!$class->can($private_reader)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can($private_reader)) 26 3 29
223 26 $class->can('has')->(
224 $field,
225 is => 'rw',
226 reader => $private_reader,
227 writer => $private_writer,
228 );
229  
230 install_modifier(
231 $class, 'around', $private_writer,
232 7 ▶ sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
233 7 my $orig = shift;
234 7 my $self = shift;
235 7 ▶ return $self->$orig() if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
236 7 ▶ my $value = (@_>1) ? [@_] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 6 7
237 7 $self->$orig( $value );
238 7 return $self;
239 },
240 26 );
241 }
242  
243 29 my $reader = $class->accessor_name_for( $field );
244 29 my $writer = $class->mutator_name_for( $field );
245  
246 29 ▶ $reader = undef if $type eq 'wo';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "wo" 8 21 29
247 29 ▶ $writer = undef if $type eq 'ro';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "ro" 8 21 29
248  
249 29 my $stash = Package::Stash->new( $class );
250  
251 29 ▶ if (($reader and $writer) and ($reader eq $writer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader and $writer and $reader eq $writer) { } 10 19 29
Condition100%6/6
$reader and $writer and $reader eq $writer
!ll&&!rl&&r
16310
$reader and $writer
!ll&&!rl&&r
8813
MC/DC100%3/3
$reader and $writer and $reader eq $writer
$reader $writer $reader eq $writer
Truth table100%4/4
A and B and C
A= $readerB= $writerC= $reader eq $writer
ABCresult
0XX0
10X0
1100
1111
252 $stash->add_symbol(
253 '&' . $reader,
254 8 ▶ sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
255 8 my $self = shift;
256 8 ▶ return $self->$private_reader() if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 4 8
257 4 return $self->$private_writer( @_ );
258 },
259 10 ▶ ) if !$stash->has_symbol('&' . $reader);
Branch: 1 branch
BranchTrueFalseTotal
unless $stash->has_symbol('&' . $reader) 8 2 10
260 }
261 else {
262 $stash->add_symbol(
263 '&' . $reader,
264 1 ▶ sub{ shift()->$private_reader( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
265 19 ▶ ) if $reader and !$stash->has_symbol('&' . $reader);
Branch: 1 branch
BranchTrueFalseTotal
if $reader and not $stash->has_symbol('&' . $reader) 10 9 19
Condition100%3/3
$reader and not $stash->has_symbol('&' . $reader)
!ll&&!rl&&r
8110
MC/DC100%2/2
$reader and not $stash->has_symbol('&' . $reader)
$reader not $stash->has_symbol('&' . $reader)
Truth table100%3/3
A and not(B)
A= $readerB= not $stash->has_symbol('&' . $reader)
ABresult
0X0
100
111
266  
267 $stash->add_symbol(
268 '&' . $writer,
269 1 ▶ sub{ shift()->$private_writer( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
270 19 ▶ ) if $writer and !$stash->has_symbol('&' . $writer);
Branch: 1 branch
BranchTrueFalseTotal
if $writer and not $stash->has_symbol('&' . $writer) 10 9 19
Condition100%3/3
$writer and not $stash->has_symbol('&' . $writer)
!ll&&!rl&&r
8110
MC/DC100%2/2
$writer and not $stash->has_symbol('&' . $writer)
$writer not $stash->has_symbol('&' . $writer)
Truth table100%3/3
A and not(B)
A= $writerB= not $stash->has_symbol('&' . $writer)
ABresult
0X0
100
111
271 }
272  
273 4 ▶ return sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
274 4 my $self = shift;
275 4 ▶ return $self->$private_reader( @_ ) unless @_ and $type ne 'wo';
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and $type ne "wo" 3 1 4
Condition66%2/3
@_ and $type ne "wo"
!ll&&!rl&&r
301
MC/DC50%1/2
@_ and $type ne "wo"
@_ $type ne "wo"
Truth table66%2/3
A and B
A= @_B= $type ne "wo"
ABresult
0X0
100
111
276 1 return $self->$private_writer( @_ );
277 29 };
278 }
279  
280 =head2 make_accessor
281  
282 my $coderef = $class->make_accessor( $field );
283  
284 See L<Class::Accessor/make_accessor>.
285  
286 =cut
287  
288 13 ▶sub make_accessor {
Subroutine make_accessor: called CC=1 SCAR=0.0
Pod documented
289 13 my ($class, $field) = @_;
290 13 return $class->_make_moocaf_accessor( $field, 'rw' );
291 }
292  
293 =head2 make_ro_accessor
294  
295 my $coderef = $class->make_ro_accessor( $field );
296  
297 See L<Class::Accessor/make_ro_accessor>.
298  
299 =cut
300  
301 8 ▶sub make_ro_accessor {
Subroutine make_ro_accessor: called CC=1 SCAR=0.0
Pod documented
302 8 my ($class, $field) = @_;
303 8 return $class->_make_moocaf_accessor( $field, 'ro' );
304 }
305  
306 =head2 make_wo_accessor
307  
308 my $coderef = $class->make_wo_accessor( $field );
309  
310 See L<Class::Accessor/make_wo_accessor>.
311  
312 =cut
313  
314 8 ▶sub make_wo_accessor {
Subroutine make_wo_accessor: called CC=1 SCAR=0.0
Pod documented
315 8 my ($class, $field) = @_;
316 8 return $class->_make_moocaf_accessor( $field, 'wo' );
317 }
318  
319 9 1;
320 __END__
↑ Summary Adopt/Class/Accessor/Fast.pm »