blib/lib/Catmandu/Fix/lookup_in_store.pm

filter: Statementstmt 100% 52 / 52 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 97.1% 69 / 71 CC 5 SCAR 7.0
CC
5
Cov
98%
__ANON__
4.0
_build_bag
2.1
BEGIN
1.0
CRAP 2.0
« blib/lib/Catmandu.pm ↑ Summary blib/lib/Catmandu/Transactional.pm »
1 package Catmandu::Fix::lookup_in_store;
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 Catmandu;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Catmandu::Fix::Has;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Catmandu::Fix::Base';
13  
14 1 has path => (fix_arg => 1);
15 1 has store_name => (fix_arg => 1);
16 1 has bag_name => (fix_opt => 1, init_arg => 'bag');
17 1 has default => (fix_opt => 1);
18 1 has delete => (fix_opt => 1);
19 1 has store_args => (fix_opt => 'collect');
20 1 has store => (is => 'lazy', init_arg => undef);
21 1 has bag => (is => 'lazy', init_arg => undef);
22  
23 sub _build_store {
24 6 ▶ my ($self) = @_;
Subroutine _build_store: called CC=1 SCAR=0.0
25 6 Catmandu->store($self->store_name, %{$self->store_args});
26 }
27  
28 sub _build_bag {
29 6 ▶ my ($self) = @_;
Subroutine _build_bag: called CC=2 SCAR=7.2
30 6 ▶ defined $self->bag_name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->bag_name ? : 0 6 6
31 ? $self->store->bag($self->bag_name)
32 : $self->store->bag;
33 }
34  
35 sub emit {
36 6 ▶ my ($self, $fixer) = @_;
Subroutine emit: called CC=1 SCAR=0.0
Pod undocumented
37 6 my $path = $fixer->split_path($self->path);
38 6 my $key = pop @$path;
39 6 my $bag_var = $fixer->capture($self->bag);
40 6 my $delete = $self->delete;
41 6 my $default = $self->default;
42  
43 $fixer->emit_walk_path(
44 $fixer->var,
45 $path,
46 sub {
47 6 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
48 $fixer->emit_get_key(
49 $var, $key,
50 sub {
51 6 ▶ my $val_var = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
52 6 my $val_index = shift;
53 6 my $bag_val_var = $fixer->generate_var;
54 6 my $perl
55 = "if (is_value(${val_var}) && defined(my ${bag_val_var} = ${bag_var}->get(${val_var}))) {"
56 . "${val_var} = ${bag_val_var};" . "}";
57 6 ▶ if ($delete) {
Branch: 2 branches
BranchTrueFalseTotal
if ($delete) { } 2 4 6
elsif (defined $default) { } 2 2 4
58 2 $perl .= "else {";
59 2 ▶ if (defined $val_index)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val_index) { } 1 1 2
60 { # wildcard: only delete the value where the lookup failed
61 1 $perl .= "splice(\@{${var}}, ${val_index}--, 1);";
62 }
63 else {
64 1 $perl .= $fixer->emit_delete_key($var, $key);
65 }
66 2 $perl .= "}";
67 }
68 elsif (defined $default) {
69 2 $perl
70 .= "else {"
71 . "${val_var} = "
72 . $fixer->emit_value($default) . ";" . "}";
73 }
74 6 $perl;
75 }
76 6 );
77 }
78 6 );
79 }
80  
81 1 1;
82  
83 __END__
84  
85 =pod
86  
87 =head1 NAME
88  
89 Catmandu::Fix::lookup_in_store - change the value of a HASH key or ARRAY index
90 by looking up its value in a store
91  
92 =head1 SYNOPSIS
93  
94 # Lookup in an SQLLite database
95 lookup_in_store(foo.bar, DBI, data_source: "dbi:SQLite:path/data.sqlite")
96  
97 # Lookup in a MongoDB database
98 lookup_in_store(foo.bar, MongoDB, database_name: lookups, bag: mydata)
99  
100 # Lookup in a MongoDB database, using the default bag and a default value when nothing found
101 lookup_in_store(foo.bar, MongoDB, database_name: lookups, default: 'default value')
102  
103 # Lookup in a MongoDB database, using the default bag and delete the foo.bar field when nothing found
104 lookup_in_store(foo.bar, MongoDB, database_name: lookups, delete: 1)
105  
106 =head1 DESCRIPTION
107  
108 =head2 lookup_in_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name][,delete:1][,default:value])
109  
110 Use the lookup_in_store fix to match a field in a record to the "_id" field in
111 a Catmandu::Store of choice. For instance, if a Catmandu::Store contains these
112 records:
113  
114 ---
115 _id: water
116 fr: l'eau
117 de: wasser
118 en: water
119 nl: water
120 ---
121 _id: tree
122 fr: arbre
123 de: baum
124 en: tree
125 nl: boom
126  
127 And you data contains these fields:
128  
129 ---
130 _id: 001
131 tag: tree
132 ---
133 _id: 002
134 tag: water
135  
136 Then, the fix below will lookup a tag in the Catmandu::Store and replace it
137 with the database value:
138  
139 lookup_in_store(tag, DBI, data_source: "dbi:SQLite:path/data.sqlite")
140  
141 The resulting data will contain:
142  
143 ---
144 _id: 001
145 tag:
146 _id: tree
147 fr: arbre
148 de: baum
149 en: tree
150 nl: boom
151 ---
152 _id: 002
153 tag:
154 _id: water
155 fr: l'eau
156 de: wasser
157 en: water
158 nl: water
159  
160 =head1 SEE ALSO
161  
162 L<Catmandu::Fix>, L<Catmandu::Store> , L<Catmandu::Fix::add_to_store>
163  
164 =cut
165  
« blib/lib/Catmandu.pm ↑ Summary blib/lib/Catmandu/Transactional.pm »