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   14020 use strict;
  12         24  
  12         416  
2 12     12   65 use warnings;
  12         22  
  12         695  
3             package Rubric::Link;
4             # ABSTRACT: a link (URI) against which entries have been made
5             $Rubric::Link::VERSION = '0.156';
6             #pod =head1 DESCRIPTION
7             #pod
8             #pod This class provides an interface to links in the Rubric. It inherits from
9             #pod Rubric::DBI, which is a Class::DBI class.
10             #pod
11             #pod =cut
12              
13 12     12   66 use base qw(Rubric::DBI);
  12         21  
  12         1595  
14              
15 12     12   76 use Digest::MD5 qw(md5_hex);
  12         23  
  12         5636  
16              
17             __PACKAGE__->table('links');
18              
19             #pod =head1 COLUMNS
20             #pod
21             #pod id - a unique identifier
22             #pod uri - the link itself
23             #pod md5 - the hex md5sum of the link's URI (set automatically)
24             #pod
25             #pod =cut
26              
27             __PACKAGE__->columns(All => qw(id uri md5));
28              
29             __PACKAGE__->add_constraint('scheme', uri => \&_check_schema);
30             sub _check_schema {
31 5     5   5811 my ($uri) = @_;
32 5 50       26 return 1 unless $uri;
33 5 50       45 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             #pod =head1 RELATIONSHIPS
39             #pod
40             #pod =head2 entries
41             #pod
42             #pod Every link has_many Rubric::Entries, available with the normal methods,
43             #pod including C.
44             #pod
45             #pod =cut
46              
47             __PACKAGE__->has_many(entries => 'Rubric::Entry');
48              
49             #pod =head3 entry_count
50             #pod
51             #pod This method returns the number of entries that refer to this link.
52             #pod
53             #pod =cut
54              
55             __PACKAGE__->set_sql(
56             entry_count => "SELECT COUNT(*) FROM entries WHERE link = ?"
57             );
58              
59             sub entry_count {
60 91     91 1 15734 my ($self) = @_;
61 91         370 my $sth = $self->sql_entry_count;
62 91         12848 $sth->execute($self->id);
63 91         15841 $sth->fetchall_arrayref->[0][0];
64             }
65              
66             #pod =head3 tags_counted
67             #pod
68             #pod This returns an arrayref of arrayrefs, each containing a tag name and the
69             #pod number of entries for this link tagged with that tag. The pairs are sorted in
70             #pod colation order by tag name.
71             #pod
72             #pod =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 7 my ($self) = @_;
83 2         15 my $sth = $self->sql_tags_counted;
84 2         656 $sth->execute($self->id);
85 2         476 my $tags = $sth->fetchall_arrayref;
86 2         16 return $tags;
87             }
88              
89             #pod =head1 INFLATIONS
90             #pod
91             #pod =head2 uri
92             #pod
93             #pod The uri column inflates to a URI object.
94             #pod
95             #pod =cut
96              
97             __PACKAGE__->has_a(
98             uri => 'URI',
99             deflate => sub { (shift)->canonical->as_string }
100             );
101              
102             #pod =head1 METHODS
103             #pod
104             #pod =head2 stringify_self
105             #pod
106             #pod This method returns the link's URI as a string, and is teh default
107             #pod stringification for Rubric::Link objects.
108             #pod
109             #pod =cut
110              
111 525     525 1 127804 sub stringify_self { $_[0]->uri->as_string }
112              
113             __PACKAGE__->add_trigger(before_create => \&_set_md5);
114              
115             sub _set_md5 {
116 6     6   1314 my ($self) = @_;
117 6         71 $self->_attribute_store(md5 => md5_hex("$self->{uri}"));
118             }
119              
120             1;
121              
122             __END__