Journyx/Record.pm

filter: Statementstmt 16.1% 22 / 136 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 27.7% 5 / 18 Podpod 0.0% 0 / 4 totaltotal 9.8% 27 / 275 CC 45 SCAR 42.9
CC
45
Cov
10%
load_from_hash
156.0
update
156.0
create
56.0
CRAP 73.2
↑ Summary Journyx/Project.pm »
1 package Net::Journyx::Record;
2 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 8 extends 'Net::Journyx::Object';
5  
6 8 with 'Net::Journyx::Object::Loadable' => {
7 check_on => [qw(update delete)],
8 drop_on => [qw(load_from_hash create delete)],
9 };
10  
11 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 ▶use constant jx_strip_record_suffix => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use constant jx_meta => {};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # class data implementation via inside out
17 8 my %jx_columns = ();
18 has jx_columns => (
19 is => 'ro',
20 init_arg => undef,
21 isa => 'ArrayRef[Str]',
22 lazy => 1,
23 0 ▶ default => sub { return $jx_columns{ ref(shift) } || [] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$jx_columns{ref shift()} || []
l!l
00
MC/DC0%0/1
$jx_columns{ref shift()} || []
$jx_columns{ref shift()}
Truth table0%0/2
A
A= $jx_columns{ref shift()}
Aresult
11
00
24 8 );
25 0 ▶sub _jx_columns { $jx_columns{ ref($_[0]) || $_[0] } = $_[1] }
Subroutine _jx_columns: not called CC=2 SCAR=17.9
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
26  
27 8 my %record_template = ();
28 has record_template => (
29 is => 'ro',
30 init_arg => undef,
31 isa => 'HashRef',
32 lazy => 1,
33 0 ▶ default => sub { return $record_template{ ref($_[0]) } ||= $_[0]->_record_template },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$record_template{ref $_[0]} ||= $_[0]->_record_template
l!l&&r!l&&!r
000
MC/DC0%0/2
$record_template{ref $_[0]} ||= $_[0]->_record_template
$record_template{ref $_[0]} $_[0]->_record_template
Truth table0%0/3
A ||= B
A= $record_template{ref $_[0]}B= $_[0]->_record_template
ABresult
1X1
011
000
34 8 );
35  
36 0 ▶sub BUILD {
Subroutine BUILD: not called CC=5 SCAR=34.0
Pod undocumented
37 0 my $self = shift;
38 0 my $args = shift;
39  
40 0 ▶ return if $self->meta->is_immutable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->is_immutable 0 0 0
41  
42 0 ▶ my $jx = $args->{'jx'} or die "No JX";
Branch: 1 branch
BranchTrueFalseTotal
my $jx = $args->{"jx"} or die "No JX" 0 0 0
43  
44 0 my %columns = $jx->soap->record_columns( $self->jx_record_class );
45  
46 0 my %type_map = (
47 'xsd:boolean' => 'Bool',
48 'xsd:int' => 'Int',
49 'xsd:double' => 'Math::BigFloat',
50 'xsd:string' => 'Str',
51 );
52 0 ▶ while (my ($name, $schema_type) = each %columns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %columns) 0 0 0
53 0 ▶ my $moose_type = $type_map{ $schema_type }
Branch: 1 branch
BranchTrueFalseTotal
my $moose_type = $type_map{$schema_type} or die "No moose type for JX type '${schema_type}'" 0 0 0
54 or die "No moose type for JX type '$schema_type'";
55  
56 #warn "adding column accessor $name to ". ref($self);
57 0 $self->meta->add_attribute(
58 $name,
59 is => 'ro',
60 isa => $moose_type,
61 writer => '_'. $name,
62 );
63 }
64  
65 0 $self->_jx_columns([keys %columns]);
66  
67 0 $self->meta->make_immutable;
68 }
69  
70 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
71 0 my $self = shift;
72 0 my %args = @_;
73  
74 my $jx_operation = $self->auto_method(
75 0 $self->jx_meta->{'load'},
76 'get'
77 );
78  
79 0 my $response = $self->jx->soap->basic_call(
80 $jx_operation => pattern => \%args
81 );
82 # warn "loaded $jx_class ". Dumper($response) ." using ". Dumper(\%args);
83  
84 0 my $jx_class = $self->jx_record_class;
85  
86 0 my $record = $response->{ $jx_class };
87 0 ▶ unless ( $record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record) 0 0 0
88 0 die "No '$jx_class' in response ". Dumper($response);
89 }
90  
91 0 return $self->load_from_hash( %$record );
92 }
93  
94 0 ▶sub load_from_hash {
Subroutine load_from_hash: not called CC=12 SCAR=50.5
95 0 my $self = shift;
96 0 my %args = @_;
97  
98 0 foreach my $k ( keys %args ) {
99 0 ▶ if ( $k =~ /^_+(.*)$/ && !exists $args{ $1 } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^_+(.*)$/ and not exists $args{$1}) 0 0 0
Condition0%0/3
$k =~ /^_+(.*)$/ and not exists $args{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$k =~ /^_+(.*)$/ and not exists $args{$1}
$k =~ /^_+(.*)$/ not exists $args{$1}
Truth table0%0/3
A and not(B)
A= $k =~ /^_+(.*)$/B= not exists $args{$1}
ABresult
0X0
100
111
100 0 $args{ $1 } = delete $args{ $k };
101 }
102 }
103  
104 0 my @missing;
105 0 foreach my $column ( @{ $self->jx_columns } ) {
106 0 my $method = '_'. $column;
107 0 ▶ if ( exists $args{ $column } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$column}) { } 0 0 0
108 0 $self->$method( delete $args{ $column } );
109 } else {
110 0 push @missing, $column;
111 # flush as it can be not empty
112 0 $self->$method( undef );
113 }
114 }
115  
116 0 my @more = keys %args;
117 0 ▶ if ( @missing || @more ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@missing or @more) { } 0 0 0
elsif ($self->can('id')) { } 0 0 0
Condition0%0/3
@missing or @more
l!l&&r!l&&!r
000
MC/DC0%0/2
@missing or @more
@missing @more
Truth table0%0/3
A or B
A= @missingB= @more
ABresult
1X1
011
000
118 0 my $msg = "Either during load or load_from_hash of ". $self->jx_record_class
119 ." incorrect columns have been provided to load_from_hash.";
120 0 ▶ $msg .= " Missing columns: ". join(', ', map "'$_'", @missing) ."."
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 0 0
121 if @missing;
122 0 ▶ $msg .= " Superfluous columns: ". join(', ', map "'$_'", @more) ."."
Branch: 1 branch
BranchTrueFalseTotal
if @more 0 0 0
123 if @more;
124 0 $msg .= " It can be either API bug or incorrect call to load_from_hash."
125 ." Record *is not* marked as loaded.";
126 0 warn $msg;
127 }
128 elsif ( $self->can('id') ) {
129 # XXX: load may pass us empty record with all defaults
130 0 ▶ $self->_is_loaded(1) if $self->id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->id 0 0 0
131 }
132 else {
133 0 $self->_is_loaded(1);
134 }
135  
136 0 return $self;
137 }
138  
139 0 ▶sub create {
Subroutine create: not called CC=7 SCAR=40.3
140 0 my $self = shift;
141 0 my %args = @_;
142  
143 0 my %private_args = map { $_ => delete $args{"__$_"} }
144 0 grep { s/^__// } # filter and strip leading __
145 keys %args;
146  
147 0 my $jx_class = $self->jx_record_class;
148 0 my $jx_meta = $self->jx_meta->{'create'};
149  
150 0 ▶ if ( my $quick = $jx_meta->{'quick'}
Branch: 1 branch
BranchTrueFalseTotal
if (my $quick = $jx_meta->{"quick"} and join(",", sort(keys %args)) eq join(",", sort(@{$jx_meta->{"quick"}{"columns"};}))) 0 0 0
Condition0%0/3
my $quick = $jx_meta->{"quick"} and join(",", sort(keys %args)) eq join(",", sort(@{$jx_meta->{"quick"}{"columns"};}))
!ll&&!rl&&r
000
MC/DC0%0/2
my $quick = $jx_meta->{"quick"} and join(",", sort(keys %args)) eq join(",", sort(@{$jx_meta->{"quick"}{"columns"};}))
my $quick = $jx_meta->{"quick"} join(",", sort(keys %args)) eq join(",", sort(@{$jx_meta->{"quick"}{"columns"};}))
Truth table0%0/3
A and B
A= my $quick = $jx_meta->{"quick"}B= join(",", sort(keys %args)) eq join(",", sort(@{$jx_meta->{"quick"}{"columns"};}))
ABresult
0X0
100
111
151 0 and join(',', sort keys %args) eq join(',', sort @{$jx_meta->{'quick'}{'columns'}})
152 ) {
153 my $response = $self->jx->soap->basic_call(
154 $quick->{'operation'},
155 0 ▶ map { $quick->{'rewrite'} || $_ => $args{$_} }
Condition0%0/3
$quick->{'rewrite'} || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$quick->{'rewrite'} || $_
$quick->{'rewrite'} $_
Truth table0%0/3
A || B
A= $quick->{'rewrite'}B= $_
ABresult
1X1
011
000
156 0 @{ $quick->{'columns'} }
157 );
158  
159 # load self to get defaults
160 0 return $self->load( id => $response );
161 }
162  
163 0 my $defaults = $self->record_template;
164 0 $args{$_} = $defaults->{$_} foreach
165 grep !exists $args{$_}, keys %$defaults;
166  
167 $jx_meta->{'defaults'}->( $self, \%args )
168 0 ▶ if $jx_meta->{'defaults'};
Branch: 1 branch
BranchTrueFalseTotal
if $jx_meta->{"defaults"} 0 0 0
169  
170 0 my $jx_operation = $self->auto_method( $jx_meta->{'operation'}, 'addFull' );
171 0 my $response = $self->jx->soap->basic_call( $jx_operation, rec => \%args );
172 #warn "created $jx_class #$response using ". Dumper( \%args );
173  
174 0 ▶ return $response if $private_args{do_not_load};
Branch: 1 branch
BranchTrueFalseTotal
if $private_args{"do_not_load"} 0 0 0
175  
176 # load self to get defaults
177 0 return $self->load( id => $response );
178 }
179  
180 0 ▶sub update {
Subroutine update: not called CC=12 SCAR=50.5
181 0 my $self = shift;
182 0 my %args = @_;
183  
184 0 my $jx_class = $self->jx_record_class;
185 0 my $jx_meta = $self->jx_meta->{'update'};
186  
187 0 ▶ my $leading_column = $jx_meta->{'leading'} || 'id';
Condition0%0/2
$jx_meta->{'leading'} || 'id'
l!l
00
MC/DC0%0/1
$jx_meta->{'leading'} || 'id'
$jx_meta->{'leading'}
Truth table0%0/2
A
A= $jx_meta->{'leading'}
Aresult
11
00
188  
189 0 my %op_args = ();
190 0 foreach my $col ( @{ $self->jx_columns } ) {
191 0 ▶ if ( $col eq $leading_column ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($col eq $leading_column) { } 0 0 0
elsif (exists $args{$col}) { } 0 0 0
192 0 ▶ if ( exists $args{ $col } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$col}) 0 0 0
193 0 my $val = $args{ $col };
194 0 ▶ if ( defined $val && length $val && $val eq $self->$col() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and length $val and $val eq $self->$col) { } 0 0 0
Condition0%0/6
defined $val and length $val and $val eq $self->$col
!ll&&!rl&&r
000
defined $val and length $val
!ll&&!rl&&r
000
MC/DC0%0/3
defined $val and length $val and $val eq $self->$col
defined $val length $val $val eq $self->$col
Truth table0%0/4
A and B and C
A= defined $valB= length $valC= $val eq $self->$col
ABCresult
0XX0
10X0
1100
1111
195 0 die "Can not update column '$col' of $jx_class #". $self->$col();
196 } else {
197 # just ignore without complain
198 0 delete $args{ $col };
199 }
200 }
201 # we must pass leading column anyway
202 0 $op_args{ $col } = $self->$col();
203 }
204 elsif ( exists $args{ $col } ) {
205 0 $op_args{ $col } = delete $args{ $col };
206 } else {
207 0 $op_args{ $col } = $self->$col();
208 }
209 }
210  
211 0 ▶ if ( keys %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 0 0
212 0 warn "Ignored ". join( ', ', map "'$_'", keys %args )
213 ." arguments on update of $jx_class #". $self->$leading_column()
214 ." as these are not columns of the record";
215 }
216  
217 0 my $jx_operation = $self->auto_method( $jx_meta->{'operation'}, 'modify' );
218 my $response = $self->jx->soap->basic_call(
219 $jx_operation,
220 $jx_meta->{'leading_argname'} || $leading_column => $self->$leading_column(),
221 0 ▶ $jx_meta->{'record_argname'} || 'rec' => \%op_args,
Condition0%0/5
$jx_meta->{'leading_argname'} || $leading_column
l!l&&r!l&&!r
000
$jx_meta->{'record_argname'} || 'rec'
l!l
00
MC/DC0%0/2
$jx_meta->{'leading_argname'} || $leading_column
$jx_meta->{'leading_argname'} $leading_column
MC/DC0%0/1
$jx_meta->{'record_argname'} || 'rec'
$jx_meta->{'record_argname'}
Truth table0%0/3
A || B
A= $jx_meta->{'leading_argname'}B= $leading_column
ABresult
1X1
011
000
Truth table0%0/2
A
A= $jx_meta->{'record_argname'}
Aresult
11
00
222 );
223  
224 # reload self just in case Journyx may update values on update
225 0 return $self->load( $leading_column => $op_args{ $leading_column } );
226 }
227  
228 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
229 0 my $self = shift;
230  
231 0 my $jx_class = $self->jx_record_class;
232 0 my $jx_meta = $self->jx_meta->{'delete'};
233 0 my $jx_operation = $self->auto_method( $jx_meta->{'operation'}, 'remove' );
234 0 ▶ my $leading_column = $jx_meta->{'leading'} || 'id';
Condition0%0/2
$jx_meta->{'leading'} || 'id'
l!l
00
MC/DC0%0/1
$jx_meta->{'leading'} || 'id'
$jx_meta->{'leading'}
Truth table0%0/2
A
A= $jx_meta->{'leading'}
Aresult
11
00
235 0 ▶ my $leading_argname = $jx_meta->{'leading_argname'} || 'id';
Condition0%0/2
$jx_meta->{'leading_argname'} || 'id'
l!l
00
MC/DC0%0/1
$jx_meta->{'leading_argname'} || 'id'
$jx_meta->{'leading_argname'}
Truth table0%0/2
A
A= $jx_meta->{'leading_argname'}
Aresult
11
00
236  
237 0 my $response = $self->jx->soap->basic_call(
238 $jx_operation, $leading_argname => $self->$leading_column(),
239 );
240  
241 0 return $self;
242 }
243  
244 0 ▶sub list {
Subroutine list: not called CC=1 SCAR=6.9
Pod undocumented
245 0 die "not implemented"
246 }
247  
248  
249 0 ▶sub attribute {
Subroutine attribute: not called CC=1 SCAR=6.9
Pod undocumented
250 0 my $self = shift;
251 0 my $attr = shift;
252 0 die "Can get attribute of not loaded record";
253 }
254  
255 0 ▶sub _record_template {
Subroutine _record_template: not called CC=4 SCAR=30.0
256 0 my $self = shift;
257  
258 0 my $jx_class = $self->jx_record_class;
259 my $jx_operation = $self->auto_method(
260 0 $self->jx_meta->{'defaults'},
261 'getDefault'
262 );
263  
264 0 ▶ my $response = $self->jx->soap->basic_call( $jx_operation )
Branch: 1 branch
BranchTrueFalseTotal
my $response = $self->jx->soap->basic_call($jx_operation) or die "Couldn't get default field values for $jx_class" 0 0 0
265 or die "Couldn't get default field values for $jx_class";
266  
267 0 my $record = $response->{$jx_class};
268 0 ▶ unless ( $record && ref($record) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($record and ref $record eq "HASH") 0 0 0
Condition0%0/3
$record and ref $record eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$record and ref $record eq "HASH"
$record ref $record eq "HASH"
Truth table0%0/3
A and B
A= $recordB= ref $record eq "HASH"
ABresult
0X0
100
111
269 0 die "Couldn't get default field values for $jx_class";
270 }
271
272 0 return $record;
273 }
274  
275 0 ▶sub auto_method {
Subroutine auto_method: not called CC=4 SCAR=30.0
Pod undocumented
276 0 my $self = shift;
277 0 my ($val, $prefix, $suffix) = @_;
278 0 ▶ return $val if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 0 0
279  
280 0 my $jx_class = $self->jx_record_class;
281 0 ▶ $jx_class =~ s/Record$//
Branch: 1 branch
BranchTrueFalseTotal
if $self->jx_strip_record_suffix 0 0 0
282 if $self->jx_strip_record_suffix;
283  
284 0 ▶ return join '', grep defined && length,
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
285 $prefix, $jx_class, $suffix;
286 }
287  
288 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
289  
290 8 1;
↑ Summary Journyx/Project.pm »