TreePath.pm

filter: Statementstmt 96.0% 242 / 252 Branchbran 82.0% 82 / 100 Conditioncond 79.3% 23 / 29 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 96.7% 30 / 31 Podpod 100% 10 / 10 totaltotal 90.4% 398 / 440 CC 74 SCAR 19.4
CC
74
Cov
91%
_normalize_node
12.6
traverse
12.1
search
12.0
CRAP 7.0
« TreePath/Backend/DBIx.pm ↑ Summary TreePath/Backend/File.pm »
1 package TreePath;
2 5 $TreePath::VERSION = '0.22';
3 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 with 'MooseX::Object::Pluggable';
6  
7 5 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Config::JFDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 subtype MyConf => as 'HashRef';
13 coerce 'MyConf'
14 4 ▶ => from 'Str' => via {
Subroutine __ANON__: called CC=2 SCAR=7.1
15 4 my $conf = shift;
16 4 ▶ my ($jfdi_h, $jfdi) = Config::JFDI->open($conf)
Branch: 1 branch
BranchTrueFalseTotal
() = "Config::JFDI"->open($conf) or croak("Error (conf: $conf) : $!\n") 4 0 4
17 or croak "Error (conf: $conf) : $!\n";
18 4 return $jfdi->get;
19 5 };
20  
21 has conf => ( is => 'rw',
22 isa => 'MyConf',
23 coerce => 1,
24 5 ▶ trigger => sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
25 5 my $self = shift;
26 5 my $args = shift;
27  
28 # if args exist
29 5 ▶ if ( defined $args->{$self->configword} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$args{$self->configword}) 5 0 5
30 croak "Error: Can not find " . $self->configword . " in your conf !"
31 5 ▶ if ( ! $args->{$self->configword});
Branch: 1 branch
BranchTrueFalseTotal
unless $$args{$self->configword} 0 5 5
32  
33 5 $self->config($args->{$self->configword});
34  
35 $self->debug($self->config->{'debug'})
36 5 ▶ if ( ! defined $self->debug && defined $self->config->{'debug'} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->debug and defined $self->config->{"debug"} 3 2 5
Condition66%2/3
not defined $self->debug and defined $self->config->{"debug"}
!ll&&!rl&&r
203
MC/DC50%1/2
not defined $self->debug and defined $self->config->{"debug"}
not defined $self->debug defined $self->config->{"debug"}
Truth table66%2/3
not(A) and B
A= not defined $self->debugB= defined $self->config->{"debug"}
ABresult
0X0
100
111
37  
38 5 my $backend = $self->config->{'backend'}->{name};
39 5 $self->backend($backend);
40 }
41 }
42 5 );
43  
44  
45  
46 5 has datas => ( is => 'rw',
47 isa => 'ArrayRef',
48 );
49  
50 5 has config => (
51 isa => "HashRef",
52 is => "rw",
53 );
54  
55 has 'configword' => (
56 is => 'rw',
57 6 ▶ default => sub { __PACKAGE__ },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 5 );
59  
60 5 has 'debug' => (
61 is => 'rw',
62 );
63  
64 has 'backend' => (
65 is => 'rw',
66 isa => 'Str',
67 5 ▶ trigger => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 5 my $self = shift;
69 5 my $backend = shift;
70  
71 5 $self->_log("Loading $backend backend ...");
72 5 $self->load_plugin( $backend );
73 }
74 5 );
75  
76 has _plugin_ns => (
77 is => 'rw',
78 required => 1,
79 isa => 'Str',
80 6 ▶ default => sub{ 'Backend' },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 5 );
82  
83 has tree => (
84 isa => "HashRef",
85 is => "rw",
86 6 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
87 5 );
88  
89  
90 5 has root => (
91 isa => "HashRef",
92 is => "rw",
93 );
94  
95  
96  
97 171 ▶sub _log{
Subroutine _log: called CC=2 SCAR=8.1
98 171 my ($self, $msg ) = @_;
99  
100 171 ▶ return if ! $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debug 171 0 171
101  
102 0 say STDERR "[debug] $msg";
103 }
104  
105 # this method is overridden if a backend is loaded
106 1 ▶sub _load{
Subroutine _load: called CC=1 SCAR=0.0
107 1 my $self = shift;
108  
109 1 return $self->datas;
110 }
111  
112  
113 5 ▶sub load{
Subroutine load: called CC=2 SCAR=6.9
Pod documented
114 5 my $self = shift;
115  
116 5 my $nodes = $self->_load;
117  
118 5 $nodes = $self->_load_order($nodes);
119 5 foreach my $node (@$nodes) {
120 105 $self->add($node);
121 }
122 }
123  
124 5 ▶sub _load_order{
Subroutine _load_order: called CC=8 SCAR=20.9
125 5 my $self = shift;
126 5 my $nodes = shift;
127  
128 5 my @nodes_ordered;
129 5 ▶ if ( defined $self->config && defined $self->config->{backend} && defined $self->config->{backend}->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config and defined $self->config->{"backend"} and defined $self->config->{"backend"}{"args"}) 4 1 5
Condition66%4/6
defined $self->config and defined $self->config->{"backend"} and defined $self->config->{"backend"}{"args"}
!ll&&!rl&&r
104
defined $self->config and defined $self->config->{"backend"}
!ll&&!rl&&r
104
MC/DC33%1/3
defined $self->config and defined $self->config->{"backend"} and defined $self->config->{"backend"}{"args"}
defined $self->config defined $self->config->{"backend"} defined $self->config->{"backend"}{"args"}
Truth table50%2/4
A and B and C
A= defined $self->configB= defined $self->config->{"backend"}C= defined $self->config->{"backend"}{"args"}
ABCresult
0XX0
10X0
1100
1111
130 4 my $load_order = $self->config->{backend}->{args}->{load_order};
131 4 ▶ if ( defined $load_order) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $load_order) 4 0 4
132 4 foreach my $source ( @$load_order ) {
133 12 foreach my $node ( @$nodes ) {
134 252 ▶ if ( $node->{$self->_get_key_name('source', $node)} eq $source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$node{$self->_get_key_name("source", $node)} eq $source) 84 168 252
135 84 push(@nodes_ordered, $node);
136 }
137 }
138 }
139 4 return \@nodes_ordered;
140 }
141 }
142  
143 1 return $nodes;
144 }
145  
146  
147 # removes the child's father
148 21 ▶sub _remove_child_father{
Subroutine _remove_child_father: called CC=4 SCAR=13.9
149 21 my $self = shift;
150 21 my $node = shift;
151  
152 21 my $father = $node->{$self->_get_key_name('parent', $node)};
153 21 my $key_children = $self->_key_children($node, $father);
154 21 my $id = 0;
155 21 foreach my $child ( @{$father->{$key_children}}) {
156 34 ▶ if ( $child->{$self->_get_key_name('search', $child)} eq $node->{$self->_get_key_name('search', $node)} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($$child{$self->_get_key_name("search", $child)} eq $$node{$self->_get_key_name("search", $node)} and $$child{$self->_get_key_name("parent", $child)} eq $$node{$self->_get_key_name("parent", $node)}) 17 17 34
Condition66%2/3
$$child{$self->_get_key_name("search", $child)} eq $$node{$self->_get_key_name("search", $node)} and $$child{$self->_get_key_name("parent", $child)} eq $$node{$self->_get_key_name("parent", $node)}
!ll&&!rl&&r
17017
MC/DC50%1/2
$$child{$self->_get_key_name("search", $child)} eq $$node{$self->_get_key_name("search", $node)} and $$child{$self->_get_key_name("parent", $child)} eq $$node{$self->_get_key_name("parent", $node)}
$$child{$self->_get_key_name("search", $child)} eq $$node{$self->_get_key_name("search", $node)} $$child{$self->_get_key_name("parent", $child)} eq $$node{$self->_get_key_name("parent", $node)}
Truth table66%2/3
A and B
A= $$child{$self->_get_key_name("search", $child)} eq $$node{$self->_get_key_name("search", $node)}B= $$child{$self->_get_key_name("parent", $child)} eq $$node{$self->_get_key_name("parent", $node)}
ABresult
0X0
100
111
157 $child->{$self->_get_key_name('parent', $child)} eq $node->{$self->_get_key_name('parent', $node)} ){
158 17 return splice ( @{$father->{$key_children}},$id,1);
159 }
160 17 $id++;
161 }
162 }
163  
164  
165 347 ▶sub _obj_key {
Subroutine _obj_key: called CC=1 SCAR=0.0
166 347 my $self = shift;
167 347 my $node = shift;
168  
169 347 return $node->{$self->_get_key_name('source', $node)} . '_' . $node->{$self->_get_key_name('primary', $node)};
170 }
171  
172  
173 # return key name of children parent
174 168 ▶sub _key_children {
Subroutine _key_children: called CC=2 SCAR=6.9
175 168 my ($self, $node, $parent) = @_;
176  
177 168 my $key_children;
178 168 my $node_source_key = $self->_get_key_name('source', $node);
179 168 my $parent_source_key = $self->_get_key_name('source', $parent);
180  
181 # if node and parent have the same source
182 168 ▶ if ( $node->{$node_source_key} eq $parent->{$parent_source_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$node_source_key} eq $parent->{$parent_source_key}) { } 112 56 168
183 112 $key_children = 'children';
184 }
185 else {
186 56 $key_children = 'children_' . $node->{$node_source_key};
187 }
188 168 return $key_children;
189 }
190  
191  
192 82 ▶sub search {
Subroutine search: called CC=12 SCAR=24.9
Pod documented
193 82 my ( $self, $args ) = @_;
194  
195 82 my $results = [];
196 82 my $tree = $self->tree;
197 82 foreach my $id ( sort {$a cmp $b} keys %$tree ) {
198  
199 1324 my $found = 1;
200  
201  
202 1324 foreach my $key ( keys %$args ) {
203 1984 my $current;
204 1984 ▶ if ( $key =~ m/(.*)\.(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /(.*)\.(.*)/) { } 56 1928 1984
205 # ex: parent.name
206 56 ▶ if ( defined $tree->{$id}->{$1} && ref($tree->{$id}->{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tree->{$id}{$1} and ref $tree->{$id}{$1}) { } 31 25 56
Condition100%3/3
defined $tree->{$id}{$1} and ref $tree->{$id}{$1}
!ll&&!rl&&r
23231
MC/DC100%2/2
defined $tree->{$id}{$1} and ref $tree->{$id}{$1}
defined $tree->{$id}{$1} ref $tree->{$id}{$1}
Truth table100%3/3
A and B
A= defined $tree->{$id}{$1}B= ref $tree->{$id}{$1}
ABresult
0X0
100
111
207 31 $current = $tree->{$id}->{$1}->{$2};
208 }
209 25 else { next }
210 }
211 else {
212 1928 ▶ if ( defined $tree->{$id}->{$key} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tree->{$id}{$key}) { } 1525 403 1928
213 1525 $current = $tree->{$id}->{$key};
214 }
215 else {
216 #print "the '$key' key is unknown [node obj_key:$id]\n";
217 403 $found = 0;
218 403 next;
219 }
220 }
221 1556 my $value = $args->{$key};
222 1556 ▶ if ( $current ne $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current ne $value) 1170 386 1556
223 1170 $found = 0;
224 1170 last;
225 }
226 }
227  
228 1324 ▶ if ( $found ){
Branch: 1 branch
BranchTrueFalseTotal
if ($found) 91 1233 1324
229 91 ▶ if ( wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 29 62 91
230 29 push(@$results, $tree->{$id});
231 }
232 # if found and scalar context
233 else {
234 62 return $tree->{$id};
235 }
236 }
237 }
238  
239 20 ▶ return 0 if ( ! wantarray && ! $$results[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray or $results->[0] 12 8 20
Condition66%2/3
wantarray or $results->[0]
l!l&&r!l&&!r
8012
MC/DC50%1/2
wantarray or $results->[0]
wantarray $results->[0]
Truth table66%2/3
A or B
A= wantarrayB= $results->[0]
ABresult
1X1
011
000
240  
241 # wantarray
242 8 return @$results;
243 }
244  
245  
246 # ex : search_path(/A/B/C')
247 # or search_path(/A/B/C, { by => 'name', source => 'Page'} )
248 12 ▶sub search_path {
Subroutine search_path: called CC=7 SCAR=19.5
Pod documented
249 12 my ( $self, $source, $path ) = @_;
250  
251 12 ▶ croak "path must be start by '/' !: $!\n" if ( $path !~ m|^/| );
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 12 12
252  
253 12 my $search_key = $self->_get_key_name('search', { source => $source});
254  
255 12 my $nodes = [ split m%/%, $path ];
256 12 $$nodes[0] = '/';
257  
258 12 my $not_found = 0;
259 12 my (@found, @not_found);
260 12 foreach my $node ( @$nodes ) {
261  
262 45 my $result = $self->search({ $search_key => $node, source => $source } );
263  
264 45 ▶ if ( ! $not_found && $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $not_found and $result) { } 33 12 45
Condition100%3/3
not $not_found and $result
!ll&&!rl&&r
6633
MC/DC100%2/2
not $not_found and $result
not $not_found $result
Truth table100%3/3
not(A) and B
A= not $not_foundB= $result
ABresult
0X0
100
111
265 33 push(@found, $result);
266 }
267 else {
268 12 $not_found = 1;
269 12 push(@not_found, $node);
270 }
271 }
272  
273 12 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 3 9 12
274 3 return ( \@found, \@not_found );
275 }
276 else {
277 9 ▶ if ( $not_found[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found[-1]) { } 3 6 9
278 3 return '';
279 }
280 else {
281 6 return $found[-1];
282 }
283 }
284 }
285  
286  
287 25 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
288 25 my $self = shift;
289  
290 25 return scalar keys %{$self->tree};
291 }
292  
293 0 ▶sub dump {
Subroutine dump: not called CC=2 SCAR=17.9
Pod documented
294 0 my $self = shift;
295 0 my $var = shift;
296  
297 0 ▶ $var = $self->tree if ! defined $var;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $var 0 0 0
298 0 $Data::Dumper::Maxdepth = 3;
299 0 $Data::Dumper::Sortkeys = 1;
300 0 $Data::Dumper::Terse = 1;
301 0 return Dumper($var);
302 }
303  
304 104 ▶sub traverse {
Subroutine traverse: called CC=12 SCAR=24.9
Pod documented
305 104 my ($self, $node, $funcref, $args) = @_;
306  
307 104 ▶ return 0 if ( ! $node );
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 104 104
308 104 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
8618
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
309 104 ▶ $args->{_count} = 1 if ! defined ($args->{_count});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"_count"} 21 83 104
310  
311 104 my $hasfunc = 0;
312 104 ▶ if ( ! $funcref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($funcref) 18 86 104
313 18 $hasfunc = 1;
314 80 ▶ $funcref = sub { my ($node, $args) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
315 $args->{_each_nodes} = []
316 80 ▶ if ( ! defined $args->{_each_nodes});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"_each_nodes"} 18 62 80
317 80 ▶ if(defined($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) 80 0 80
318 80 push(@{$args->{_each_nodes}}, $node);
319 80 return 1;
320 }
321 }
322 18 }
323 # if first node
324 104 ▶ if ( $args->{_count} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"_count"} == 1) 21 83 104
325 21 ▶ return 0 if ( ! &$funcref( $node, $args ) )
Branch: 1 branch
BranchTrueFalseTotal
unless &$funcref($node, $args) 0 21 21
326 }
327  
328  
329  
330 104 foreach my $field ( keys %$node) {
331 449 ▶ if ( ref($node->{$field}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{$field} eq "ARRAY") 69 380 449
332 69 foreach my $child ( @{$node->{$field}} ) {
333 83 ▶ return 0 if ( ! &$funcref( $child, $args ) );
Branch: 1 branch
BranchTrueFalseTotal
unless &$funcref($child, $args) 0 83 83
334 83 $args->{_count}++;
335 83 $self->traverse( $child, $funcref, $args );
336 }
337 }
338 }
339  
340 104 ▶ return $args->{_each_nodes} if $hasfunc;
Branch: 1 branch
BranchTrueFalseTotal
if $hasfunc 18 86 104
341 86 return 1;
342 }
343  
344  
345 9 ▶sub del {
Subroutine del: called CC=2 SCAR=6.9
Pod documented
346 9 my ($self, @nodes) = @_;
347  
348 9 my @deleted;
349 9 foreach my $node ( @nodes ) {
350  
351 12 $self->_remove_child_father($node);
352  
353 12 my $key_children = $self->_key_children($node, $node->{parent});
354  
355 # traverse child branches and delete it
356 12 my $Nodes = $self->traverse($node);
357  
358 12 push(@deleted,map { delete $self->tree->{$self->_obj_key($_)} } @$Nodes);
359 }
360 9 return @deleted;
361 }
362  
363  
364  
365 1940 ▶sub _get_key_name {
Subroutine _get_key_name: called CC=5 SCAR=16.2
366 1940 my ($self, $key, $node) = @_;
367  
368 1940 my $values = {
369 source => 'source',
370 primary => 'id',
371 parent => 'parent',
372 search => 'name',
373 };
374  
375 die "the key '$key' is unknown in _get_key_name !"
376 1940 ▶ if ! defined $values->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values->{$key} 0 1940 1940
377  
378 1940 ▶ if ( defined $self->config && defined $node->{source} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->config and defined $node->{"source"}) 1603 337 1940
Condition100%3/3
defined $self->config and defined $node->{"source"}
!ll&&!rl&&r
33161603
MC/DC100%2/2
defined $self->config and defined $node->{"source"}
defined $self->config defined $node->{"source"}
Truth table100%3/3
A and B
A= defined $self->configB= defined $node->{"source"}
ABresult
0X0
100
111
379 1603 my $key_name = $self->config->{backend}->{args}->{sources_name}->{$node->{source}}->{"${key}_key"};
380 1603 ▶ return $key_name || $values->{$key};
Condition66%2/3
$key_name || $values->{$key}
l!l&&r!l&&!r
35812450
MC/DC0%0/2
$key_name || $values->{$key}
$key_name $values->{$key}
Truth table66%2/3
A || B
A= $key_nameB= $values->{$key}
ABresult
1X1
011
000
381 }
382 337 return $values->{$key};
383 };
384  
385  
386 # checks that the necessary keys are set
387 165 ▶sub _normalize_node {
Subroutine _normalize_node: called CC=12 SCAR=25.4
388 165 my ($self, $node, $parent) = @_;
389  
390 # check parent key
391 165 my $primary_key = $self->_get_key_name('primary', $node);
392 die "primary key $primary_key is not set in node " . $self->_obj_key($node) ." !\nAdd 'id' to config args/" . $node->{$self->_get_key_name('source',$node) } . "/columns"
393 165 ▶ if ! defined $node->{$primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{$primary_key} 0 165 165
394  
395 165 my $parent_key = $self->_get_key_name('parent', $node);
396  
397 # transform obj_id to object reference
398 165 foreach my $col ( keys %$node ) {
399  
400 # hash = belongs_to
401 628 ▶ if ( ref($node->{$col}) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node->{$col} eq 'HASH') { } 133 495 628
elsif (ref $node->{$col} eq 'ARRAY') { } 39 456 495
402  
403 133 my @values = keys %{$node->{$col}};
404 # hasref is not already transformed
405 133 ▶ if ( ! defined $values[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $values[1]) 109 24 133
406 109 my $value = $values[0];
407 109 ▶ if ( $value =~ m/^\w*_\d*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\w*_\d*$/) { } 109 0 109
408 109 $node->{$col} = $self->tree->{$value};
409 }
410 else {
411 0 die "Error: Relationship belongs_to is not well formatted : '$value', use Obj_ID as form"
412 }
413 }
414 }
415 # array = has_may
416 elsif ( ref($node->{$col}) eq 'ARRAY') {
417 39 my $newarray = [];
418 39 foreach my $child (@{$node->{$col}}) {
419 12 ▶ if ( $child =~ m/^\w*_\d*$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child =~ /^\w*_\d*$/) { } 6 6 12
elsif (ref $child eq 'HASH') { } 6 0 6
420 6 push(@$newarray, $self->tree->{$child});
421 }
422 elsif ( ref($child) eq 'HASH') {
423 # push(@$newarray, $child);
424 6 my @values = keys %{$child};
425 # hasref is not already transformed
426 6 ▶ if ( ! defined $values[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $values[1]) 6 0 6
427  
428 die "tree->{$values[0]} doesn't exist, can not load " . $self->_obj_key($node) . " !\n"
429 6 ▶ if ( ! exists $self->tree->{$values[0]} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->tree->{$values[0]} 0 6 6
430  
431 6 push(@$newarray, $self->tree->{$values[0]});
432 }
433 }
434 else {
435 0 die "Error : has_many relationship $col (=$child) is not formatted, ex: 'Page_3'\n";
436 }
437 }
438 39 $node->{$col} = $newarray;
439 }
440 }
441  
442 }
443  
444  
445 # Inserts a node beneath the parent
446 150 ▶sub add {
Subroutine add: called CC=5 SCAR=16.1
Pod documented
447 150 my ($self, $node, $parent) = @_;
448  
449 150 $self->_log("Add node ". $self->_obj_key($node));
450  
451 150 my $parent_key = $self->_get_key_name('parent', $node);
452  
453 150 ▶ if ( $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 15 135 150
454 15 $node->{$parent_key} = $parent;
455 }
456  
457 150 $self->_normalize_node($node, $parent);
458  
459 150 ▶ if ( exists $node->{$parent_key} ){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$parent_key}) { } 133 17 150
460 133 ▶ if ( $node->{$parent_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{$parent_key}) { } 124 9 133
461 #print "parent is defined and not null\n";
462 124 my $key_children = $self->_key_children($node, $node->{$parent_key});
463 124 push(@{$node->{$parent_key}->{$key_children}}, $node);
464 }
465 else {
466 #print "it's root\n";
467 9 ▶ if ( $self->root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->root) 3 6 9
468 3 die "root already exist [ node: " .$node->{$self->_get_key_name('source', $node)}. '_' . $node->{$self->_get_key_name('primary', $node)} . " ]!";
469 }
470 6 $self->root($node);
471 }
472 }
473 else {
474 #print "parent is not defined, node is an orphan\n";
475 }
476  
477 # save ref node in tree
478 147 $self->tree->{$self->_obj_key($node)} = $node;
479  
480 147 return $node;
481 }
482  
483 9 ▶sub update {
Subroutine update: called CC=5 SCAR=16.1
Pod documented
484 9 my ($self, $node, $datas) = @_;
485  
486 9 $self->_log("Update node ". $self->_obj_key($node));
487 9 $self->_normalize_node($node);
488  
489 9 foreach my $k ( sort keys %$datas ) {
490  
491 15 ▶ if ( ! defined $node->{$k} ){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $node->{$k}) { } 3 12 15
elsif ($node->{$k} eq $datas->{$k}) { } 4 8 12
492 #$self->_log("update: can not update, node->{$k} is not defined");
493 #next;
494 3 $self->_log("node->{$k} is not defined, continue ...");
495 }
496 4 elsif ( $node->{$k} eq $datas->{$k} ){next}
497  
498 11 my $previous = $node->{$k};
499 11 my $parent = $node->{$self->_get_key_name('parent', $node)};
500 11 my $key_children = $self->_key_children($node, $parent);
501 11 my $children = $parent->{$key_children};
502  
503  
504 11 ▶ if ( $k eq $self->_get_key_name('parent', $node)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq $self->_get_key_name('parent', $node)) { } 9 2 11
505  
506 9 my $old = $self->_remove_child_father($node);
507 9 my $new_parent = $datas->{$k};
508 9 push(@{$new_parent->{$key_children}}, $old);
509  
510 9 $node->{$self->_get_key_name('parent', $node)} = $new_parent;
511 }
512 else {
513 2 $node->{$k} = $datas->{$k};
514 }
515 }
516 9 return $node;
517 }
518  
519  
520 6 ▶sub move {
Subroutine move: called CC=1 SCAR=0.0
Pod documented
521 6 my ($self, $node, $parent) = @_;
522  
523 6 $self->_normalize_node($node);
524 6 return $self->update($node, { $self->_get_key_name('parent', $node) => $parent });
525 }
526  
527 =head1 NAME
528  
529 TreePath - Simple Tree Path!
530  
531 This module is at EXPERIMENTAL stage, so use with caution.
532  
533 =head1 VERSION
534  
535  
536 =head1 SYNOPSIS
537  
538 use TreePath;
539  
540 my $tp = TreePath->new( conf => $conf );
541  
542 # load datas from backend
543 $tp->load;
544  
545 my $tree = $tp->tree;
546  
547 # All nodes are hash
548 # The first is called 'root'
549 my $root = $tp->root;
550  
551 # a node can have children
552 my $children = $root->{children};
553  
554 # or children from another source (if Source has a parent key)
555 my $children = $root->{children_Comment};
556  
557 # also can has_many relationship. it's the same but without parent key
558 my files = $root->{files};
559  
560 =head1 SUBROUTINES/METHODS
561  
562 =head2 new($method => $value)
563  
564 # for now there are two backends : DBIX and File
565 $tp = TreePath->new( conf => 't/conf/treefromdbix.yml')
566 # $tp = TreePath->new( conf => 't/conf/treefromfile.yml')
567  
568 # see t/conf/treepath.yml for hash structure
569 $tp = TreePath->new( datas => $datas);
570  
571 also see t/01-tpath.t
572  
573 =cut
574  
575 =head2 load
576  
577 # The data load is not automatic
578 $tp->load;
579  
580 =cut
581  
582 =head2 tree
583  
584 $tree = $tp->tree;
585  
586 =cut
587  
588 =head2 nodes
589  
590 $root = $tp->root;
591  
592 This is the root node ( a simple hashref )
593 it has no parent.
594  
595 {
596 'id' => '1',
597 'name' => '/',
598 'parent' => '0' # or ''
599 'source' => 'Page'
600 }
601  
602 All keys can be a relation to another source, as belong_to with DBIx like this :
603 {
604 'id' => '15',
605 'name' => '/',
606 'parent' => { 'Page_10' }
607 'source' => 'Comment'
608 }
609  
610 where 'Page' is the source of relationship and '10' the id. So Comment_15 is linked with Page_10.
611  
612 See the dump of a node example :
613  
614  
615 {
616 'id' => 2,
617 'name' => 'A',
618 'source' => 'Page'
619 'parent' => {
620 'children' => [
621 $VAR1
622 ],
623 'children_Comment' => [
624 'HASH(0x52b6df8)',
625 'HASH(0x52aab28)',
626 'HASH(0x527daf8)'
627 ],
628 'files' => [
629 'HASH(0x43de208)',
630 'HASH(0x52d8c80)'
631 ],
632 'id' => 1,
633 'name' => '/',
634 'parent' => undef,
635 'source' => 'Page'
636 },
637 'children' => [
638 {
639 'children' => 'ARRAY(0x52cbd70)',
640 'files' => 'ARRAY(0x52dbc30)',
641 'id' => 3,
642 'name' => 'B',
643 'parent' => $VAR1,
644 'source' => 'Page'
645 },
646 {
647 'children' => 'ARRAY(0x52dbb58)',
648 'children_Comment' => 'ARRAY(0x528ba38)',
649 'files' => 'ARRAY(0x52e45d8)',
650 'id' => 7,
651 'name' => "\x{2665}",
652 'parent' => $VAR1,
653 'source' => 'Page'
654 }
655 ],
656 'children_Comment' => [
657 {
658 'id' => 3,
659 'page' => $VAR1,
660 'source' => 'Comment'
661 },
662 {
663 'id' => 4,
664 'page' => $VAR1,
665 'source' => 'Comment'
666 }
667 ],
668 'files' => [],
669 }
670  
671  
672 => 'parent' is a reference to root node and 'children' is an array containing 2 Pages, children_Comment has 2 Comments
673  
674 =cut
675  
676 =head2 search (hashref)
677  
678 # in scalar context return the first result
679 my $E = $tp->search( { name => 'E', source => 'Page' } );
680  
681 # return all result in array context
682 my @allE = $tp->search( { name => 'E', source => 'Page' } );
683  
684 # It is also possible to specify a particular field of a hash
685 my $B = $tp->search( { name => 'B', source => 'Page', 'parent.name' => 'A'} );
686  
687 =cut
688  
689 =head2 search_path (SOURCE, PATH)
690  
691 # Search a path in a tree
692 # in scalar context return last node (last page in this example)
693 my $c = $tp->search_path('Page', '/A/B/C');
694  
695 # in array context return found and not_found nodes
696 my ($found, $not_found) = $tp->search_path('Page', '/A/B/X/D/E');
697  
698 =cut
699  
700 =head2 dump
701  
702 # dump whole tree
703 print $tp->dump;
704  
705 # dump a node
706 print $tp->dump($c);;
707  
708 =cut
709  
710 =head2 count
711  
712 # return the number of nodes
713 print $tp->count;
714  
715 =cut
716  
717 =head2 traverse ($node, [\&function], [$args])
718  
719 # return an arrayref of nodes
720 my $nodes = $tp->traverse($node);
721  
722 # or use a function on each nodes
723 $tp->traverse($node, \&function, $args);
724  
725 =cut
726  
727 =head2 del ($node)
728  
729 # delete recursively all children and node
730 $deleted = $tp->del($node);
731  
732 # delete several nodes at once
733 @del = $tp->del($n1, $n2, ...);
734  
735 =cut
736  
737 =head2 add ($node, [ $parent ])
738  
739 # add the root
740 $root = $tp->add({ name => '/', source => 'Node', id => 123 });
741  
742 # add a node beneath the parent.
743 $Z = $tp->add({ name => 'Z', source => 'Node' }, $parent);
744  
745 =cut
746  
747 =head2 update ($node, $datas)
748  
749 # update node with somes datas
750 $Z = $tp->update($node, { name => 'new_name' });
751  
752  
753 =cut
754  
755  
756 =head2 move ($node, $parent)
757  
758 # move a node as child of given parent
759 $Z = $tp->move($Z, $X);
760  
761 =cut
762  
763  
764 =head1 AUTHOR
765  
766 Daniel Brosseau, C<< <dab at catapulse.org> >>
767  
768 =head1 BUGS
769  
770 Please report any bugs or feature requests to C<bug-tpath at rt.cpan.org>, or through
771 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=TreePath>. I will be notified, and then you'll
772 automatically be notified of progress on your bug as I make changes.
773  
774  
775  
776  
777 =head1 SUPPORT
778  
779 You can find documentation for this module with the perldoc command.
780  
781 perldoc TreePath
782  
783  
784 You can also look for information at:
785  
786 =over 4
787  
788 =item * RT: CPAN's request tracker (report bugs here)
789  
790 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=TreePath>
791  
792 =item * AnnoCPAN: Annotated CPAN documentation
793  
794 L<http://annocpan.org/dist/TreePath>
795  
796 =item * CPAN Ratings
797  
798 L<http://cpanratings.perl.org/d/TreePath>
799  
800 =item * Search CPAN
801  
802 L<http://search.cpan.org/dist/TreePath/>
803  
804 =back
805  
806  
807 =head1 ACKNOWLEDGEMENTS
808  
809  
810 =head1 LICENSE AND COPYRIGHT
811  
812 Copyright 2014 Daniel Brosseau.
813  
814 This program is free software; you can redistribute it and/or modify it
815 under the terms of the the Artistic License (2.0). You may obtain a
816 copy of the full license at:
817  
818 L<http://www.perlfoundation.org/artistic_license_2_0>
819  
820 Any use, modification, and distribution of the Standard or Modified
821 Versions is governed by this Artistic License. By using, modifying or
822 distributing the Package, you accept this license. Do not use, modify,
823 or distribute the Package, if you do not accept this license.
824  
825 If your Modified Version has been derived from a Modified Version made
826 by someone other than you, you are nevertheless required to ensure that
827 your Modified Version complies with the requirements of this license.
828  
829 This license does not grant you the right to use any trademark, service
830 mark, tradename, or logo of the Copyright Holder.
831  
832 This license includes the non-exclusive, worldwide, free-of-charge
833 patent license to make, have made, use, offer to sell, sell, import and
834 otherwise transfer the Package with respect to any patent claims
835 licensable by the Copyright Holder that are necessarily infringed by the
836 Package. If you institute patent litigation (including a cross-claim or
837 counterclaim) against any party alleging that the Package constitutes
838 direct or contributory patent infringement, then this Artistic License
839 to you shall terminate on the date that such litigation is filed.
840  
841 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
842 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
843 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
844 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
845 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
846 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
847 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
848 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
849  
850  
851 =cut
852  
853 5 1; # End of TreePath
« TreePath/Backend/DBIx.pm ↑ Summary TreePath/Backend/File.pm »