CheckPad/CheckList.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 175 CC 16 SCAR 23.8
CC
16
Cov
0%
insert
30.0
checkitems
20.0
retrieve
12.0
CRAP 10.8
1 package WWW::CheckPad::CheckList;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use Class::Accessor;
7 use WWW::CheckPad::CheckItem;
8 use WWW::CheckPad::Base;
9 use WWW::CheckPad::ListParser;
10 use base qw(Class::Accessor
11 WWW::CheckPad::Base
12 );
13  
14  
15 __PACKAGE__->mk_accessors(qw(
16 id
17 title
18 ));
19  
20 sub _entity {
21 my ($class, %value_of) = @_;
22 my $self = bless {}, $class;
23  
24 $self->id($value_of{id});
25 $self->title($value_of{title});
26  
27 if (not $value_of{ lazy_loading } and $value_of{ id }) {
28 $self->_load_checkitems();
29 }
30 return $self;
31 }
32  
33 ##############################################################################
34 ## Instance Method
35 ##############################################################################
36 sub update {
37 my $self = shift;
38  
39 $self->connection->_clear_cookie_of('sys_msg');
40 my $content = $self->connection->_request({
41 path => '/',
42 form => {
43 mode => 'pjt',
44 act => 'edit',
45 id => $self->id,
46 ttl => $self->title,
47 }
48 });
49 my $sys_msg = $self->connection->_get_cookie_of('sys_msg');
50 croak "There was error while inserting new CheckList: " . $sys_msg if ($sys_msg);
51
52 return $self;
53 }
54  
55  
56 sub delete {
57 my $self = shift;
58  
59 $self->connection->_clear_cookie_of('sys_msg');
60 my $response = $self->connection->_request({
61 path => '/',
62 form => {
63 mode => 'pjt',
64 act => 'del',
65 id => $self->id,
66 }
67 });
68  
69 my $sys_msg = $self->connection->_get_cookie_of('sys_msg');
70  
71 croak "There was error while inserting new CheckList."if (not $sys_msg);
72  
73 $self->id(0);
74 $self->title(q{});
75 }
76  
77  
78 sub add_checkitem {
79 my ($self, $checkitem_title) = @_;
80  
81 my $current_checkitems = $self->checkitems();
82  
83 ## Insert the item to check*pad.
84 my $checkitem = WWW::CheckPad::CheckItem->_insert(
85 $self,
86 $checkitem_title
87 );
88  
89 ## push into the cached items.
90 push @{$current_checkitems}, $checkitem;
91  
92 return $checkitem;
93 }
94  
95  
96 sub checkitems {
97 my ($self) = @_;
98  
99 unless ($self->{_cp_loaded_checkitems}) {
100 $self->_load_checkitems();
101 }
102 return $self->{_cp_loaded_checkitems} ?
103 (wantarray) ? @{$self->{_cp_checkitems}} : $self->{_cp_checkitems} :
104 undef;
105 }
106  
107  
108 sub _remove_checkitem_from_cache {
109 my ($self, $checkitem_to_be_removed) = @_;
110 my $checkitems = $self->checkitems;
111 ## TODO: Doesn't look like good way to remove the value from array.
112  
113 SEARCH:
114 foreach my $n (0..$#{$checkitems}) {
115 my $checkitem = $checkitems->[$n];
116 if ($checkitem->id == $checkitem_to_be_removed->id) {
117 splice @{$checkitems}, $n, 1;
118 last SEARCH;
119 }
120 }
121 }
122  
123 ##############################################################################
124 ## Class Method
125 ##############################################################################
126 sub retrieve_all {
127 my ($class) = @_;
128  
129 croak "Have to login before calling retrieve_all()."
130 unless ($class->connection and $class->connection->has_logged_in);
131  
132 my $response = $class->connection->_request({
133 path => '',
134 form => {
135 mode => 'pjt',
136 act => 'sort'
137 }
138 });
139  
140 # parse the contents with ListParser.
141 my $parser = WWW::CheckPad::ListParser->new();
142 my @check_list_infos = @{$parser->convert_to_item($response->content)};
143 my @check_lists;
144 foreach my $check_list_info (@check_list_infos) {
145 my $check_list = WWW::CheckPad::CheckList->_entity(
146 id => $check_list_info->{id},
147 title => $check_list_info->{title}
148 );
149 push @check_lists, $check_list;
150 }
151  
152 return @check_lists;
153 }
154  
155  
156 sub retrieve {
157 my ($class, $id) = @_;
158  
159 croak "Have to call retrieve with ID of ChechList." unless $id;
160  
161 # Currently, we don't have no way to get only one CheckList.
162 # So, just get all and filter afterword.
163 foreach my $check_list ($class->retrieve_all()) {
164 return $check_list if $check_list->id == $id
165 }
166 return undef;
167 }
168  
169  
170 sub insert {
171 my $class = shift;
172 my $checklist = undef;
173  
174 ## TODO: Reduce if-elsif-else ...
175 if (ref $_[0] eq 'HASH') {
176 $checklist = WWW::CheckPad::CheckList->_entity(title => $_[0]->{title});
177 }
178 elsif (not defined ref $_[0]) {
179 $checklist = WWW::CheckPad::CheckList->_entity(title => $_[0]);
180 }
181 else {
182 $checklist = $_[0];
183 }
184  
185 $class->connection->_clear_cookie_of('sys_msg');
186 my $response = $class->connection->_request({
187 path => 'index.php',
188 form => {
189 mode => 'pjt',
190 act => 'add',
191 ajax => 1,
192 ttl => $checklist->title,
193 }
194 });
195 $checklist->title($class->connection->_jconvert($checklist->title, 'euc-jp'));
196
197 my $sys_msg = $class->connection->_get_cookie_of('sys_msg');
198 croak "There was error while inserting new CheckList." if (not $sys_msg);
199  
200 if ($response->header('location') =~ /id=([0-9]+)&/) {
201 $checklist->id($1);
202 }
203 else {
204 croak "There were error while inserting new CheckList.";
205 }
206  
207 return $checklist;
208 }
209  
210  
211  
212  
213 sub _load_checkitems {
214 my ($self) = @_;
215  
216 croak "Have to set id before loading checkitems from server." unless ($self->id);
217  
218 ## TODO: Should check return value or cache exception to handle error.
219 my @checkitems = WWW::CheckPad::CheckItem->retrieve_all_of($self->id);
220 $self->{_cp_checkitems} = \@checkitems;
221 $self->{_cp_loaded_checkitems} = 1;
222  
223 return $self->{_cp_loaed_checkitems};
224 }
225  
226  
227 ##############################################################################
228 1;
229 __END__