Jiffy/TimeEntry.pm

filter: Statementstmt 32.4% 25 / 77 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 37.5% 6 / 16 Podpod 100% 7 / 7 totaltotal 28.3% 38 / 134 CC 15 SCAR 22.7
CC
15
Cov
23%
duration
20.0
search
20.0
__ANON__
12.0
CRAP 9.7
« Jiffy/Util/Duration.pm ↑ Summary Jiffyd.pm »
1 package App::Jiffy::TimeEntry;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MongoDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MongoDB::OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 my $collection = 'timeEntry';
13  
14 has id => (
15 is => 'rw',
16 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
17 0 ▶ die 'id must be a MongoDB::OID'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) and $_[0]->isa("MongoDB::OID") 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa("MongoDB::OID")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("MongoDB::OID")
builtin::blessed($_[0]) $_[0]->isa("MongoDB::OID")
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("MongoDB::OID")
ABresult
0X0
100
111
18 unless blessed $_[0] and $_[0]->isa('MongoDB::OID');
19 },
20 2 );
21  
22 has start_time => (
23 is => 'rw',
24 0 ▶ isa => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
25 0 ▶ die 'start_time is not a DateTime object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) and $_[0]->isa("DateTime") 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa("DateTime")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("DateTime")
builtin::blessed($_[0]) $_[0]->isa("DateTime")
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("DateTime")
ABresult
0X0
100
111
26 unless blessed $_[0] and $_[0]->isa('DateTime');
27 },
28 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 DateTime->now();
30 },
31 2 );
32  
33 2 has title => (
34 is => 'rw',
35 required => 1,
36 );
37  
38 2 has _duration => ( is => 'rw', );
39  
40 2 has cfg => ( is => 'ro', );
41  
42 =head2 db
43  
44 C<db> get the db handler from MongoDB
45  
46 =cut
47  
48 0 ▶sub db {
Subroutine db: not called CC=2 SCAR=17.9
Pod documented
49 0 my $self = shift;
50 0 my $client = MongoDB::MongoClient->new;
51 0 ▶ return $client->get_database( ( $self->cfg ) ? $self->cfg->{db} : 'jiffy' );
Branch: 1 branch
BranchTrueFalseTotal
$self->cfg ? : 0 0 0
52 }
53  
54 =head2 save
55  
56 C<save> will commit the current state of the C<TimeEntry> to the database.
57  
58 =cut
59  
60 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
61 0 my $self = shift;
62 0 my $_id = $self->id;
63 0 my $document = {
64 start_time => $self->start_time,
65 title => $self->title,
66 };
67 0 ▶ if ($_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_id) { } 0 0 0
68  
69 # Update the existing record
70 0 $self->db->get_collection($collection)
71 ->update_one( { _id => $self->id }, { '$set' => $document } );
72 } else {
73  
74 # Insert new record
75 0 my $result = $self->db->get_collection($collection)->insert_one($document);
76  
77 # Update id
78 0 $self->id( $result->inserted_id );
79 }
80 }
81  
82 =head2 duration
83  
84 C<duration> returns the time between this entry and the next.
85  
86 =cut
87  
88 0 ▶sub duration {
Subroutine duration: not called CC=4 SCAR=30.0
Pod documented
89 0 my $self = shift;
90 0 my $set = shift;
91  
92 0 ▶ if ($set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set) 0 0 0
93 0 $self->_duration($set);
94 }
95  
96 0 ▶ if ( $self->_duration ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_duration) 0 0 0
97 0 return $self->_duration;
98 }
99  
100 0 my @entry_after = App::Jiffy::TimeEntry::search(
101 $self->cfg,
102 query => {
103 start_time => {
104 '$gt' => $self->start_time,
105 },
106 },
107 sort => {
108 start_time => 1,
109 },
110 limit => 1,
111 );
112  
113 0 ▶ if (@entry_after) {
Branch: 1 branch
BranchTrueFalseTotal
if (@entry_after) { } 0 0 0
114 0 return $entry_after[0]->start_time->subtract_datetime( $self->start_time );
115 } else {
116 0 return DateTime->now->subtract_datetime( $self->start_time );
117 }
118 }
119  
120 =head2 find
121  
122 C<find> will return a single document. The query will use the provided C<_id>.
123  
124 =cut
125  
126 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
127 0 my $cfg = shift;
128 0 my $_id = shift;
129  
130 0 my $client = MongoDB::MongoClient->new;
131 0 my $entry = $client->get_database( $cfg->{db} )->get_collection($collection)
132 ->find_one( { _id => $_id } );
133 0 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 0 0
134 return App::Jiffy::TimeEntry->new(
135 id => $entry->{_id},
136 title => $entry->{title},
137 start_time => $entry->{start_time},
138 0 cfg => $cfg,
139 );
140 }
141  
142 =head2 search C<$config> C<%options>...
143  
144 C<search> will return an array of TimeEntry that fit the given C<%options>.
145  
146 C<%options> can include the following:
147  
148 =over
149  
150 =item C<query>
151  
152 This is the query syntax available via L<MongoDB::Collection>.
153  
154 =item C<sort>
155  
156 If present, the results will be sorted via the hashref given by this option.
157  
158 =item C<limit>
159  
160 If present, the results will limited to the number provided.
161  
162 =back
163  
164 =cut
165  
166 0 ▶sub search {
Subroutine search: not called CC=4 SCAR=30.0
Pod documented
167 0 my $cfg = shift;
168 0 my %options = @_;
169 0 my $query = $options{query};
170 0 my $sort = $options{sort};
171 0 my $limit = $options{limit};
172  
173 0 my $client = MongoDB::MongoClient->new;
174 0 my $entries = $client->get_database( $cfg->{db} )->get_collection($collection)
175 ->find($query);
176  
177 0 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 0 0 0
178 0 $entries = $entries->sort($sort);
179 }
180  
181 0 ▶ if ($limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
182 0 $entries = $entries->limit($limit);
183 }
184  
185 # Return undef if nothing was found
186 0 ▶ return unless $entries;
Branch: 1 branch
BranchTrueFalseTotal
unless $entries 0 0 0
187  
188 0 my $LocalTZ = DateTime::TimeZone->new( name => 'local' ); # For caching
189  
190 # @TODO create a subclass of the MongoDB cursor that allows chaining of results like MongoDB
191 map {
192 0 $_->{start_time}->set_time_zone($LocalTZ); # Convert to local tz
193 App::Jiffy::TimeEntry->new(
194 id => $_->{_id},
195 title => $_->{title},
196 start_time => $_->{start_time},
197 0 cfg => $cfg,
198 )
199 } $entries->all;
200 }
201  
202 =head2 last_entry
203  
204 C<last_entry> will return the last TimeEntry in the db.
205  
206 =cut
207  
208 0 ▶sub last_entry {
Subroutine last_entry: not called CC=2 SCAR=17.9
Pod documented
209 0 my $cfg = shift;
210  
211 0 my $client = MongoDB::MongoClient->new;
212 0 my $entry = $client->get_database( $cfg->{db} )->get_collection($collection)
213 ->find->sort( { start_time => -1 } )->limit(1)->next;
214 0 ▶ return unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 0 0
215 return App::Jiffy::TimeEntry->new(
216 id => $entry->{_id},
217 title => $entry->{title},
218 start_time => $entry->{start_time},
219 0 cfg => $cfg,
220 );
221 }
222  
223 =head2 TO_JSON
224  
225 C<TO_JSON> will return the entry as a JSON convertible hash.
226  
227 =cut
228  
229 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
230 0 my $self = shift;
231  
232 {
233 0 start_time => $self->start_time->iso8601,
234 title => $self->title,
235 duration => { $self->duration->deltas },
236 };
237 }
238  
239 2 1;
« Jiffy/Util/Duration.pm ↑ Summary Jiffyd.pm »