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   10174 use strict;
  12         26  
  12         386  
2 12     12   65 use warnings;
  12         25  
  12         554  
3             package Rubric::EntryTag;
4             # ABSTRACT: a tag on an entry
5             $Rubric::EntryTag::VERSION = '0.156';
6 12     12   784 use String::TagString;
  12         27952  
  12         333  
7              
8             #pod =head1 DESCRIPTION
9             #pod
10             #pod This class provides an interface to tags on Rubric entries. It inherits from
11             #pod Rubric::DBI, which is a Class::DBI class.
12             #pod
13             #pod =cut
14              
15 12     12   63 use base qw(Rubric::DBI);
  12         20  
  12         6566  
16              
17             __PACKAGE__->table('entrytags');
18              
19             #pod =head1 COLUMNS
20             #pod
21             #pod id - a unique identifier
22             #pod entry - the tagged entry
23             #pod tag - the tag itself
24             #pod tag_value - the value of the tag (for tags in "tag:value" form)
25             #pod
26             #pod =cut
27              
28             __PACKAGE__->columns(All => qw(id entry tag tag_value));
29              
30             #pod =head1 RELATIONSHIPS
31             #pod
32             #pod =head2 entry
33             #pod
34             #pod The entry attribute returns a Rubric::Entry.
35             #pod
36             #pod =cut
37              
38             __PACKAGE__->has_a(entry => 'Rubric::Entry');
39              
40             #pod =head1 TRIGGERS
41             #pod
42             #pod =cut
43              
44             __PACKAGE__->add_trigger(before_create => \&_nullify_values);
45             __PACKAGE__->add_trigger(before_update => \&_nullify_values);
46              
47             sub _nullify_values {
48 18     18   14740 my $self = shift;
49             $self->tag_value(undef)
50 18 100 66     231 unless defined $self->{tag_value} and length $self->{tag_value};
51             }
52              
53             #pod =head1 METHODS
54             #pod
55             #pod =head2 related_tags(\@tags)
56             #pod
57             #pod This method returns a reference to an array of tags related to all the given
58             #pod tags. Tags are related if they occur together on entries.
59             #pod
60             #pod =cut
61              
62             sub related_tags {
63 1     1 1 883 my ($self, $tags) = @_;
64 1 50 33     10 return unless $tags and my @tags = @$tags;
65              
66             # or maybe we should throw an exception? -- rjbs, 2006-02-13
67 1 50       2 return [] if grep { $_ eq '@private' } @tags;
  1         8  
68              
69             my $query = q|
70             SELECT DISTINCT tag FROM entrytags
71             WHERE
72 0         0 tag NOT IN (| . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
73             AND tag NOT LIKE '@%'
74             AND | .
75             join ' AND ',
76 0         0 map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
77 0         0 map { $self->db_Main->quote($_) }
  0         0  
78             @tags;
79              
80 0         0 $self->db_Main->selectcol_arrayref($query, undef);
81             }
82              
83             #pod =head3 related_tags_counted(\@tags)
84             #pod
85             #pod This is the obvious conjunction of C and C. It
86             #pod returns an arrayref of arrayrefs, each a pair of tag/occurance values.
87             #pod
88             #pod =cut
89              
90             sub related_tags_counted {
91 23     23 1 12604 my ($self, $tags) = @_;
92 23 100       84 return unless $tags;
93 13 100       91 $tags = [ keys %$tags ] if ref $tags eq 'HASH';
94 13 50       63 return unless my @tags = @$tags;
95              
96             # or maybe we should throw an exception? -- rjbs, 2006-02-13
97 13 100       34 return [] if grep { $_ eq '@private' } @tags;
  13         66  
98              
99             my $query = q|
100             SELECT DISTINCT tag, COUNT(*) AS count
101             FROM entrytags
102             WHERE tag NOT IN (|
103 12         64 . join(',',map { $self->db_Main->quote($_) } @tags) . q|)
104             AND tag NOT LIKE '@%'
105             AND | .
106             join ' AND ',
107 12         613 map { "entry IN (SELECT entry FROM entrytags WHERE tag=$_)" }
108 12         30 map { $self->db_Main->quote($_) }
  12         877  
109             @tags;
110              
111 12         46 $query .= " GROUP BY tag";
112              
113 12         43 $self->db_Main->selectall_arrayref($query, undef);
114             }
115              
116             #pod =head2 stringify_self
117             #pod
118             #pod =cut
119              
120             sub stringify_self {
121 0     0 1   my ($self) = @_;
122 0           String::TagString->string_from_tags({
123             $self->tag => $self->tag_value
124             });
125             }
126              
127             1;
128              
129             __END__