Rubric/Entry.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 94.4% 17 / 18 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 95.4% 21 / 22 Podpod 88.8% 8 / 9 totaltotal 94.1% 129 / 137 CC 11 SCAR 6.1
CC
11
Cov
96%
tags_from_string
3.0
markup
3.0
_create_times
3.0
CRAP 1.8
« Rubric/WebApp/Session.pm ↑ Summary Rubric/WebApp/Login/Post.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Rubric::Entry 0.157;
4 # ABSTRACT: a single entry made by a user
5  
6 12 ▶use parent qw(Rubric::DBI);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Class::DBI::utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #pod =head1 DESCRIPTION
11 #pod
12 #pod This class provides an interface to Rubric entries. It inherits from
13 #pod Rubric::DBI, which is a Class::DBI class.
14 #pod
15 #pod =cut
16  
17 12 ▶use Encode 2 qw(_utf8_on);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Rubric::Entry::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use String::TagString;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 12 __PACKAGE__->table('entries');
23  
24 #pod =head1 COLUMNS
25 #pod
26 #pod id - a unique identifier
27 #pod link - the link to which the entry refers
28 #pod username - the user who made the entry
29 #pod title - the title of the link's destination
30 #pod description - a short description of the entry
31 #pod body - a long body of text for the entry
32 #pod created - the time when the entry was first created
33 #pod modified - the time when the entry was last modified
34 #pod
35 #pod =cut
36  
37 12 __PACKAGE__->columns(
38 All => qw(id link username title description body created modified)
39 );
40  
41 12 __PACKAGE__->utf8_columns(qw( title description body ));
42  
43 #pod =head1 RELATIONSHIPS
44 #pod
45 #pod =head2 link
46 #pod
47 #pod The link attribute returns a Rubric::Link.
48 #pod
49 #pod =cut
50  
51 12 __PACKAGE__->has_a(link => 'Rubric::Link');
52  
53 #pod =head2 uri
54 #pod
55 #pod The uri attribute returns the URI of the entry's link.
56 #pod
57 #pod =cut
58  
59 4 ▶sub uri { my ($self) = @_; return unless $self->link; $self->link->uri; }
Subroutine uri: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $self->link 2 2 4
60  
61 #pod =head2 username
62 #pod
63 #pod The user attribute returns a Rubric::User.
64 #pod
65 #pod =cut
66  
67 12 __PACKAGE__->has_a(username => 'Rubric::User');
68  
69 #pod =head2 tags
70 #pod
71 #pod Every entry has_many tags that describe it. The C<tags> method will return the
72 #pod tags, and the C<entrytags> method will return the Rubric::EntryTag objects that
73 #pod represent them.
74 #pod
75 #pod =cut
76  
77 12 __PACKAGE__->has_many(entrytags => 'Rubric::EntryTag');
78 12 __PACKAGE__->has_many(tags => [ 'Rubric::EntryTag' => 'tag' ]);
79  
80 #pod =head3 recent_tags_counted
81 #pod
82 #pod This method returns a reference to an array of arrayrefs, each a (tag, count)
83 #pod pair for tags used on the week's 50 most recent entries.
84 #pod
85 #pod =cut
86  
87 12 __PACKAGE__->set_sql(recent_tags_counted => <<'');
88 SELECT tag, COUNT(*) as count
89 FROM entrytags
90 WHERE entry IN (SELECT id FROM entries WHERE created > ? LIMIT 100)
91 AND tag NOT LIKE '@%%'
92 AND entry NOT IN (SELECT entry FROM entrytags WHERE tag = '@private')
93 GROUP BY tag
94 ORDER BY count DESC
95 LIMIT 50
96  
97 11 ▶sub recent_tags_counted {
Subroutine recent_tags_counted: called CC=1 SCAR=0.0
Pod documented
98 11 my ($class) = @_;
99 11 my $sth = $class->sql_recent_tags_counted;
100 11 $sth->execute(time - (86400 * 7));
101 11 my $result = $sth->fetchall_arrayref;
102 11 return $result;
103 }
104  
105 #pod =head1 INFLATIONS
106 #pod
107 #pod =head2 created
108 #pod
109 #pod =head2 modified
110 #pod
111 #pod The created and modified columns are stored as seconds since epoch, but
112 #pod inflated to Time::Piece objects.
113 #pod
114 #pod =cut
115  
116 __PACKAGE__->has_a(
117 $_ => 'Time::Piece',
118 deflate => 'epoch',
119 0 ▶ inflate => Rubric::Config->display_localtime ? sub { localtime($_[0]) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 186 ▶ : sub { gmtime($_[0]) }
Subroutine __ANON__: called CC=1 SCAR=0.0
121 12 ▶) for qw(created modified);
Branch: 1 branch
BranchTrueFalseTotal
'Rubric::Config'->display_localtime ? : 0 24 24
122  
123 12 __PACKAGE__->add_trigger(before_create => \&_default_title);
124  
125 12 __PACKAGE__->add_trigger(before_create => \&_create_times);
126 12 __PACKAGE__->add_trigger(before_update => \&_update_times);
127  
128 12 ▶sub _default_title {
Subroutine _default_title: called CC=2 SCAR=6.9
129 12 my $self = shift;
130 $self->title('(default)') unless $self->{title}
131 12 ▶}
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"title"} 11 1 12
132  
133 12 ▶sub _create_times {
Subroutine _create_times: called CC=3 SCAR=11.0
134 12 my $self = shift;
135 12 ▶ $self->created(scalar gmtime) unless defined $self->{created};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"created"} 11 1 12
136 12 ▶ $self->modified(scalar gmtime) unless defined $self->{modified};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"modified"} 11 1 12
137 }
138  
139 29 ▶sub _update_times {
Subroutine _update_times: called CC=1 SCAR=0.0
140 29 my $self = shift;
141 29 $self->modified(scalar gmtime);
142 }
143  
144 #pod =head1 METHODS
145 #pod
146 #pod =head2 query(\%arg)
147 #pod
148 #pod The arguments to C<query> provide a set of abstract constraints for the query.
149 #pod These are sent to Rubric::Entry::Query, which builds an SQL query and returns
150 #pod the result of running it. (Either a list or an Iterator is returned.)
151 #pod
152 #pod (The built-in Class::DBI search method can't handle this kind of search.)
153 #pod
154 #pod user - entries for this User
155 #pod tags - entries with these tags (arrayref)
156 #pod link - entries for this Link
157 #pod urimd5 - entries for the Link with this md5 sum
158 #pod has_body - whether entries must have bodies (T, F, or undef)
159 #pod has_link - whether entries must have a link (T, F, or undef)
160 #pod (time spec) - {created,modified}_{before,after,on}
161 #pod limits entries by time; given as a complete or partial
162 #pod time and date string in the form "YYYY-MM-DD HH:MM"
163 #pod
164 #pod =cut
165  
166 47 ▶sub query {
Subroutine query: called CC=1 SCAR=0.0
Pod documented
167 47 my $self = shift;
168 47 require Rubric::Entry::Query;
169 47 Rubric::Entry::Query->query(@_);
170 }
171  
172 #pod =head2 set_new_tags(\%tags)
173 #pod
174 #pod This method replaces all entry's current tags with the new set of tags.
175 #pod
176 #pod =cut
177  
178 13 ▶sub set_new_tags {
Subroutine set_new_tags: called CC=2 SCAR=6.9
Pod documented
179 13 my ($self, $tags) = @_;
180 13 $self->entrytags->delete_all;
181 13 $self->update;
182  
183 13 ▶ while (my ($tag, $value) = each %$tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$tags) 17 13 30
184 17 $self->add_to_tags({ tag => $tag, tag_value => $value });
185 }
186 }
187  
188 #pod =head2 tags_from_string
189 #pod
190 #pod my $tags = Rubric::Entry->tags_from_string($string);
191 #pod
192 #pod This (class) method takes a string of tags, delimited by whitespace, and
193 #pod returns an array of the tags, throwing an exception if it finds invalid tags.
194 #pod
195 #pod Valid tags (shouldn't this be documented somewhere else instead?) may contain
196 #pod letters, numbers, underscores, colons, dots, and asterisks. Hyphens me be
197 #pod used, but not as the first character.
198 #pod
199 #pod =cut
200  
201 35 ▶sub tags_from_string {
Subroutine tags_from_string: called CC=3 SCAR=11.0
Pod documented
202 35 my ($class, $tagstring) = @_;
203  
204 35 ▶ return {} unless $tagstring and $tagstring =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tagstring and $tagstring =~ /\S/ 6 29 35
Condition66%2/3
$tagstring and $tagstring =~ /\S/
!ll&&!rl&&r
6029
MC/DC50%1/2
$tagstring and $tagstring =~ /\S/
$tagstring $tagstring =~ /\S/
Truth table66%2/3
A and B
A= $tagstringB= $tagstring =~ /\S/
ABresult
0X0
100
111
205  
206 29 String::TagString->tags_from_string($tagstring);
207 }
208  
209 #pod =head2 C< markup >
210 #pod
211 #pod This method returns the value of the entry's @markup tag, or C<_default> if
212 #pod there is no such tag.
213 #pod
214 #pod =cut
215  
216 20 ▶sub markup {
Subroutine markup: called CC=3 SCAR=11.0
Pod documented
217 20 my ($self) = @_;
218  
219 20 my ($tag)
220 = Rubric::EntryTag->search({ entry => $self->id, tag => '@markup' });
221  
222 20 ▶ return ($tag and $tag->tag_value) ? $tag->tag_value : '_default';
Branch: 1 branch
BranchTrueFalseTotal
$tag && $tag->tag_value ? : 2 18 20
Condition66%2/3
$tag and $tag->tag_value
!ll&&!rl&&r
1802
MC/DC50%1/2
$tag and $tag->tag_value
$tag $tag->tag_value
Truth table66%2/3
A and B
A= $tagB= $tag->tag_value
ABresult
0X0
100
111
223 }
224  
225  
226 #pod =head2 C< body_as >
227 #pod
228 #pod my $formatted_body = $entry->body_as("html");
229 #pod
230 #pod This method returns the body of the entry, formatted into the given format. If
231 #pod the entry cannot be rendered into the given format, an exception is thrown.
232 #pod
233 #pod =cut
234  
235 18 ▶sub body_as {
Subroutine body_as: called CC=1 SCAR=0.0
Pod documented
236 18 my ($self, $format) = @_;
237  
238 18 my $markup = $self->markup;
239  
240 18 Rubric::Entry::Formatter->format({
241 text => $self->body,
242 markup => $markup,
243 format => $format
244 });
245 }
246  
247 96 ▶sub accessor_name_for {
Subroutine accessor_name_for: called CC=2 SCAR=6.9
Pod documented
248 96 my ($class, $field) = @_;
249  
250 96 ▶ return 'user' if $field eq 'username';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "username" 12 84 96
251  
252 84 return $field;
253 }
254  
255 ## return retrieve_all'd objects in recent-to-older order
256  
257 12 __PACKAGE__->set_sql(RetrieveAll => <<'');
258 SELECT __ESSENTIAL__
259 FROM __TABLE__
260 ORDER BY created DESC
261  
262 1 ▶sub tagstring {
Subroutine tagstring: called CC=1 SCAR=0.0
Pod undocumented
263 1 my ($self) = @_;
264 String::TagString->string_from_tags({
265 1 map {; $_->tag => $_->tag_value } $self->entrytags
266 });
267 }
268  
269 12 1;
270  
271 __END__
« Rubric/WebApp/Session.pm ↑ Summary Rubric/WebApp/Login/Post.pm »