blib/lib/Catmandu/Fix/Bind/with.pm

filter: Statementstmt 92.1% 47 / 51 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 5 totaltotal 83.7% 67 / 80 CC 9 SCAR 11.2
CC
9
Cov
89%
bind
5.5
inline_copy
4.0
zero
2.0
CRAP 3.1
« blib/lib/Catmandu/Cmd/count.pm ↑ Summary blib/lib/Catmandu/Fix/Bind/visitor.pm »
1 package Catmandu::Fix::Bind::with;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.0201_02';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Catmandu::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::ArrayIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Catmandu::Fix::Bind';
13  
14 1 has path => (is => 'ro');
15 1 has flag => (is => 'rw');
16  
17 sub zero {
18 0 ▶ my ($self) = @_;
Subroutine zero: not called CC=1 SCAR=6.9
Pod undocumented
19 0 [];
20 }
21  
22 sub unit {
23 12 ▶ my ($self, $data) = @_;
Subroutine unit: called CC=2 SCAR=6.9
Pod undocumented
24  
25 12 my $ref;
26  
27 12 ▶ if (defined $self->path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path) { } 2 10 12
28 2 $ref = Catmandu::Util::data_at($self->path, $data);
29 }
30 else {
31 10 $ref = $data;
32 }
33  
34 # Set a flag so that all the bind fixes are only run once...
35 12 $self->flag(0);
36  
37 12 return $ref;
38 }
39  
40 sub bind {
41 11 ▶ my ($self, $mvar, $func, $name, $fixer) = @_;
Subroutine bind: called CC=5 SCAR=17.0
Pod undocumented
42  
43 # The fixer contains all the fixes no every separate fix.
44 # Set a flag so that the fixes are only run once, creating an implicit do identity() ... end block
45  
46 11 ▶ return $mvar unless $self->flag == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->flag == 0 0 11 11
47  
48 11 my $ref;
49  
50 11 ▶ if (!defined $mvar) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $mvar) { } 0 11 11
elsif (Ref::Util::XS::is_plain_arrayref($mvar)) { } 2 9 11
elsif (Ref::Util::XS::is_plain_hashref($mvar)) { } 9 0 9
51 0 $ref = $fixer->fix($self->zero);
52 }
53 elsif (Catmandu::Util::is_array_ref($mvar)) {
54 2 $ref = $fixer->fix($mvar);
55 }
56 elsif (Catmandu::Util::is_hash_ref($mvar)) {
57 9 $ref = $fixer->fix($mvar);
58 }
59 else {
60 0 $ref = $fixer->fix($self->zero);
61 }
62  
63 11 inline_copy($mvar, $ref);
64  
65 11 $self->flag(1);
66  
67 11 $mvar;
68 }
69  
70 sub result {
71 12 ▶ my ($self, $mvar) = @_;
Subroutine result: called CC=1 SCAR=0.0
Pod undocumented
72 12 $self->flag(0);
73 12 $mvar;
74 }
75  
76 sub inline_copy {
77 11 ▶ my ($old, $new) = @_;
Subroutine inline_copy: called CC=4 SCAR=13.9
Pod undocumented
78  
79 11 ▶ if (Catmandu::Util::is_array_ref($old)) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($old)) { } 2 9 11
elsif (not defined $new) { } 1 8 9
80 2 undef @{$old};
81 2 for (@$new) {
82 4 push @$old, $_;
83 }
84 }
85 elsif (!defined $new) {
86 1 undef %{$old};
87 }
88 }
89  
90 1 1;
91  
92 __END__
93  
94 =pod
95  
96 =head1 NAME
97  
98 Catmandu::Fix::Bind::with - a binder that computes Fix-es in the context of a path
99  
100 =head1 SYNOPSIS
101
102 # Input data
103 data:
104 - name: patrick
105 - name: nicolas
106  
107 # Fix
108 do with(path => data)
109 if all_match(name,nicolas)
110 reject()
111 end
112 end
113  
114 # will produce
115 data:
116 - name: patrick
117  
118
119 =head1 DESCRIPTION
120  
121 The C<with> bind allows to run fixes in the scope of a path.
122  
123 Given a deep nested data structure :
124  
125 my:
126 deep:
127 field:
128 name: James Brown
129  
130 these two fixes are equal:
131  
132 add_field(my.deep.field.style, funk)
133  
134 do with(path => my.deep.field)
135 add_field(style,funk)
136 end
137  
138 =head1 CONFIGURATION
139  
140 =head2 path
141  
142 The path to a list in the data.
143  
144 =head1 SEE ALSO
145  
146 L<Catmandu::Fix::Bind>
147  
148 =cut
« blib/lib/Catmandu/Cmd/count.pm ↑ Summary blib/lib/Catmandu/Fix/Bind/visitor.pm »