blib/lib/Catmandu/Fix/paste.pm

filter: Statementstmt 100% 46 / 46 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 98.3% 60 / 61 CC 5 SCAR 9.8
CC
5
Cov
100%
emit
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.7
« blib/lib/Catmandu/Cmd/help.pm ↑ Summary blib/lib/Catmandu/Fix/Condition/exists.pm »
1 package Catmandu::Fix::paste;
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 namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Catmandu::Fix::Base';
12  
13 1 has path => (fix_arg => 1);
14 1 has values => (fix_arg => 'collect');
15  
16 sub emit {
17 3 ▶ my ($self, $fixer) = @_;
Subroutine emit: called CC=5 SCAR=16.1
Pod undocumented
18 3 my $values = $self->values;
19  
20 3 my @parsed_values = ();
21 3 my $join_char = ' ';
22  
23 3 ▶ while (@$values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 10 2 12
24 10 my $val = shift @$values;
25 10 ▶ if ($val eq 'join_char') {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq 'join_char') { } 1 9 10
26 1 $join_char = shift @$values;
27 1 last;
28 }
29 else {
30 9 push @parsed_values, $val;
31 }
32 }
33  
34 3 $join_char = $fixer->emit_string($join_char);
35  
36 3 my $vals_var = $fixer->generate_var;
37 3 my $perl = $fixer->emit_declare_vars($vals_var, '[]');
38  
39 3 for my $val (@parsed_values) {
40 9 my $vals_path = $fixer->split_path($val);
41 9 my $vals_key = pop @$vals_path;
42  
43 9 ▶ if ($val =~ /^~(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^~(.*)/) { } 1 8 9
44 1 my $tmp = $fixer->emit_string($1);
45 1 $perl .= "push(\@{${vals_var}}, ${tmp});";
46 }
47 else {
48 $perl .= $fixer->emit_walk_path(
49 $fixer->var,
50 $vals_path,
51 sub {
52 8 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
53 $fixer->emit_get_key(
54 $var,
55 $vals_key,
56 sub {
57 8 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
58 8 "push(\@{${vals_var}}, ${var}) if is_value(${var});";
59 }
60 8 );
61 }
62 8 );
63 }
64 }
65  
66 3 my $path = $fixer->split_path($self->path);
67  
68 $perl .= $fixer->emit_create_path(
69 $fixer->var,
70 $path,
71 sub {
72 3 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
73 3 "${var} = join(${join_char}, \@{${vals_var}});";
74 }
75 3 );
76  
77 3 $perl;
78 }
79  
80 1 1;
81  
82 __END__
83  
84 =pod
85  
86 =head1 NAME
87  
88 Catmandu::Fix::paste - concatenate path values
89  
90 =head1 SYNOPSIS
91  
92 # If you data record is:
93 # a: eeny
94 # b: meeny
95 # c: miny
96 # d: moe
97 paste(my.string,a,b,c,d) # my.string: eeny meeny miny moe
98  
99 # Use a join character
100 paste(my.string,a,b,c,d,join_char:", ") # my.string: eeny, meeny, miny, moe
101  
102 # Paste literal strings with a tilde sign
103 paste(my.string,~Hi,a,~how are you?) # my.string: Hi eeny how are you?
104  
105 =head1 DESCRIPTION
106  
107 Paste places a concatenation of all paths starting from the second path into the first path.
108 Literal values can be pasted by prefixing them with a tilde (~) sign.
109  
110 =head1 SEE ALSO
111  
112 L<Catmandu::Fix>
113  
114 =cut
« blib/lib/Catmandu/Cmd/help.pm ↑ Summary blib/lib/Catmandu/Fix/Condition/exists.pm »