| 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::Link 0.157; | ||||||||||
| 4 | # ABSTRACT: a link (URI) against which entries have been made | ||||||||||
| 5 | |||||||||||
| 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 | ▶ | use base qw(Rubric::DBI); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 12 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 12 | __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 | 12 | __PACKAGE__->columns(All => qw(id uri md5)); | |||||||||
| 28 | |||||||||||
| 29 | 12 | __PACKAGE__->add_constraint('scheme', uri => \&_check_schema); | |||||||||
| 30 | 5 | ▶ | sub _check_schema { | ||||||||
|
Subroutine
_check_schema: called CC=4 SCAR=20.4
| |||||||||||
| 31 | 5 | my ($uri) = @_; | |||||||||
| 32 | 5 | ▶ | return 1 unless $uri; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 5 | ▶ | return 1 unless Rubric::Config->allowed_schemes; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 0 | ▶ | $uri = URI->new($uri) unless ref $uri; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | return scalar grep { $_ eq $uri->scheme } @{ Rubric::Config->allowed_schemes } | |||||||||
| 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<entries>. | ||||||||||
| 44 | #pod | ||||||||||
| 45 | #pod =cut | ||||||||||
| 46 | |||||||||||
| 47 | 12 | __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 | 12 | __PACKAGE__->set_sql( | |||||||||
| 56 | entry_count => "SELECT COUNT(*) FROM entries WHERE link = ?" | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | 93 | ▶ | sub entry_count { | ||||||||
|
Subroutine
entry_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 60 | 93 | my ($self) = @_; | |||||||||
| 61 | 93 | my $sth = $self->sql_entry_count; | |||||||||
| 62 | 93 | $sth->execute($self->id); | |||||||||
| 63 | 93 | $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 | 12 | __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 | 2 | ▶ | sub tags_counted { | ||||||||
|
Subroutine
tags_counted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 82 | 2 | my ($self) = @_; | |||||||||
| 83 | 2 | my $sth = $self->sql_tags_counted; | |||||||||
| 84 | 2 | $sth->execute($self->id); | |||||||||
| 85 | 2 | my $tags = $sth->fetchall_arrayref; | |||||||||
| 86 | 2 | 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 | 1 | ▶ | deflate => sub { (shift)->canonical->as_string } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 12 | ); | |||||||||
| 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 | 561 | ▶ | sub stringify_self { $_[0]->uri->as_string } | ||||||||
|
Subroutine
stringify_self: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 112 | |||||||||||
| 113 | 12 | __PACKAGE__->add_trigger(before_create => \&_set_md5); | |||||||||
| 114 | |||||||||||
| 115 | 6 | ▶ | sub _set_md5 { | ||||||||
|
Subroutine
_set_md5: called CC=1 SCAR=0.0
| |||||||||||
| 116 | 6 | my ($self) = @_; | |||||||||
| 117 | 6 | $self->_attribute_store(md5 => md5_hex("$self->{uri}")); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 12 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||