ArangoDB2/Base.pm

filter: Statementstmt 43.8% 43 / 98 Branchbran 16.6% 10 / 60 Conditioncond 14.2% 3 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 59.0% 13 / 22 Podpod 100% 10 / 10 totaltotal 35.2% 79 / 224 CC 40 SCAR 37.4
CC
40
Cov
31%
_build_self
132.0
_build_args
72.0
_get_set_bool
42.0
CRAP 42.0
« ArangoDB2/HTTP/LWP.pm ↑ Summary ArangoDB2/Graph/Vertex.pm »
1 package ArangoDB2::Base;
2  
3 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 22 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 # new
14 #
15 # Arango organizes data hierarchically: Databases > Collections > Documents
16 #
17 # This constructor can build ArangoDB2::Database, Collection, Document, Edge,
18 # etc. objects which all follow the same pattern
19 sub new
20 53 ▶{
Subroutine new: called CC=5 SCAR=16.2
Pod documented
21 53 my $class = shift;
22 # new instance
23 53 my $self = {};
24 53 bless($self, $class);
25 # process args
26 53 ▶ while (my $arg = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift()) 124 53 177
27 # if arg is a ref it should be another
28 # ArangoDB::* object
29 124 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 99 25 124
30 # prevent circular ref
31 99 weaken $arg;
32 # create reference to parent object
33 99 $self->{$arg->_class} = $arg;
34 }
35 # if arg is a string then it is the "name"
36 # of this object
37 else {
38 25 $self->name($arg);
39 }
40 }
41 # if we have a name and can get then try it
42 53 ▶ $self->get if defined $self->name
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name and $self->can("get") 0 53 53
Condition66%2/3
defined $self->name and $self->can("get")
!ll&&!rl&&r
28250
MC/DC0%0/2
defined $self->name and $self->can("get")
defined $self->name $self->can("get")
Truth table66%2/3
A and B
A= defined $self->nameB= $self->can("get")
ABresult
0X0
100
111
43 and $self->can('get');
44  
45 53 return $self;
46 }
47  
48 # api_path
49 #
50 # return /_db/<db name>/_api
51 sub api_path
52 0 ▶{
Subroutine api_path: not called CC=2 SCAR=17.9
Pod documented
53 0 my $self = shift;
54  
55 0 ▶ my $db_name
Branch: 1 branch
BranchTrueFalseTotal
$self->database ? : 0 0 0
56 = $self->database
57 ? $self->database->name
58 : $self->name;
59  
60 0 return '/' . join('/', '_db', $db_name, '_api', @_);
61 }
62  
63 # arango
64 #
65 # ArangoDB2 instance
66 31 ▶sub arango { $_[0]->{arango} }
Subroutine arango: called CC=1 SCAR=0.0
Pod documented
67  
68 # collection
69 #
70 # parent ArangoDB2::Collection instance
71 0 ▶sub collection {
Subroutine collection: not called CC=3 SCAR=24.8
Pod documented
72 0 my($self, $value) = @_;
73  
74 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
75 # if value is already an object then set it
76 0 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 0 0
77 0 $self->{collection} = $value;
78 }
79 # otherwise treat as name and get collection object
80 else {
81 0 $self->{collection} = $self->database->collection($value);
82 }
83 }
84  
85 0 return $self->{collection};
86 }
87  
88 # data
89 #
90 # ref to hash containing document data
91 0 ▶sub data { shift->_get_set('data', @_) }
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
92  
93 # database
94 #
95 # parent ArangoDB2::Database instance
96 12 ▶sub database { $_[0]->{database} }
Subroutine database: called CC=1 SCAR=0.0
Pod documented
97  
98 # graph
99 #
100 # parent ArangoDB2::Graph instance
101 2 ▶sub graph { $_[0]->{graph} }
Subroutine graph: called CC=1 SCAR=0.0
Pod documented
102  
103 # id
104 #
105 # ArangoDB _id
106 0 ▶sub id { $_[0]->{id} }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
107  
108 # name
109 #
110 # name/handle of object
111 78 ▶sub name { shift->_get_set_name('name', @_) }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
112  
113 # rev
114 #
115 # ArangoDB _rev
116 0 ▶sub rev { $_[0]->{rev} }
Subroutine rev: not called CC=1 SCAR=6.9
Pod documented
117  
118 # _build_args
119 #
120 # process args for requests
121 sub _build_args
122 0 ▶{
Subroutine _build_args: not called CC=8 SCAR=42.8
123 0 my($self, $args, $params) = @_;
124 # require hash ref for args
125 0 ▶ $args = {} unless defined $args
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args and ref $args and ref $args eq "HASH" 0 0 0
Condition0%0/6
defined $args and ref $args and ref $args eq "HASH"
!ll&&!rl&&r
000
defined $args and ref $args
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args and ref $args and ref $args eq "HASH"
defined $args ref $args ref $args eq "HASH"
Truth table0%0/4
A and B and C
A= defined $argsB= ref $argsC= ref $args eq "HASH"
ABCresult
0XX0
10X0
1100
1111
126 and ref $args and ref $args eq 'HASH';
127 # if an explicit list is not passed for this call
128 # then use global list for class
129 0 ▶ $params ||= $self->_params;
Condition0%0/3
$params ||= $self->_params
l!l&&r!l&&!r
000
MC/DC0%0/2
$params ||= $self->_params
$params $self->_params
Truth table0%0/3
A ||= B
A= $paramsB= $self->_params
ABresult
1X1
011
000
130 # for each of the allowed parameters use the
131 # setter to set the value if it is passed and
132 # and the value to the request args if it is
133 # defined
134 0 for my $param (@$params) {
135 # if params is pased in args then it supersedes
136 # any value set as a property of the object
137 0 ▶ if (exists $args->{$param}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{$param}) { } 0 0 0
elsif (defined $self->$param) { } 0 0 0
138 # run arg through setter with validate only
139 # flag which will return the validated arg
140 0 $args->{$param} = $self->$param($args->{$param}, 1);
141 }
142 # otherwise if the param is set then add to args
143 elsif (defined $self->$param) {
144 0 $args->{$param} = $self->$param;
145 }
146 }
147  
148 0 return $args;
149 }
150  
151 # _build_self
152 #
153 # copy param values from passed data to self
154 sub _build_self
155 0 ▶{
Subroutine _build_self: not called CC=11 SCAR=48.8
156 0 my($self, $data, $params) = @_;
157 # require data
158 0 ▶ croak "Invalid Args"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 0 0
159 unless ref $data eq 'HASH';
160 # if an explicit list is not passed for this call
161 # then use global list for class
162 0 ▶ $params ||= $self->_params;
Condition0%0/3
$params ||= $self->_params
l!l&&r!l&&!r
000
MC/DC0%0/2
$params ||= $self->_params
$params $self->_params
Truth table0%0/3
A ||= B
A= $paramsB= $self->_params
ABresult
1X1
011
000
163 # copy params
164 0 for my $param (@$params) {
165 $self->{$param} = delete $data->{$param}
166 0 ▶ if exists $data->{$param};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$param} 0 0 0
167 }
168 # copy _id and _rev to id/rev
169 $self->{id} = delete $data->{_id}
170 0 ▶ if exists $data->{_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"_id"} 0 0 0
171 $self->{rev} = delete $data->{_rev}
172 0 ▶ if exists $data->{_rev};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"_rev"} 0 0 0
173 # if user is set then use it as name
174 0 ▶ if (exists $data->{user}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $data->{'user'}) { } 0 0 0
elsif (exists $data->{'_key'}) { } 0 0 0
175 0 $self->{name} = delete $data->{user};
176 }
177 # otherwise try _key
178 elsif (exists $data->{_key}) {
179 $self->{name} = delete $data->{_key}
180 0 }
181 # copy to and from
182 $self->{from} = delete $data->{_from}
183 0 ▶ if exists $data->{_from};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"_from"} 0 0 0
184 $self->{to} = delete $data->{_to}
185 0 ▶ if exists $data->{_to};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"_to"} 0 0 0
186  
187 0 return $self;
188 }
189  
190 # _get_set
191 #
192 # either get or set value.
193 # setting value returns self.
194 sub _get_set
195 42 ▶{
Subroutine _get_set: called CC=3 SCAR=16.0
196 42 my($self, $param, $value, $validate) = @_;
197  
198 42 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 42 42
199 # if we are only validating then return valid value
200 0 ▶ return $value if $validate;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 0 0
201 # set value and return self
202 0 $self->{$param} = $value;
203 0 return $self;
204 }
205 else {
206 # return currently set value
207 42 return $self->{$param};
208 }
209 }
210  
211 # _get_set_bool
212 #
213 # either get value or set JSON bool value.
214 # setting value returns self.
215 sub _get_set_bool
216 0 ▶{
Subroutine _get_set_bool: not called CC=6 SCAR=37.4
217 0 my($self, $param, $value, $validate) = @_;
218  
219 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
220 # accept "true" as JSON bool false
221 0 ▶ if ($value eq "true") {
Branch: 2 branches
BranchTrueFalseTotal
if ($value eq 'true') { } 0 0 0
elsif ($value eq 'false') { } 0 0 0
222 0 $value = JSON::XS::true;
223 }
224 # accept "false" as JSON bool false
225 elsif ($value eq "false") {
226 0 $value = JSON::XS::false;
227 }
228 # use the true/false of value to determine JSON bool
229 else {
230 0 ▶ $value = $value ? JSON::XS::true : JSON::XS::false;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
231 }
232 # if we are only validating then return valid value
233 0 ▶ return $value if $validate;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 0 0
234 # set value and return self
235 0 $self->{$param} = $value;
236 0 return $self;
237 }
238 else {
239 # return currently set value
240 0 return $self->{$param};
241 }
242 }
243  
244 # _get_set_id
245 #
246 # get/set id with either string or object
247 # setting a value returns self
248 sub _get_set_id
249 0 ▶{
Subroutine _get_set_id: not called CC=5 SCAR=34.0
250 0 my($self, $param, $value, $validate) = @_;
251  
252 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
253 # get value from an ArangoDB2 object
254 0 ▶ if ( blessed $value && $value->can('id') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->can("id")) 0 0 0
Condition0%0/3
builtin::blessed($value) and $value->can("id")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and $value->can("id")
builtin::blessed($value) $value->can("id")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->can("id")
ABresult
0X0
100
111
255 0 $value = $value->id
256 }
257 # if we are only validating then return valid value
258 0 ▶ return $value if $validate;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 0 0
259 # set value and return self
260 0 $self->{$param} = $value;
261 0 return $self;
262 }
263 else {
264 # return currently set value
265 0 return $self->{$param};
266 }
267 }
268  
269 # _get_set_name
270 #
271 # get/set name with either name or object
272 # setting a value returns self
273 sub _get_set_name
274 78 ▶{
Subroutine _get_set_name: called CC=5 SCAR=17.3
275 78 my($self, $param, $value, $validate) = @_;
276  
277 78 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 25 53 78
278 # get value from an ArangoDB2 object
279 25 ▶ if ( blessed $value && $value->can('name') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and $value->can("name")) 0 25 25
Condition33%1/3
builtin::blessed($value) and $value->can("name")
!ll&&!rl&&r
2500
MC/DC0%0/2
builtin::blessed($value) and $value->can("name")
builtin::blessed($value) $value->can("name")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->can("name")
ABresult
0X0
100
111
280 0 $value = $value->name
281 }
282 # if we are only validating then return valid value
283 25 ▶ return $value if $validate;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 25 25
284 # set value and return self
285 25 $self->{$param} = $value;
286 25 return $self;
287 }
288 else {
289 # return currently set value
290 53 return $self->{$param};
291 }
292 }
293  
294 22 1;
295  
296 __END__
« ArangoDB2/HTTP/LWP.pm ↑ Summary ArangoDB2/Graph/Vertex.pm »