blib/lib/GitInsight/Obj.pm

filter: Statementstmt 54.2% 32 / 59 Branchbran 21.8% 7 / 32 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 54.5% 6 / 11 Podpod 0.0% 0 / 3 totaltotal 34.8% 45 / 129 SCAR 52.1
CC
25
Cov
37%
attr
182.0
new
20.0
import
12.3
CRAP 182.8
↑ Summary blib/lib/GitInsight/Util.pm »
1 package GitInsight::Obj;
2  
3 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 #use feature ();
8  
9 4 our $feature = eval {
10 4 require feature;
11 4 feature->import();
12 0 1;
13 };
14  
15  
16 # Only Perl 5.14+ requires it on demand
17 4 ▶ use IO::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Protect subclasses using AUTOLOAD
20 ▶ sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
21  
22 sub import {
23 4 ▶ my $class = shift;
Subroutine import: called CC=9 SCAR=25.1
24 4 ▶ return unless my $flag = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $flag = shift() 0 4 4
25  
26 # Base
27 4 ▶ if ( $flag eq '-base' ) { $flag = $class }
Branch: 3 branches
BranchTrueFalseTotal
if ($flag eq '-base') { } 2 2 4
elsif ($flag eq '-strict') { } 2 0 2
elsif (my $file = $flag and not $flag->can('new')) { } 0 0 0
28  
29 # Strict
30 2 elsif ( $flag eq '-strict' ) { $flag = undef }
31  
32 # Module
33 ▶ elsif ( ( my $file = $flag ) && !$flag->can('new') ) {
Condition0%0/3
my $file = $flag and not $flag->can("new")
!ll&&!rl&&r
000
MC/DC0%0/2
my $file = $flag and not $flag->can("new")
my $file = $flag not $flag->can("new")
Truth table0%0/3
A and not(B)
A= my $file = $flagB= not $flag->can("new")
ABresult
0X0
100
111
34 0 $file =~ s!::|'!/!g;
35 0 require "$file.pm";
36 }
37  
38 # ISA
39 4 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 2 2 4
40 2 my $caller = caller;
41 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 push @{"${caller}::ISA"}, $flag;
43 2 ▶ *{"${caller}::has"} = sub { attr( $caller, @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
44 }
45  
46 # Mojo modules are strict!
47 4 $_->import for qw(strict warnings utf8);
48 4 ▶ if ($feature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($GitInsight::Obj::feature) 0 4 4
49 0 feature->import(':5.10');
50 }
51 }
52  
53 sub attr {
54 0 ▶ my ( $self, $attrs, $default ) = @_;
Subroutine attr: not called CC=13 SCAR=52.0
Pod undocumented
55 0 ▶ return unless ( my $class = ref $self || $self ) && $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless my $class = ref $self || $self and $attrs 0 0 0
Condition0%0/6
my $class = ref $self || $self and $attrs
!ll&&!rl&&r
000
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/3
my $class = ref $self || $self and $attrs
ref $self $self $attrs
Truth table0%0/5
A || B and C
A= ref $selfB= $selfC= $attrs
ABCresult
00X0
1X00
0100
1X11
0111
56  
57 0 ▶ die 'Default has to be a code reference or constant value'
Branch: 1 branch
BranchTrueFalseTotal
if ref $default and ref $default ne "CODE" 0 0 0
Condition0%0/3
ref $default and ref $default ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $default and ref $default ne "CODE"
ref $default ref $default ne "CODE"
Truth table0%0/3
A and B
A= ref $defaultB= ref $default ne "CODE"
ABresult
0X0
100
111
58 if ref $default && ref $default ne 'CODE';
59  
60 0 ▶ for my $attr ( @{ ref $attrs eq 'ARRAY' ? $attrs : [$attrs] } ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $attrs eq 'ARRAY' ? : 0 0 0
61 0 ▶ die qq{Attribute "$attr" invalid}
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^[a-zA-Z_]\w*$/ 0 0 0
62 unless $attr =~ /^[a-zA-Z_]\w*$/;
63  
64 # Header (check arguments)
65 0 my $code = "package $class;\nsub $attr {\n if (\@_ == 1) {\n";
66  
67 # No default value (return value)
68 0 ▶ unless ( defined $default ) { $code .= " return \$_[0]{'$attr'};" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 0 0 0
69  
70 # Default value
71 else {
72  
73 # Return value
74 0 $code
75 .= " return \$_[0]{'$attr'} if exists \$_[0]{'$attr'};\n";
76  
77 # Return default value
78 0 $code .= " return \$_[0]{'$attr'} = ";
79 0 ▶ $code .=
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 0 0 0
80 ref $default eq 'CODE'
81 ? '$default->($_[0]);'
82 : '$default;';
83 }
84  
85 # Store value
86 0 $code .= "\n }\n \$_[0]{'$attr'} = \$_[1];\n";
87  
88 # Footer (return invocant)
89 0 $code .= " \$_[0];\n}";
90  
91 warn "-- Attribute $attr in $class\n$code\n\n"
92 0 ▶ if $ENV{GitInsight_OBJ_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GitInsight_OBJ_DEBUG"} 0 0 0
93 0 ▶ die "GitInsight::Obj error: $@" unless eval "$code;1";
Branch: 1 branch
BranchTrueFalseTotal
unless eval "$code;1" 0 0 0
94 }
95 }
96  
97 sub new {
98 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
99 0 ▶ bless @_ ? @_ > 1 ? {@_} : { %{ $_[0] } } : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
@_ > 1 ? : 0 0 0
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
100 }
101  
102 sub tap {
103 0 ▶ my ( $self, $cb ) = @_;
Subroutine tap: not called CC=2 SCAR=17.9
Pod undocumented
104 0 $_->$cb for $self;
105 0 return $self;
106 }
107  
108 4 1;
109 __END__
110  
111 =encoding utf8
112  
113 =head1 NAME
114  
115 GitInsight::Obj - Minimal base class for GitInsight
116  
117 =head1 SYNOPSIS
118  
119 package Cat;
120 use GitInsight::Obj -base;
121  
122 has name => 'Nyan';
123 has [qw(birds mice)] => 2;
124  
125 package Tiger;
126 use GitInsight::Obj 'Cat';
127  
128 has friend => sub { Cat->new };
129 has stripes => 42;
130  
131 package main;
132 use GitInsight::Obj -strict;
133  
134 my $mew = Cat->new(name => 'Longcat');
135 say $mew->mice;
136 say $mew->mice(3)->birds(4)->mice;
137  
138 my $rawr = Tiger->new(stripes => 23, mice => 0);
139 say $rawr->tap(sub { $_->friend->name('Tacgnol') })->mice;
140  
141 =head1 DESCRIPTION
142  
143 L<GitInsight::Obj> is a simple base class for L<GitInsight>, a fork of L<Mojo::Base>.
144  
145 # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
146 use GitInsight::Obj -strict;
147 use GitInsight::Obj -base;
148 use GitInsight::Obj 'SomeBaseClass';
149  
150 All three forms save a lot of typing.
151  
152 # use GitInsight::Obj -strict;
153 use strict;
154 use warnings;
155 use utf8;
156 use feature ':5.10';
157 use IO::Handle ();
158  
159 # use GitInsight::Obj -base;
160 use strict;
161 use warnings;
162 use utf8;
163 use feature ':5.10';
164 use IO::Handle ();
165 use GitInsight::Obj;
166 push @ISA, 'GitInsight::Obj';
167 sub has { GitInsight::Obj::attr(__PACKAGE__, @_) }
168  
169 # use GitInsight::Obj 'SomeBaseClass';
170 use strict;
171 use warnings;
172 use utf8;
173 use feature ':5.10';
174 use IO::Handle ();
175 require SomeBaseClass;
176 push @ISA, 'SomeBaseClass';
177 use GitInsight::Obj;
178 sub has { GitInsight::Obj::attr(__PACKAGE__, @_) }
179  
180 =head1 FUNCTIONS
181  
182 L<GitInsight::Obj> implements the following functions like L<Mojo::Base>, which can be imported with
183 the C<-base> flag or by setting a base class.
184  
185 =head2 has
186  
187 has 'name';
188 has [qw(name1 name2 name3)];
189 has name => 'foo';
190 has name => sub {...};
191 has [qw(name1 name2 name3)] => 'foo';
192 has [qw(name1 name2 name3)] => sub {...};
193  
194 Create attributes for hash-based objects, just like the L</"attr"> method.
195  
196 =head1 METHODS
197  
198 L<GitInsight::Obj> implements the following methods.
199  
200 =head2 attr
201  
202 $object->attr('name');
203 BaseSubClass->attr('name');
204 BaseSubClass->attr([qw(name1 name2 name3)]);
205 BaseSubClass->attr(name => 'foo');
206 BaseSubClass->attr(name => sub {...});
207 BaseSubClass->attr([qw(name1 name2 name3)] => 'foo');
208 BaseSubClass->attr([qw(name1 name2 name3)] => sub {...});
209  
210 Create attribute accessor for hash-based objects, an array reference can be
211 used to create more than one at a time. Pass an optional second argument to
212 set a default value, it should be a constant or a callback. The callback will
213 be executed at accessor read time if there's no set value. Accessors can be
214 chained, that means they return their invocant when they are called with an
215 argument.
216  
217 =head2 new
218  
219 my $object = BaseSubClass->new;
220 my $object = BaseSubClass->new(name => 'value');
221 my $object = BaseSubClass->new({name => 'value'});
222  
223 This base class provides a basic constructor for hash-based objects. You can
224 pass it either a hash or a hash reference with attribute values.
225  
226 =head2 tap
227  
228 $object = $object->tap(sub {...});
229  
230 K combinator, tap into a method chain to perform operations on an object
231 within the chain. The object will be the first argument passed to the callback
232 and is also available as C<$_>.
233  
234 =head1 DEBUGGING
235  
236 You can set the C<GitInsight_OBJ_DEBUG> environment variable to get some advanced
237 diagnostics information printed to C<STDERR>.
238  
239 GitInsight_OBJ_DEBUG=1
240  
241 =head1 SEE ALSO
242  
243 L<GitInsight>, L<Mojo::Base>.
244  
245 =cut
↑ Summary blib/lib/GitInsight/Util.pm »