ODS/Storage/File.pm

filter: Statementstmt 88.7% 118 / 133 Branchbran 53.1% 17 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.0% 23 / 25 Podpod 0.0% 0 / 14 totaltotal 77.4% 158 / 204 CC 21 SCAR 11.0
CC
21
Cov
82%
__ANON__
12.0
seek_file
6.0
find
3.0
CRAP 3.0
« ODS/Translator.pm ↑ Summary ODS/Table/Column/Base.pm »
1 package ODS::Storage::File;
2  
3 1 ▶use YAOO;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 extends 'ODS::Storage::Base';
7  
8 1 ▶use ODS::Utils qw/move error/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 auto_build;
11  
12 1 has file_handle => isa(fh);
13  
14 has file => isa(string), coerce(sub {
15 2 ▶ my ($self, $value) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 2 my $path = getcwd;
17 2 $value =~ s/^\///;
18 2 return sprintf("%s/%s.%s", $path, $value, $self->file_suffix);
19 }), trigger(sub {
20 2 ▶ my ($self, $value) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
21 2 $value .= '.tmp';
22 2 $self->save_file($value);
23 1 });
24  
25 1 has save_file => isa(string);
26  
27 sub all {
28 2 ▶ my ($self) = @_;
Subroutine all: called CC=1 SCAR=0.0
Pod undocumented
29
30 2 my $data = $self->into_rows($self->read_file());
31  
32 2 return $data;
33 }
34  
35 sub create {
36 2 ▶ my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] });
Subroutine create: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
37  
38 2 my $data = $self->into_rows(\%params, 1);
39  
40 2 $data->validate();
41  
42 2 push @{ $self->table->rows }, $data;
43  
44 2 $data = $self->into_storage(1);
45  
46 2 $self->write_file( $data );
47  
48 2 $self->table;
49 }
50  
51 sub search {
52 1 ▶ my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] });
Subroutine search: called CC=3 SCAR=11.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
53  
54 1 ▶ my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all;
Branch: 1 branch
BranchTrueFalseTotal
$self->table->rows ? : 1 0 1
55
56 # this only works for JSON and YAML, CSS and JSONL we can stream/read rows/lines instead of reading/loading
57 # all into memory.
58 my $select = $data->filter(sub {
59 3 ▶ my $row = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
60 3 my $select = 1;
61 3 for my $key ( keys %params ) {
62 3 ▶ if ( $params{$key} ne $row->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$key} ne $row->{$key}) 2 1 3
63 2 $select = undef;
64 2 last;
65 }
66 }
67 3 $select;
68 1 });
69  
70 1 my $table = $self->table->clone();
71 1 $table->rows($select);
72 1 my $table = $self->table->clone();
73 1 $table->rows($select);
74 1 ODS::Iterator->new(table => $table);
75 }
76  
77 sub find {
78 3 ▶ my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] });
Subroutine find: called CC=3 SCAR=11.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 3 0 3
79  
80 3 ▶ my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all;
Branch: 1 branch
BranchTrueFalseTotal
$self->table->rows ? : 3 0 3
81
82 # this only works for JSON and YAML, CSS and JSONL we can stream/read rows/lines instead of reading/loading
83 # all into memory.
84 my $select = $data->find(sub {
85 12 ▶ my $row = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
86 12 my $select = 1;
87 12 for my $key ( keys %params ) {
88 14 ▶ if ( $params{$key} ne $row->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$key} ne $row->{$key}) 10 4 14
89 10 $select = undef;
90 10 last;
91 }
92 }
93 12 $select;
94 3 });
95  
96 3 return $select;
97 }
98  
99 sub update {
100 1 ▶ my ($self, $update, %params) = (shift, pop, @_);
Subroutine update: called CC=2 SCAR=7.0
Pod undocumented
101  
102 1 my $find = $self->find(%params);
103  
104 1 ▶ croak sprintf "No row found for search params %s", Dumper \%params
Branch: 1 branch
BranchTrueFalseTotal
unless $find 0 1 1
105 unless $find;
106  
107 1 $find->validate($update);
108  
109 1 $self->update_row();
110 }
111  
112 sub update_row {
113 2 ▶ my ($self) = @_;
Subroutine update_row: called CC=1 SCAR=0.0
Pod undocumented
114  
115 2 my $data = $self->into_storage(1);
116  
117 2 $self->write_file( $data );
118  
119 2 $self->table;
120 }
121  
122 sub delete {
123 1 ▶ my ($self, %params) = (shift, @_ > 1 ? @_ : %{ $_[0] });
Subroutine delete: called CC=3 SCAR=11.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
124  
125 1 ▶ my $data = $self->table->rows ? ODS::Iterator->new(table => $self->table) : $self->all;
Branch: 1 branch
BranchTrueFalseTotal
$self->table->rows ? : 1 0 1
126
127 my $index = $data->find_index(sub {
128 4 ▶ my $row = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
129 4 my $select = 1;
130 4 for my $key ( keys %params ) {
131 4 ▶ if ( $params{$key} ne $row->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{$key} ne $row->{$key}) 3 1 4
132 3 $select = undef;
133 3 last;
134 }
135 }
136 4 $select;
137 1 });
138
139 1 $data->splice($index, 1);
140  
141 1 $data = $self->into_storage(1);
142  
143 1 $self->write_file( $data );
144  
145 1 $self->table;
146 }
147  
148 sub delete_row {
149 1 ▶ my ($self, $r) = @_;
Subroutine delete_row: called CC=2 SCAR=9.2
Pod undocumented
150  
151 1 my $data = ODS::Iterator->new(table => $self->table);
152  
153 1 my $keyfield = $data->table->keyfield;
154  
155 1 my $index;
156 1 ▶ if ($keyfield) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyfield) { } 1 0 1
157 $index = $data->find_index(sub {
158 4 ▶ $_[0]->{$keyfield} eq $r->$keyfield;
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 });
160 } else {
161 $index = $data->find_index(sub {
162 0 ▶ my $row = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
163 0 my $select = 1;
164 0 for my $key ( keys %{ $row->columns } ) {
165 0 ▶ if ( $r->$key ne $row->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->$key ne $row->{$key}) 0 0 0
166 0 $select = undef;
167 0 last;
168 }
169 }
170 0 $select;
171 0 });
172 }
173  
174 1 $data->splice($index, 1);
175  
176 1 $data = $self->into_storage(1);
177  
178 1 $self->write_file( $data );
179  
180 1 $self->table;
181 }
182  
183  
184 # methods very much specific to files
185  
186 sub open_file {
187 2 ▶ my ($self) = @_;
Subroutine open_file: called CC=2 SCAR=7.0
Pod undocumented
188 1 ▶ open my $fh, '<:encoding(UTF-8)', $self->file or die "Cannot open file for reading: $!";
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $self->file or die "Cannot open file for reading: $!" 0 2 2
189 2 $self->file_handle($fh);
190 2 return $fh;
191 }
192  
193 sub open_write_file {
194 6 ▶ my ($self) = @_;
Subroutine open_write_file: called CC=2 SCAR=7.1
Pod undocumented
195 6 ▶ open my $fh, '>:encoding(UTF-8)', $self->save_file or die "Cannot open file for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(UTF-8)", $self->save_file or die "Cannot open file for writing: $!" 0 6 6
196 6 return $fh;
197 }
198  
199 sub seek_file {
200 0 ▶ my ($self, @args) = @_;
Subroutine seek_file: not called CC=2 SCAR=17.9
Pod undocumented
201 0 ▶ @args = (0, 0) if (!scalar @args);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @args 0 0 0
202 0 seek $self->file_handle, shift @args, shift @args;
203 }
204  
205 sub read_file {
206 2 ▶ my ($self) = @_;
Subroutine read_file: called CC=1 SCAR=0.0
Pod undocumented
207 2 my $fh = $self->open_file;
208 2 my $data = do { local $/; <$fh> };
209 2 return $data;
210 }
211  
212 sub write_file {
213 6 ▶ my ($self, $data) = @_;
Subroutine write_file: called CC=1 SCAR=0.0
Pod undocumented
214 6 my $fh = $self->open_write_file;
215 6 print $fh $data;
216 6 $self->close_file($fh);
217 6 $self->close_file($self->file_handle);
218 6 move($self->save_file, $self->file);
219 6 unlink $self->save_file;
220 }
221  
222 sub close_file {
223 12 ▶ close $_[1];
Subroutine close_file: called CC=1 SCAR=0.0
Pod undocumented
224 }
225  
226 1 1;
« ODS/Translator.pm ↑ Summary ODS/Table/Column/Base.pm »