blib/lib/Yote/Server/ListContainer.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 157 CC 10 SCAR 21.6
CC
10
Cov
0%
remove_entry
30.0
update
12.0
add_entry
12.0
CRAP 8.7
1 package Yote::Server::ListContainer;
2  
3 #
4 # Caveats. Be careful
5 # * with gather/gather_all
6 # * when using js clearCache
7 # * about asyn with yote object methods
8 # * about 'use Foo' for ListContainer list objects
9 #
10  
11  
12 use strict;
13 use warnings;
14 no warnings 'uninitialized';
15  
16 use Yote::Server;
17 use base 'Yote::ServerObj';
18  
19 # --vvv override -------
20 sub _allowedUpdates { [qw(name notes)] } #override returning a list of allowed updates
21  
22 sub _lists { {} } # override with list-name -> class
23  
24  
25 # new item added to this object
26 sub _on_add { my($self,$listName,$obj,$moreArgs) =@_; }
27 sub _on_remove { my($self,$listName,$obj,$moreArgs) =@_; }
28  
29 # what to run when added to something
30 sub _when_added {
31 my($self,$toObj,$listName,$itemArgs) = @_;
32 $self->update( $itemArgs );
33 }
34  
35 # what to run when this is removed from something
36 sub _when_removed {
37 my($self,$fromObj,$listName,$itemArgs) = @_;
38 }
39  
40 sub _gather {}
41  
42 sub _init {
43 my $self = shift;
44 my $listhash = $self->_lists;
45 for my $list (keys %$listhash) {
46 $self->set( $list, [] );
47 }
48 }
49  
50 sub calculate {} #override
51  
52 # --^^^ override -------
53  
54 sub _valid_choice { return 1; }
55  
56 sub __allowedUpdates {
57 map { $_ => 1 } @{shift->_allowedUpdates()};
58 }
59  
60 sub update {
61 my( $self, $updates ) = @_;
62 my %allowed = $self->__allowedUpdates;
63 for my $fld (keys %$updates) {
64 die "Cant update '$fld' in ".ref($self) unless $allowed{$fld};
65 my $val = $updates->{$fld};
66 die "Cant update '$fld' to $val in " . ref($self) unless $self->_valid_choice($fld,$val);
67 $self->set( $fld, $val )
68 }
69 $self->calculate;
70 } #update
71  
72 sub add_entry {
73 my( $self, $args ) = @_;
74
75 my($listName, $obj, $itemArgs, $parentArgs ) = @$args{'listName', 'item', 'itemArgs', 'parentArgs'};
76
77 my $class = $self->_lists->{$listName};
78
79 die "Unknown list '$listName' in ".ref($self) unless $class;
80 die "Cannot add this choice to list $listName in ".ref($self) unless $self->_valid_choice( $listName, $obj );
81 my $list = $self->get( $listName, [] );
82 $obj //= $self->{STORE}->newobj( {
83 parent => $self,
84 name => $listName.' '.(1 + @$list),
85 },$class );
86 $obj->_when_added( $self, $listName, $itemArgs );
87 $self->_on_add( $listName, $obj, $parentArgs );
88 push @$list, $obj;
89 $self->select_current( $listName, $obj );
90 $self->calculate;
91 $obj, $obj->gather_all;
92 } #add_entry
93  
94 sub select_current {
95 my( $self, $listName, $item ) = @_;
96 die "Unknown list '$listName' in ".ref($self) unless $self->_lists->{$listName};
97 $self->set( "current_$listName", $item );
98 $item;
99 } #select_current
100  
101 sub gather {
102 my $self = shift;
103 my $listhash = $self->_lists;
104 my @res;
105 for my $list (keys %$listhash) {
106 my $l = $self->get( $list, [] );
107 push @res, $l, @$l;
108 }
109 @res, $self->_gather;
110 } #gather
111  
112 sub gather_all {
113 my $self = shift;
114 my $seen = shift || {};
115 my $listhash = $self->_lists;
116 my @res;
117 for my $list (keys %$listhash) {
118 my $l = $self->get( $list, [] );
119 push @res, $l, (map { $_, $_->gather_all($seen) } grep { ref($_) && ! $seen->{$_->{ID}}++ } @$l);
120 }
121 @res, $self->_gather;
122 } #gather_all
123  
124 sub remove_entry { #TODO - paramertize this like add_entry does
125 my( $self, $item, $from, $moreArgs ) = @_;
126 die "Unknown list '$from' in ".ref($self) unless $self->_lists->{$from};
127 my $list = $self->get($from);
128 for( my $i=0; $i<@$list; $i++ ) {
129 if( $list->[$i] == $item ) {
130 splice @$list, $i, 1;
131 if( @$list ) {
132 $i-- if $i > $#$list;
133 $self->select_current( $from, $list->[$i] );
134 } else {
135 $self->select_current( $from, undef );
136 }
137 }
138 }
139 $self->_on_remove( $from, $item, $moreArgs );
140 $item->_when_removed( $self, $from, $moreArgs );
141 $self->calculate;
142 return $item;
143 } #remove_entry
144  
145 # TODO - implement a copy?
146 1;
147  
148 __END__