blib/lib/Object/Enum.pm

filter: Statementstmt 100% 105 / 105 Branchbran 89.4% 34 / 38 Conditioncond 81.2% 13 / 16 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 24 / 24 Podpod 100% 6 / 6 totaltotal 94.4% 188 / 199 SCAR 33.0
CC
27
Cov
96%
new
12.0
value
5.0
_curried
4.0
CRAP 27.1
↑ Summary
1 package Object::Enum;
2 6 $Object::Enum::VERSION = '0.074';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Sub::Install ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 use base qw(
11 Class::Data::Inheritable
12 Class::Accessor::Fast
13 6 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 __PACKAGE__->mk_classdata($_) for (
16 '_values',
17 '_unset',
18 '_default',
19 '_readonly',
20 );
21  
22 6 __PACKAGE__->mk_accessors(
23 'value',
24 );
25  
26 6 __PACKAGE__->_unset(1);
27  
28 use overload (
29 6 q{""} => '_stringify',
30 fallback => 1,
31 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 6 use Sub::Exporter -setup => {
34 exports => [ Enum => \&_build_enum ],
35 6 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub _build_enum {
38 3 ▶ my ($class, undef, $arg) = @_;
Subroutine _build_enum: called CC=1 SCAR=0.0
39 3 ▶ return sub { $class->new({ %$arg, %{shift || {} } }) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
shift() or {} 6 5 11
40 }
41  
42 =head1 NAME
43  
44 Object::Enum - replacement for C<< if ($foo eq 'bar') >>
45  
46 =head1 SYNOPSIS
47  
48 use Object::Enum qw(Enum);
49  
50 my $color = Enum([ qw(red yellow green) ]);
51 # ... later
52 if ($color->is_red) {
53 # it can't be yellow or green
54  
55 =head1 EXPORTS
56  
57 See L<Sub::Exporter> for ways to customize this module's
58 exports.
59  
60 =head2 Enum
61  
62 An optional shortcut for C<< Object::Enum->new >>.
63  
64 =head1 CLASS METHODS
65  
66 =head2 new
67  
68 my $obj = Object::Enum->new(\@values);
69 # or
70 $obj = Object::Enum->new(\%arg);
71  
72 Return a new Object::Enum, with one or more sets of possible
73 values.
74  
75 The simplest case is to pass an arrayref, which returns an
76 object capable of having any one of the given values or of
77 being unset.
78  
79 The more complex cases involve passing a hashref, which may
80 have the following keys:
81  
82 =over
83  
84 =item * unset
85  
86 whether this object can be 'unset' (defaults to true)
87  
88 =item * default
89  
90 this object's default value is (defaults to undef)
91  
92 =item * values
93  
94 an arrayref, listing the object's possible values (at least
95 one required)
96  
97 =item * readonly
98  
99 boolean value to indicate if the object is read-only. If set
100 to read-only the objects C<value> and C<set_*> methods become ineffectual.
101  
102 =back
103  
104 =cut
105  
106 6 my $id = 0;
107 sub _generate_class {
108 11 ▶ my $class = shift;
Subroutine _generate_class: called CC=1 SCAR=0.0
109 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 11 my $gen = sprintf "%s::obj_%08d", $class, ++$id;
111 11 push @{$gen."::ISA"}, $class;
112 11 return $gen;
113 }
114  
115 sub _mk_values {
116 11 ▶ my $class = shift;
Subroutine _mk_values: called CC=3 SCAR=11.1
117 11 for my $value (keys %{ $class->_values }) {
118 Sub::Install::install_sub({
119 into => $class,
120 as => "set_$value",
121 8 ▶ code => sub { $_[0]->value($value); return $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
122 23 });
123 Sub::Install::install_sub({
124 into => $class,
125 as => "is_$value",
126 14 ▶ code => sub { (shift->value || '') eq $value },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
(shift())->value || ''
l!l
140
MC/DC0%0/1
(shift())->value || ''
(shift())->value
Truth table50%1/2
A
A= (shift())->value
Aresult
11
00
127 23 ▶ }) unless $class->can("is_$value");
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("is_$value") 23 0 23
128 }
129 }
130  
131 sub new {
132 14 ▶ my ($class, $arg) = @_;
Subroutine new: called CC=12 SCAR=24.9
Pod documented
133 14 ▶ $arg ||= [];
Condition50%1/2
$arg ||= []
l!l
140
MC/DC0%0/1
$arg ||= []
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
134 14 ▶ if (ref $arg eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq "ARRAY") 2 12 14
135 2 $arg = { values => $arg };
136 }
137  
138 14 ▶ unless (@{$arg->{values} || []}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@{$arg->{"values"} or [];}) 1 13 14
$arg->{"values"} or [] 1 13 14
139 1 Carp::croak("at least one possible value must be provided");
140 }
141  
142 13 ▶ exists $arg->{unset} or $arg->{unset} = 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{"unset"} or $arg->{"unset"} = 1 11 2 13
143 13 ▶ exists $arg->{default} or $arg->{default} = undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{"default"} or $arg->{"default"} = undef 6 7 13
144 13 ▶ exists $arg->{readonly} or $arg->{readonly} = 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{"readonly"} or $arg->{"readonly"} = 0 12 1 13
145  
146 13 ▶ if (!$arg->{unset} && !defined $arg->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->{"unset"} or defined $arg->{"default"}) 1 12 13
Condition100%3/3
$arg->{"unset"} or defined $arg->{"default"}
l!l&&r!l&&!r
1111
MC/DC100%2/2
$arg->{"unset"} or defined $arg->{"default"}
$arg->{"unset"} defined $arg->{"default"}
Truth table100%3/3
A or B
A= $arg->{"unset"}B= defined $arg->{"default"}
ABresult
1X1
011
000
147 1 Carp::croak("must supply a defined default for 'unset' to be false");
148 }
149  
150 12 ▶ if (defined($arg->{default}) && ! grep {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg->{"default"} and not grep({$_ eq $arg->{"default"};} @{$$arg{"values"};})) 1 11 12
Condition100%3/3
defined $arg->{"default"} and not grep({$_ eq $arg->{"default"};} @{$$arg{"values"};})
!ll&&!rl&&r
561
MC/DC100%2/2
defined $arg->{"default"} and not grep({$_ eq $arg->{"default"};} @{$$arg{"values"};})
defined $arg->{"default"} not grep({$_ eq $arg->{"default"};} @{$$arg{"values"};})
Truth table100%3/3
A and not(B)
A= defined $arg->{"default"}B= not grep({$_ eq $arg->{"default"};} @{$$arg{"values"};})
ABresult
0X0
100
111
151 $_ eq $arg->{default}
152 14 } @{$arg->{values}}) {
153 1 Carp::croak("default value must be listed in 'values' or undef");
154 }
155  
156 11 my $gen = $class->_generate_class;
157 11 $gen->_unset($arg->{unset});
158 11 $gen->_default($arg->{default});
159 11 $gen->_readonly($arg->{readonly});
160 11 $gen->_values({ map { $_ => 1 } @{$arg->{values}} });
161 11 $gen->_mk_values;
162  
163 # constructors shouldn't call cloners
164 #return $gen->spawn;
165 11 return $gen->_curried;
166 }
167  
168 sub _stringify {
169 9 ▶ my $self = shift;
Subroutine _stringify: called CC=2 SCAR=7.1
170 9 ▶ return '(undef)' unless defined $self->value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->value 0 9 9
171 9 return $self->value;
172 }
173  
174 =head1 OBJECT METHODS
175  
176 =head2 spawn
177  
178 =head2 clone
179  
180 my $new = $obj->clone;
181  
182 my $new = $obj->clone($value);
183  
184 Create a new Enum from an existing object, using the same arguments as were
185 originally passed to C<< new >> when that object was created.
186  
187 An optional value may be passed in; this is identical to (but more convenient
188 than) calling C<value> with the same argument on the newly cloned object.
189  
190 This method was formerly named C<spawn>. That name will still work but is
191 deprecated.
192  
193 =cut
194  
195 sub _curried {
196 18 ▶ my $class = shift;
Subroutine _curried: called CC=4 SCAR=13.9
197 18 ▶ my $self = bless {
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 7 11 18
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
7110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
198 value => ref($class)? $class->value : $class->_default,
199 } => ref($class) || $class;
200 18 ▶ $self->value(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 17 18
201  
202 18 return $self;
203 }
204  
205 sub clone {
206 7 ▶ my $self = shift->_curried(@_);
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
207 7 ▶ $self->_readonly(0)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_readonly 1 6 7
208 if $self->_readonly;
209  
210 7 return $self;
211 }
212  
213 6 ▶BEGIN { *spawn = \&clone }
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 =head2 readonly
216  
217 my $obj = $obj->readonly(\@values, $value)
218  
219 Creates a read-only enum object, also known as immutable. When enum objects are created
220 with this C<< set_* >> methods, and the C<value> method will become ineffectual.
221  
222 If you want a mutable version, simply clone the immutable version
223  
224 my $new_obj = $readonly_obj->clone;
225 $new_obj->set_red;
226  
227 =cut
228  
229 sub readonly {
230 1 ▶ my $class = shift;
Subroutine readonly: called CC=2 SCAR=7.0
Pod documented
231 1 my $values = shift;
232 1 my $value = shift;
233  
234 1 ▶ $values = []
Branch: 1 branch
BranchTrueFalseTotal
unless ref $values eq "ARRAY" 0 1 1
235 unless ref($values) eq 'ARRAY';
236  
237 1 return $class->new({
238 values => $values,
239 default => $value,
240 readonly => 1
241 });
242 }
243  
244 =head2 value
245  
246 The current value as a string (or undef)
247  
248 Note: don't pass in undef; use the L<unset|/unset> method instead.
249  
250 =cut
251  
252 sub value {
253 59 ▶ my $self = shift;
Subroutine value: called CC=5 SCAR=16.1
Pod documented
254 59 ▶ if (@_ && !$self->_readonly) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and not $self->_readonly) 10 49 59
Condition100%3/3
@_ and not $self->_readonly
!ll&&!rl&&r
48110
MC/DC100%2/2
@_ and not $self->_readonly
@_ not $self->_readonly
Truth table100%3/3
A and not(B)
A= @_B= not $self->_readonly
ABresult
0X0
100
111
255 10 my $val = shift;
256 10 ▶ Carp::croak("object $self cannot be set to undef") unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 10 10
257 10 ▶ unless ($self->_values->{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_values->{$val}) 1 9 10
258 1 Carp::croak("object $self cannot be set to '$val'");
259 }
260 9 return $self->_value_accessor($val);
261 }
262 49 return $self->_value_accessor;
263 }
264  
265 =head2 values
266  
267 The possible values for this object
268  
269 =cut
270  
271 sub values {
272 1 ▶ my $self = shift;
Subroutine values: called CC=1 SCAR=0.0
Pod documented
273 1 return keys %{ $self->_values };
274 }
275  
276 =head2 unset
277  
278 Unset the object's value (set to undef)
279  
280 =cut
281  
282 sub unset {
283 3 ▶ my $self = shift;
Subroutine unset: called CC=2 SCAR=6.9
Pod documented
284 3 ▶ unless ($self->_unset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_unset) 2 1 3
285 2 Carp::croak("object $self cannot be unset");
286 }
287 1 $self->_value_accessor(undef);
288 }
289  
290 =head2 is_*
291  
292 =head2 set_*
293  
294 Automatically generated from the values passed into C<< new
295 >>.
296  
297 None of these methods take any arguments.
298  
299 The C<< set_* >> methods are chainable; that is, they return
300 the object on which they were called. This lets you do useful things like:
301  
302 use Object::Enum Enum => { -as => 'color', values => [qw(red blue)] };
303  
304 print color->set_red->value; # prints 'red'
305  
306 =cut
307  
308 =head1 AUTHOR
309  
310 Hans Dieter Pearcey, C<< <hdp at cpan.org> >>
311  
312 =head1 BUGS
313  
314 Please report any bugs or feature requests to
315 C<bug-object-enum at rt.cpan.org>, or through the web interface at
316 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Enum>.
317 I will be notified, and then you'll automatically be notified of progress on
318 your bug as I make changes.
319  
320 =head1 SUPPORT
321  
322 You can find documentation for this module with the perldoc command.
323  
324 perldoc Object::Enum
325  
326 You can also look for information at:
327  
328 =over 4
329  
330 =item * AnnoCPAN: Annotated CPAN documentation
331  
332 L<http://annocpan.org/dist/Object-Enum>
333  
334 =item * CPAN Ratings
335  
336 L<http://cpanratings.perl.org/d/Object-Enum>
337  
338 =item * RT: CPAN's request tracker
339  
340 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-Enum>
341  
342 =item * Search CPAN
343  
344 L<http://search.cpan.org/dist/Object-Enum>
345  
346 =item * GitHub
347  
348 L<https://github.com/jmmills/object-enum/>
349  
350 =back
351  
352 =head1 ACKNOWLEDGEMENTS
353  
354 =head1 COPYRIGHT & LICENSE
355  
356 Copyright 2006 Hans Dieter Pearcey, all rights reserved.
357  
358 This program is free software; you can redistribute it and/or modify it
359 under the same terms as Perl itself.
360  
361 =cut
362  
363 6 1; # End of Object::Enum
↑ Summary