blib/lib/Raisin/Entity.pm

filter: Statementstmt 82.8% 92 / 111 Branchbran 48.1% 26 / 54 Conditioncond 39.3% 13 / 33 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 100% 19 / 19 Podpod 50.0% 1 / 2 totaltotal 64.9% 154 / 237 CC 41 SCAR 33.4
CC
41
Cov
66%
_make_exposition
64.8
compile
45.1
_compile_column
19.0
CRAP 28.3
↑ Summary blib/lib/Raisin/Plugin/Swagger.pm »
1 #!perl
2 #PODNAME: Raisin::Entity
3 #ABSTRACT: A simple facade to use with your API
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Raisin::Entity;
9 10 $Raisin::Entity::VERSION = '0.94';
10 10 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Types::Standard qw/HashRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use Raisin::Entity::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 our @EXPORT = qw(expose);
19  
20 10 my @SUBNAME;
21  
22 10 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
23 {
24 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 my $class = caller;
27  
28 10 ▶ *{ "${class}::name" } = sub { $class };
Subroutine __ANON__: called CC=1 SCAR=0.0
29 # A kind of a workaround for OpenAPI
30 # Every entity has a HashRef type even if it is not, so it could cause
31 # issues for users in OpenAPI specification.
32 10 ▶ *{ "${class}::type" } = sub { HashRef };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 10 ▶ *{ "${class}::enclosed" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 4 \@{ "${class}::EXPOSE" };
36 10 };
37 }
38  
39 10 Raisin::Entity->export_to_level(1, @_);
40 }
41  
42 34 ▶sub expose {
Subroutine expose: called CC=3 SCAR=11.0
Pod documented
43 34 my ($name, @params) = @_;
44  
45 34 my $class = caller;
46 34 ▶ if (scalar @SUBNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @SUBNAME) 3 31 34
47 3 $class = 'Raisin::Entity::Nested::' . join('', @SUBNAME);
48 }
49  
50 {
51 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 34 push @{ "${class}::EXPOSE" }, Raisin::Entity::Object->new($name, @params);
53 }
54  
55 34 ▶ return $class if scalar @SUBNAME;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @SUBNAME 3 31 34
56 }
57  
58 26 ▶sub compile {
Subroutine compile: called CC=14 SCAR=38.1
Pod undocumented
59 26 my ($self, $entity, $data) = @_;
60  
61 26 my @expose = do {
62 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 26 @{ "${entity}::EXPOSE" };
64 };
65  
66 26 ▶ @expose = _make_exposition($data) unless @expose;
Branch: 1 branch
BranchTrueFalseTotal
unless @expose 12 14 26
67 26 ▶ return $data unless @expose;
Branch: 1 branch
BranchTrueFalseTotal
unless @expose 12 14 26
68  
69 14 my $result;
70  
71 # Rose::DB::Object::Iterator, DBIx::Class::ResultSet
72 14 ▶ if (blessed($data) && $data->can('next')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->can('next')) { } 0 14 14
elsif (ref $data eq 'ARRAY') { } 0 14 14
elsif (ref $data eq 'HASH' or builtin::blessed($data) and $data->isa('Rose::DB::Object') || $data->isa('DBIx::Class::Core')) { } 14 0 14
Condition33%1/3
builtin::blessed($data) and $data->can("next")
!ll&&!rl&&r
1400
MC/DC0%0/2
builtin::blessed($data) and $data->can("next")
builtin::blessed($data) $data->can("next")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->can("next")
ABresult
0X0
100
111
73 0 ▶ while (my $i = $data->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $data->next) 0 0 0
74 0 push @$result, _compile_column($entity, $i, \@expose);
75 }
76  
77 0 ▶ $result = [] unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
78 }
79 # Array
80 elsif (ref($data) eq 'ARRAY') {
81 0 for my $i (@$data) {
82 0 push @$result, _compile_column($entity, $i, \@expose);
83 }
84  
85 0 ▶ $result = [] unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
86 }
87 # Hash, Rose::DB::Object, DBIx::Class::Core
88 ▶ elsif (ref($data) eq 'HASH'
Condition11%1/9
ref $data eq "HASH" or builtin::blessed($data) and $data->isa("Rose::DB::Object") || $data->isa("DBIx::Class::Core")
l!l&&r!l&&!r
1400
builtin::blessed($data) and $data->isa("Rose::DB::Object") || $data->isa("DBIx::Class::Core")
!ll&&!rl&&r
000
$data->isa("Rose::DB::Object") or $data->isa("DBIx::Class::Core")
l!l&&r!l&&!r
000
MC/DC0%0/4
ref $data eq "HASH" or builtin::blessed($data) and $data->isa("Rose::DB::Object") || $data->isa("DBIx::Class::Core")
ref $data eq "HASH" builtin::blessed($data) $data->isa("Rose::DB::Object") $data->isa("DBIx::Class::Core")
Truth table20%1/5
A or B and C or D
A= ref $data eq "HASH"B= builtin::blessed($data)C= $data->isa("Rose::DB::Object")D= $data->isa("DBIx::Class::Core")
ABCDresult
1XXX1
011X1
01011
00XX0
01000
89 || (blessed($data)
90 && ( $data->isa('Rose::DB::Object')
91 || $data->isa('DBIx::Class::Core'))))
92 {
93 14 $result = _compile_column($entity, $data, \@expose);
94 }
95 # Scalar, everything else
96 else {
97 0 $result = $data;
98 }
99  
100 14 $result;
101 }
102  
103 23 ▶sub _compile_column {
Subroutine _compile_column: called CC=16 SCAR=29.4
104 23 my ($entity, $data, $settings) = @_;
105 23 my %result;
106  
107 23 for my $obj (@$settings) {
108  
109 26 ▶ next if blessed($obj) && $obj->condition && !$obj->condition->($data);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($obj) and $obj->condition and not $obj->condition->($data) 0 26 26
Condition66%4/6
builtin::blessed($obj) and $obj->condition and not $obj->condition->($data)
!ll&&!rl&&r
2420
builtin::blessed($obj) and $obj->condition
!ll&&!rl&&r
0242
MC/DC0%0/3
builtin::blessed($obj) and $obj->condition and not $obj->condition->($data)
builtin::blessed($obj) $obj->condition not $obj->condition->($data)
Truth table50%2/4
A and B and not(C)
A= builtin::blessed($obj)B= $obj->conditionC= not $obj->condition->($data)
ABCresult
0XX0
10X0
1100
1111
110  
111 26 ▶ my $column = blessed($obj) ? $obj->name : $obj->{name};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) ? : 26 0 26
112  
113 26 ▶ my $key = blessed($obj) ? $obj->display_name : $obj->{name};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($obj) ? : 26 0 26
114 26 my $value = do {
115 26 ▶ if (blessed($obj) and my $runtime = $obj->runtime) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($obj) and my $runtime = $obj->runtime) { } 5 21 26
elsif (builtin::blessed($obj) and my $e = $obj->using) { } 2 19 21
Condition66%2/3
builtin::blessed($obj) and my $runtime = $obj->runtime
!ll&&!rl&&r
0215
MC/DC50%1/2
builtin::blessed($obj) and my $runtime = $obj->runtime
builtin::blessed($obj) my $runtime = $obj->runtime
Truth table66%2/3
A and B
A= builtin::blessed($obj)B= my $runtime = $obj->runtime
ABresult
0X0
100
111
116  
117 5 push @SUBNAME, "${entity}::$column";
118 5 my $retval = $runtime->($data);
119 5 pop @SUBNAME;
120  
121 5 ▶ if ($retval && !ref($retval) && $retval =~ /^Raisin::Entity::Nested::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval and not ref $retval and $retval =~ /^Raisin::Entity::Nested::/) 3 2 5
Condition50%3/6
$retval and not ref $retval and $retval =~ /^Raisin::Entity::Nested::/
!ll&&!rl&&r
023
$retval and not ref $retval
!ll&&!rl&&r
005
MC/DC33%1/3
$retval and not ref $retval and $retval =~ /^Raisin::Entity::Nested::/
$retval not ref $retval $retval =~ /^Raisin::Entity::Nested::/
Truth table50%2/4
A and not(B) and C
A= $retvalB= not ref $retvalC= $retval =~ /^Raisin::Entity::Nested::/
ABCresult
0XX0
10X0
1100
1111
122 3 $retval = __PACKAGE__->compile($retval, $data);
123 }
124  
125 5 $retval;
126 }
127 ▶ elsif (blessed($obj) and my $e = $obj->using) {
Condition66%2/3
builtin::blessed($obj) and my $e = $obj->using
!ll&&!rl&&r
0192
MC/DC50%1/2
builtin::blessed($obj) and my $e = $obj->using
builtin::blessed($obj) my $e = $obj->using
Truth table66%2/3
A and B
A= builtin::blessed($obj)B= my $e = $obj->using
ABresult
0X0
100
111
128 2 ▶ my $in = blessed($data) ? $data->$column : $data->{$column};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 0 2 2
129 2 __PACKAGE__->compile($e, $in);
130 }
131 else {
132 19 ▶ blessed($data) ? $data->$column : $data->{$column};
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($data) ? : 0 19 19
133 }
134 };
135  
136 26 $result{$key} = $value;
137 }
138  
139 23 \%result;
140 }
141  
142 21 ▶sub _make_exposition {
Subroutine _make_exposition: called CC=11 SCAR=41.7
143 21 my $data = shift;
144  
145 21 my @columns = do {
146 21 ▶ if (blessed($data)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($data)) { } 0 21 21
elsif (ref $data eq 'ARRAY') { } 0 21 21
elsif (ref $data eq 'HASH') { } 21 0 21
147 0 ▶ if ($data->isa('DBIx::Class::ResultSet')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($data->isa('DBIx::Class::ResultSet')) { } 0 0 0
elsif ($data->isa('DBIx::Class::Core')) { } 0 0 0
elsif ($data->isa('Rose::DB::Object')) { } 0 0 0
elsif ($data->isa('Rose::DB::Object::Iterator')) { } 0 0 0
148 0 keys %{ $data->result_class->columns_info };
149 }
150 elsif ($data->isa('DBIx::Class::Core')) {
151 0 keys %{ $data->columns_info };
152 }
153 elsif ($data->isa('Rose::DB::Object')) {
154 0 $data->meta->column_names;
155 }
156 elsif ($data->isa('Rose::DB::Object::Iterator')) {
157 0 croak 'Rose::DB::Object::Iterator isn\'t supported';
158 }
159 }
160 elsif (ref($data) eq 'ARRAY') {
161 0 ▶ if (blessed($data->[0]) && $data->[0]->isa('Rose::DB::Object')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data->[0]) and $data->[0]->isa('Rose::DB::Object')) { } 0 0 0
Condition0%0/3
builtin::blessed($data->[0]) and $data->[0]->isa("Rose::DB::Object")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($data->[0]) and $data->[0]->isa("Rose::DB::Object")
builtin::blessed($data->[0]) $data->[0]->isa("Rose::DB::Object")
Truth table0%0/3
A and B
A= builtin::blessed($data->[0])B= $data->[0]->isa("Rose::DB::Object")
ABresult
0X0
100
111
162 0 $data->[0]->meta->column_names;
163 }
164 else {
165 0 ();
166 }
167 }
168 elsif (ref($data) eq 'HASH') {
169 21 ();
170 }
171 };
172  
173 21 ▶ return if not @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 21 0 21
174 0 map { { name => $_ } } @columns;
175 }
176  
177 10 1;
178  
179 __END__
↑ Summary blib/lib/Raisin/Plugin/Swagger.pm »