ODS/Storage/File.pm

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