File Coverage

blib/lib/Rubric/Link.pm
Criterion Covered Total %
statement 27 31 87.1
branch 2 6 33.3
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 41 49 83.6


line stmt bran cond sub pod time code
1 12     12   14221 use strict;
  12         22  
  12         503  
2 12     12   61 use warnings;
  12         24  
  12         698  
3             package Rubric::Link;
4             # ABSTRACT: a link (URI) against which entries have been made
5             $Rubric::Link::VERSION = '0.155';
6             # =head1 DESCRIPTION
7             #
8             # This class provides an interface to links in the Rubric. It inherits from
9             # Rubric::DBI, which is a Class::DBI class.
10             #
11             # =cut
12              
13 12     12   65 use base qw(Rubric::DBI);
  12         21  
  12         1711  
14              
15 12     12   71 use Digest::MD5 qw(md5_hex);
  12         21  
  12         5239  
16              
17             __PACKAGE__->table('links');
18              
19             # =head1 COLUMNS
20             #
21             # id - a unique identifier
22             # uri - the link itself
23             # md5 - the hex md5sum of the link's URI (set automatically)
24             #
25             # =cut
26              
27             __PACKAGE__->columns(All => qw(id uri md5));
28              
29             __PACKAGE__->add_constraint('scheme', uri => \&_check_schema);
30             sub _check_schema {
31 1     1   1255 my ($uri) = @_;
32 1 50       5 return 1 unless $uri;
33 1 50       10 return 1 unless Rubric::Config->allowed_schemes;
34 0 0       0 $uri = URI->new($uri) unless ref $uri;
35 0         0 return scalar grep { $_ eq $uri->scheme } @{ Rubric::Config->allowed_schemes }
  0         0  
  0         0  
36             }
37              
38             # =head1 RELATIONSHIPS
39             #
40             # =head2 entries
41             #
42             # Every link has_many Rubric::Entries, available with the normal methods,
43             # including C.
44             #
45             # =cut
46              
47             __PACKAGE__->has_many(entries => 'Rubric::Entry');
48              
49             # =head3 entry_count
50             #
51             # This method returns the number of entries that refer to this link.
52             #
53             # =cut
54              
55             __PACKAGE__->set_sql(
56             entry_count => "SELECT COUNT(*) FROM entries WHERE link = ?"
57             );
58              
59             sub entry_count {
60 92     92 1 16857 my ($self) = @_;
61 92         404 my $sth = $self->sql_entry_count;
62 92         21642 $sth->execute($self->id);
63 92         22309 $sth->fetchall_arrayref->[0][0];
64             }
65              
66             # =head3 tags_counted
67             #
68             # This returns an arrayref of arrayrefs, each containing a tag name and the
69             # number of entries for this link tagged with that tag. The pairs are sorted in
70             # colation order by tag name.
71             #
72             # =cut
73              
74             __PACKAGE__->set_sql(tags_counted => <<'' );
75             SELECT DISTINCT tag, COUNT(*) AS count
76             FROM entrytags
77             WHERE entry IN (SELECT id FROM entries WHERE link = ?)
78             GROUP BY tag
79             ORDER BY tag
80              
81             sub tags_counted {
82 2     2 1 8 my ($self) = @_;
83 2         12 my $sth = $self->sql_tags_counted;
84 2         576 $sth->execute($self->id);
85 2         484 my $tags = $sth->fetchall_arrayref;
86 2         17 return $tags;
87             }
88              
89             # =head1 INFLATIONS
90             #
91             # =head2 uri
92             #
93             # The uri column inflates to a URI object.
94             #
95             # =cut
96              
97             __PACKAGE__->has_a(
98             uri => 'URI',
99             deflate => sub { (shift)->canonical->as_string }
100             );
101              
102             # =head1 METHODS
103             #
104             # =head2 stringify_self
105             #
106             # This method returns the link's URI as a string, and is teh default
107             # stringification for Rubric::Link objects.
108             #
109             # =cut
110              
111 555     555 1 161736 sub stringify_self { $_[0]->uri->as_string }
112              
113             __PACKAGE__->add_trigger(before_create => \&_set_md5);
114              
115             sub _set_md5 {
116 2     2   717 my ($self) = @_;
117 2         23 $self->_attribute_store(md5 => md5_hex("$self->{uri}"));
118             }
119              
120             1;
121              
122             __END__