blib/lib/Simple/Accessor.pm

filter: Statementstmt 96.6% 57 / 59 Branchbran 76.9% 20 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 91.4% 86 / 94 SCAR 29.1
CC
18
Cov
91%
__ANON__
9.0
__ANON__
6.5
_add_accessors
4.0
CRAP 18.3
↑ Summary
1 package Simple::Accessor;
2 2 $Simple::Accessor::VERSION = '1.11';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: a light and simple way to provide accessor in perl
7  
8 =head1 NAME
9 Simple::Accessor - very simple, light and powerful accessor
10  
11 =head1 VERSION
12  
13 version 1.11
14  
15 =head1 DESCRIPTION
16  
17 Simple::Accessor provides a simple object layer without any dependency.
18 It can be used where other ORM could be considered too heavy.
19 But it has also the main advantage to only need one single line of code.
20  
21 It can be easily used in scripts...
22  
23 =head1 Usage
24  
25 Create a package and just call Simple::Accessor.
26 The new method will be imported for you, and all accessors will be directly
27 accessible.
28  
29 package MyClass;
30  
31 # that s all what you need ! no more line required
32 use Simple::Accessor qw{foo bar cherry apple};
33  
34 You can now call 'new' on your class, and create objects using these attributes
35
36 package main;
37 use MyClass;
38  
39 my $o = MyClass->new()
40 or MyClass->new(bar => 42)
41 or MyClass->new(apple => 'fruit', cherry => 'fruit', banana => 'yummy');
42  
43 You can get / set any value using the accessor
44
45 is $o->bar(), 42;
46 $o->bar(51);
47 is $o->bar(), 51;
48
49 You can provide your own init method that will be call by new with default args.
50 This is optional.
51  
52 package MyClass;
53  
54 sub build { # previously known as initialize
55 my ($self, %opts) = @_;
56
57 $self->foo(12345);
58 }
59  
60 You can also control the object after or before its creation using
61  
62 sub _before_build {
63 my ($self, %opts) = @_;
64 ...
65 }
66  
67 sub _after_build {
68 my ($self, %opts) = @_;
69 ...
70 bless $self, 'Basket';
71 }
72  
73 You can also provide individual builders / initializers
74  
75 sub _build_bar { # previously known as _initialize_bar
76 # will be used if no value has been provided for bar
77 1031;
78 }
79  
80 sub _build_cherry {
81 'red';
82 }
83  
84 You can even use a very basic but useful hook system.
85 Any false value return by before or validate, will stop the setting process.
86 Be careful with the after method, as there is no protection against infinite loop.
87  
88 sub _before_foo {
89 my ($self, $v) = @_;
90
91 # do whatever you want with $v
92 return 1 or 0;
93 }
94  
95 sub _validate_foo {
96 my ($self, $v) = @_;
97 # invalid value ( will not be set )
98 return 0 if ( $v == 42);
99 # valid value
100 return 1;
101 }
102  
103 sub _after_cherry {
104 my ($self) = @_;
105
106 # use the set value for extra operations
107 $self->apple($self->cherry());
108 }
109
110 =head1 METHODS
111  
112 None. The only public method provided is the classical import.
113  
114 =cut
115  
116 sub import {
117 3 ▶ my ( $class, @attr ) = @_;
Subroutine import: called CC=1 SCAR=0.0
118  
119 3 my $from = caller();
120  
121 3 _add_new($from);
122 3 _add_accessors( to => $from, attributes => \@attr );
123 }
124  
125 sub _add_new {
126 3 ▶ my $class = shift;
Subroutine _add_new: called CC=2 SCAR=7.1
127 3 ▶ return unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 3 3
128  
129 3 my $new = $class . '::new';
130 {
131 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 *$new = sub {
133 8 ▶ my ( $class, %opts ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.7
134  
135 8 my $self = bless {}, $class;
136  
137 # set values if attributes exist
138 map {
139 8 eval { $self->$_( $opts{$_} ) }
140 } keys %opts;
141  
142 8 ▶ if ( $self->can( '_before_build') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_before_build")) 0 8 8
143 0 $self->_before_build( %opts );
144 }
145  
146 8 foreach my $init ( 'build', 'initialize' ) {
147 16 ▶ if ( $self->can( $init ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($init)) 4 12 16
148 4 ▶ return unless $self->$init(%opts);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$init(%opts) 0 4 4
149 }
150 }
151  
152 8 ▶ if ( $self->can( '_after_build') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_after_build")) 0 8 8
153 0 $self->_after_build( %opts );
154 }
155  
156 8 return $self;
157 3 };
158 }
159 }
160  
161 sub _add_accessors {
162 3 ▶ my (%opts) = @_;
Subroutine _add_accessors: called CC=4 SCAR=13.9
163  
164 3 ▶ return unless $opts{to};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"to"} 0 3 3
165 3 my @attributes = @{ $opts{attributes} };
166 3 ▶ return unless @attributes;
Branch: 1 branch
BranchTrueFalseTotal
unless @attributes 0 3 3
167  
168 3 foreach my $att (@attributes) {
169 10 my $accessor = $opts{to} . "::$att";
170  
171 # allow symbolic refs to typeglob
172 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 *$accessor = sub {
174 34 ▶ my ( $self, $v ) = @_;
Subroutine __ANON__: called CC=9 SCAR=22.0
175 34 ▶ if ( defined $v ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $v) { } 15 19 34
elsif (not defined $self->{$att}) { } 7 12 19
176 15 foreach (qw{before validate set after}) {
177 58 ▶ if ( $_ eq 'set' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "set") 14 44 58
178 14 $self->{$att} = $v;
179 14 next;
180 }
181 44 my $sub = '_' . $_ . '_' . $att;
182 44 ▶ if ( $self->can( $sub ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($sub)) 4 40 44
183 4 ▶ return unless $self->$sub($v);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$sub($v) 1 3 4
184 }
185 }
186 }
187 elsif ( !defined $self->{$att} ) {
188 # try to initialize the value (try first with build)
189 # initialize is here for backward compatibility with older versions
190 7 foreach my $builder ( qw{build initialize} ) {
191 14 my $sub = '_' . $builder . '_' . $att;
192 14 ▶ if ( $self->can( $sub ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($sub)) 1 13 14
193 1 return $self->{$att} = $self->$sub();
194 }
195 }
196 }
197  
198 32 return $self->{$att};
199 10 };
200 }
201 3 @attributes = ();
202 }
203  
204 2 1;
205  
206 =head1 CONTRIBUTE
207  
208 You can contribute to this project on github https://github.com/atoomic/Simple-Accessor
209  
210 =cut
211  
212 __END__
↑ Summary