File Coverage

blib/lib/Rubric/EntryTag.pm
Criterion Covered Total %
statement 30 37 81.0
branch 11 14 78.5
condition 3 6 50.0
subroutine 7 8 87.5
pod 3 3 100.0
total 54 68 79.4


line stmt bran cond sub pod time code
1 12     12   5141 use strict;
  12         23  
  12         329  
2 12     12   50 use warnings;
  12         20  
  12         397  
3             # ABSTRACT: a tag on an entry
4              
5             use String::TagString;
6 12     12   336  
  12         16164  
  12         257  
7             #pod =head1 DESCRIPTION
8             #pod
9             #pod This class provides an interface to tags on Rubric entries. It inherits from
10             #pod Rubric::DBI, which is a Class::DBI class.
11             #pod
12             #pod =cut
13              
14             use base qw(Rubric::DBI);
15 12     12   48  
  12         22  
  12         5169  
16             __PACKAGE__->table('entrytags');
17              
18             #pod =head1 COLUMNS
19             #pod
20             #pod id - a unique identifier
21             #pod entry - the tagged entry
22             #pod tag - the tag itself
23             #pod tag_value - the value of the tag (for tags in "tag:value" form)
24             #pod
25             #pod =cut
26              
27             __PACKAGE__->columns(All => qw(id entry tag tag_value));
28              
29             #pod =head1 RELATIONSHIPS
30             #pod
31             #pod =head2 entry
32             #pod
33             #pod The entry attribute returns a Rubric::Entry.
34             #pod
35             #pod =cut
36              
37             __PACKAGE__->has_a(entry => 'Rubric::Entry');
38              
39             #pod =head1 TRIGGERS
40             #pod
41             #pod =cut
42              
43             __PACKAGE__->add_trigger(before_create => \&_nullify_values);
44             __PACKAGE__->add_trigger(before_update => \&_nullify_values);
45              
46             my $self = shift;
47             $self->tag_value(undef)
48 18     18   13170 unless defined $self->{tag_value} and length $self->{tag_value};
49             }
50 18 100 66     132  
51             #pod =head1 METHODS
52             #pod
53             #pod =head2 related_tags(\@tags)
54             #pod
55             #pod This method returns a reference to an array of tags related to all the given
56             #pod tags. Tags are related if they occur together on entries.
57             #pod
58             #pod =cut
59              
60             my ($self, $tags) = @_;
61             return unless $tags and my @tags = @$tags;
62              
63 1     1 1 1234 # or maybe we should throw an exception? -- rjbs, 2006-02-13
64 1 50 33     8 return [] if grep { $_ eq '@private' } @tags;
65              
66             my $query = q|
67 1 50       3 SELECT DISTINCT tag FROM entrytags
  1         8  
68             WHERE
69             tag NOT IN (| . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
70             AND tag NOT LIKE '@%'
71             AND | .
72 0         0 join ' AND ',
73             map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
74             map { $self->db_Main->quote($_) }
75             @tags;
76 0         0  
77 0         0 $self->db_Main->selectcol_arrayref($query, undef);
  0         0  
78             }
79              
80 0         0 #pod =head3 related_tags_counted(\@tags)
81             #pod
82             #pod This is the obvious conjunction of C<related_tags> and C<tags_counted>. It
83             #pod returns an arrayref of arrayrefs, each a pair of tag/occurance values.
84             #pod
85             #pod =cut
86              
87             my ($self, $tags) = @_;
88             return unless $tags;
89             $tags = [ keys %$tags ] if ref $tags eq 'HASH';
90             return unless my @tags = @$tags;
91 23     23 1 12394  
92 23 100       79 # or maybe we should throw an exception? -- rjbs, 2006-02-13
93 13 100       64 return [] if grep { $_ eq '@private' } @tags;
94 13 50       95  
95             my $query = q|
96             SELECT DISTINCT tag, COUNT(*) AS count
97 13 100       32 FROM entrytags
  13         53  
98             WHERE tag NOT IN (|
99             . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
100             AND tag NOT LIKE '@%'
101             AND | .
102             join ' AND ',
103 12         48 map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
104             map { $self->db_Main->quote($_) }
105             @tags;
106              
107 12         462 $query .= " GROUP BY tag";
108 12         26  
  12         832  
109             $self->db_Main->selectall_arrayref($query, undef);
110             }
111 12         41  
112             #pod =head2 stringify_self
113 12         32 #pod
114             #pod =cut
115              
116             my ($self) = @_;
117             String::TagString->string_from_tags({
118             $self->tag => $self->tag_value
119             });
120             }
121 0     0 1    
122 0           1;
123              
124              
125             =pod
126              
127             =encoding UTF-8
128              
129             =head1 NAME
130              
131             Rubric::EntryTag - a tag on an entry
132              
133             =head1 VERSION
134              
135             version 0.157
136              
137             =head1 DESCRIPTION
138              
139             This class provides an interface to tags on Rubric entries. It inherits from
140             Rubric::DBI, which is a Class::DBI class.
141              
142             =head1 PERL VERSION
143              
144             This code is effectively abandonware. Although releases will sometimes be made
145             to update contact info or to fix packaging flaws, bug reports will mostly be
146             ignored. Feature requests are even more likely to be ignored. (If someone
147             takes up maintenance of this code, they will presumably remove this notice.)
148             This means that whatever version of perl is currently required is unlikely to
149             change -- but also that it might change at any new maintainer's whim.
150              
151             =head1 COLUMNS
152              
153             id - a unique identifier
154             entry - the tagged entry
155             tag - the tag itself
156             tag_value - the value of the tag (for tags in "tag:value" form)
157              
158             =head1 RELATIONSHIPS
159              
160             =head2 entry
161              
162             The entry attribute returns a Rubric::Entry.
163              
164             =head1 TRIGGERS
165              
166             =head1 METHODS
167              
168             =head2 related_tags(\@tags)
169              
170             This method returns a reference to an array of tags related to all the given
171             tags. Tags are related if they occur together on entries.
172              
173             =head3 related_tags_counted(\@tags)
174              
175             This is the obvious conjunction of C<related_tags> and C<tags_counted>. It
176             returns an arrayref of arrayrefs, each a pair of tag/occurance values.
177              
178             =head2 stringify_self
179              
180             =head1 AUTHOR
181              
182             Ricardo SIGNES <rjbs@semiotic.systems>
183              
184             =head1 COPYRIGHT AND LICENSE
185              
186             This software is copyright (c) 2004 by Ricardo SIGNES.
187              
188             This is free software; you can redistribute it and/or modify it under
189             the same terms as the Perl 5 programming language system itself.
190              
191             =cut