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

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 145 CC 9 SCAR 17.8
CC
9
Cov
0%
update
12.0
add_entry
12.0
remove_entry
12.0
CRAP 5.9
1 package Yote::Server::ListContainer;
2  
3 #
4 # Caveats. Be careful
5 # * with gather
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 vars qw($VERSION);
17  
18 $VERSION = '1.0';
19  
20 use Yote::Server;
21 use base 'Yote::ServerObj';
22  
23 # --vvv override -------
24 sub _allowedUpdates { qw(name notes) } #override returning a list of allowed updates
25  
26 sub _lists { {} } # override with list-name -> class
27  
28 sub _gather {} # override with extra stuff to send across
29  
30 sub _init {
31 my $self = shift;
32 my $listhash = $self->_lists;
33 for my $list (keys %$listhash) {
34 $self->set( $list, [] );
35 }
36 }
37  
38 sub _calculate {} #override
39  
40 # --^^^ override -------
41  
42 sub _valid_choice { return 1; }
43  
44 sub _addEditAccess {
45 my( $self, $acct ) = @_;
46 my $acls = $self->get__acls({});
47 $acls->{$acct} = 1;
48 $self->set__has_acls( 0 < keys %$acls );
49 }
50 sub _removeEditAccess {
51 my( $self, $acct ) = @_;
52 my $acls = $self->get__acls({});
53 delete $acls->{$acct};
54 $self->set__has_acls( 0 < keys %$acls );
55 }
56  
57 sub __allowedUpdates {
58 my $self = shift;
59 die "Cant update" if $self->get__has_acls && ! $self->get__acls->{$self->{SESSION}{acct} };
60 $self->{__ALLOWED} //= { map { $_ => 1 } ($self->_allowedUpdates) };
61 }
62  
63 sub update {
64 my( $self, $updates ) = @_;
65 my %allowed = %{$self->__allowedUpdates};
66 for my $fld (keys %$updates) {
67 die "Cant update '$fld' in ".ref($self) unless $allowed{$fld};
68 my $val = $updates->{$fld};
69 die "Cant update '$fld' to $val in " . ref($self)
70 unless $self->_valid_choice($fld,$val);
71 $self->set( $fld, $val )
72 }
73 $self->_calculate( 'update', $updates );
74 } #update
75  
76 sub add_entry {
77 my( $self, $listName, $obj ) = @_;
78
79 my $class = $self->_lists->{$listName};
80
81 die "Unknown list '$listName' in ".ref($self) unless $class;
82 die "Cannot add this choice to list $listName in ".ref($self)
83 unless $self->_valid_choice( $listName, $obj );
84  
85 my $list = $self->get( $listName, [] );
86 $obj //= $self->{STORE}->newobj( {
87 parent => $self,
88 name => $listName.' '.(1 + @$list),
89 },$class );
90 $obj->get_parent( $self );
91  
92 push @$list, $obj;
93 $obj->_calculate( 'added_to_list', $listName, $self );
94 $self->_calculate( 'new_entry', $listName, $obj, scalar(@$list) );
95 $obj, $obj->gather;
96 } #add_entry
97  
98 sub gather {
99 my $self = shift;
100 my $seen = shift || {};
101 return if $seen->{$self->{ID}}++;
102 my $listhash = $self->_lists;
103 my @res;
104 for my $list (keys %$listhash) {
105 my $l = $self->get( $list, [] );
106 push @res, $l, (map { $_, $_->gather($seen) } grep { ref($_) } @$l);
107 }
108 @res, $self->_gather( $seen );
109 } #gather
110  
111 sub remove_entry { #TODO - paramertize this like add_entry does
112 my( $self, $item, $from, $moreArgs ) = @_;
113 die "Unknown list '$from' in ".ref($self) unless $self->_lists->{$from};
114 my $list = $self->get($from);
115 my $i = 0;
116 my $removed;
117 for( $i=0; $i<@$list; $i++ ) {
118 if( $list->[$i] == $item ) {
119 $removed = splice @$list, $i, 1;
120 last;
121 }
122 }
123 $self->_calculate( 'removed_entry', $from, $removed, $i );
124  
125 } #remove_entry
126  
127 # TODO - implement a copy?
128 1;
129  
130 __END__