CheckPad/CheckItem.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 167 CC 17 SCAR 24.7
CC
17
Cov
0%
delete
20.0
finish
20.0
unfinish
20.0
CRAP 11.8
1 package WWW::CheckPad::CheckItem;
2  
3 use strict;
4 use warnings;
5 use Carp;
6 use Class::Accessor;
7 use WWW::CheckPad::Base;
8 use WWW::CheckPad::TodoParser;
9 use base qw(Class::Accessor
10 WWW::CheckPad::Base
11 );
12  
13 __PACKAGE__->mk_accessors(qw(
14 id
15 title
16 is_finished
17 finished_time
18 checklist
19 ));
20  
21 # DO NOT USE THIS.
22 sub _new {
23 my ($class, %value_of) = @_;
24 my $self = bless {}, $class;
25  
26 $self->id($value_of{id});
27 $self->title($value_of{title});
28  
29 return $self;
30 }
31  
32  
33 ## Creates insntance without inserting to the server.
34 sub _entity {
35 my ($class, %value_of) = @_;
36 my $self = bless {}, $class;
37  
38 $self->id($value_of{id});
39 $self->title($value_of{title});
40 $self->is_finished($value_of{is_finished});
41 $self->finished_time($value_of{finished_time});
42 $self->checklist($value_of{checklist});
43  
44 return $self;
45 }
46  
47  
48  
49 ##############################################################################
50 ## Instance Method
51 ##############################################################################
52  
53 sub update {
54 my $self = shift;
55
56 croak("User haven't logged in") unless $self->connection->has_logged_in;
57  
58 $self->connection->_clear_cookie_of('sys_msg');
59 my $response = $self->connection->_request({
60 path => '/',
61 encoding => 'utf8',
62 form => {
63 mode => 'ms',
64 act => 'edit',
65 id => $self->id,
66 ttl => $self->title
67 }
68 });
69  
70 ## Error handling.
71 if ($response->content =~ /DB *Error/) {
72 croak "There were error during updating CheckItem(id=".$self->id.").";
73 }
74  
75 return 1;
76 }
77  
78  
79 sub delete {
80 my $self = shift;
81  
82 croak("User haven't logged in") unless $self->connection->has_logged_in;
83  
84 my $action = $self->is_finished() ? 'del_done' : 'del_notyet';
85
86 $self->connection->_clear_cookie_of('sys_msg');
87 my $response = $self->connection->_request({
88 path => '/',
89 form => {
90 mode => 'ms',
91 act => $action,
92 id => $self->id(),
93 }
94 });
95  
96 ## Error handling.
97 if ($response->content =~ /DB *Error/) {
98 croak "There were error during updating CheckItem(id=".$self->id.").";
99 }
100  
101 $self->checklist->_remove_checkitem_from_cache($self);
102  
103 return 1;
104 }
105  
106  
107 sub finish {
108 my $self = shift;
109  
110 croak("User haven't logged in") unless $self->connection->has_logged_in();
111 croak("Cannot finish the item which are already finished.") if $self->is_finished();
112  
113 my $response = $self->connection->_request({
114 path => '/',
115 form => {
116 mode => 'ms',
117 act => 'finish',
118 id => $self->id(),
119 }
120 });
121
122 $self->connection->_clear_cookie_of('sys_msg');
123  
124 ## Error handling.
125 if ($response->content =~ /DB *Error/) {
126 croak "There were error during updating CheckItem(id=".$self->id.").";
127 }
128 $self->is_finished(1);
129  
130 return 1;
131 }
132  
133  
134 sub unfinish {
135 my $self = shift;
136  
137 croak("User haven't logged in") unless $self->connection->has_logged_in();
138 croak("Cannot unfinish the item which are not yet finished.") unless $self->is_finished();
139  
140 my $response = $self->connection->_request({
141 path => '/',
142 form => {
143 mode => 'ms',
144 act => 'unfinish',
145 id => $self->id(),
146 }
147 });
148
149 $self->connection->_clear_cookie_of('sys_msg');
150  
151 ## Error handling.
152 if ($response->content =~ /DB *Error/) {
153 croak "There were error during updating CheckItem(id=".$self->id.").";
154 }
155 $self->is_finished(0);
156
157 return 1;
158 }
159  
160  
161 ##############################################################################
162 ## Class Method
163 ##############################################################################
164 sub retrieve_all_of {
165 my ($class) = shift;
166  
167 croak "User haven't logged in" unless $class->connection->has_logged_in;
168  
169 my $checklist_id = ((ref $_[0]) eq 'WWW::CheckPad::CheckList')?
170 $_[0]->id:
171 $_[0];
172  
173 my $response = $class->connection->_request({
174 path => '/',
175 form => {
176 mode => 'pjt',
177 act => 'detail',
178 id => $checklist_id
179 }
180 });
181 my $parser = WWW::CheckPad::TodoParser->new();
182 my @checkitem_infos = @{$parser->convert_to_item($response->content)};
183 my @checkitems = ();
184  
185  
186 foreach my $checkitem_info (@checkitem_infos) {
187 my $checkitem = WWW::CheckPad::CheckItem->_entity(
188 id => $checkitem_info->{id},
189 title => $checkitem_info->{title},
190 is_finished => $checkitem_info->{is_finished},
191 finished_time => $checkitem_info->{finished_time},
192 );
193 push @checkitems, $checkitem;
194 }
195 return @checkitems;
196 }
197  
198  
199 sub _insert {
200 my ($class, $checklist, $checkitem_title) = @_;
201  
202 croak("User haven't logged in") unless $class->connection->has_logged_in();
203  
204 my $response = $class->connection->_request({
205 path => 'index.php',
206 encoding => 'euc-jp',
207 form => {
208 mode => 'ms',
209 act => 'add',
210 ajax => 1,
211 pjt_id => $checklist->id,
212 ttl => $checkitem_title
213 }
214 });
215  
216 my $new_checkitem_id;
217 if ($response->content =~ /<div id="new_id_value".*>([0-9]+)<\/div>/) {
218 $new_checkitem_id = $1;
219 }
220 else {
221 croak "There were error while inserting new CheckItem " .
222 "(HTML of check*pad might changed).";
223 }
224  
225 my $checkitem = WWW::CheckPad::CheckItem->_entity(
226 id => $new_checkitem_id,
227 title => $checkitem_title,
228 is_finished => 0,
229 checklist => $checklist,
230 );
231  
232 return $checkitem;
233 }
234  
235  
236 sub dumper {
237 my ($self, $table, $indent) = @_;
238  
239 foreach my $key (keys %{$table}) {
240 my $value = $table->{$key};
241 if (ref $value eq 'HASH' or (ref $value) =~ /HTTP/) {
242 $self->dumper($value, $indent + 4);
243 }
244 else {
245 printf "%s%s = %s(%s)\n", ' ' x $indent, $key, $value, ref $value;
246 }
247 }
248 }
249  
250  
251  
252 ##############################################################################
253 1;
254 __END__