File Coverage

blib/lib/Data/Taxonomy/Tags/Tag.pm
Criterion Covered Total %
statement 29 30 96.6
branch 10 12 83.3
condition 3 6 50.0
subroutine 9 9 100.0
pod 4 4 100.0
total 55 61 90.1


line stmt bran cond sub pod time code
1             package Data::Taxonomy::Tags::Tag;
2              
3             use overload
4 131     131   10175 '""' => sub { shift->as_string },
5 4     4   21 fallback => 1;
  4         8  
  4         30  
6              
7             # Constants for separator and category
8 4     4   249 use constant SPLIT => 0;
  4         9  
  4         217  
9 4     4   21 use constant JOIN => 1;
  4         8  
  4         1628  
10              
11             =head1 NAME
12              
13             Data::Taxonomy::Tags::Tag - Represents a single tag
14              
15             =head1 SYNOPSIS
16              
17             print $tag->name, " (category: ", $tag->category, ")\n";
18              
19             =head1 DESCRIPTION
20              
21             Data::Taxonomy::Tags::Tag represents a single tag for a Data::Taxonomy::Tags
22             object.
23              
24             =head2 Methods
25              
26             =over 12
27              
28             =item new
29              
30             Creates a new instance of the class representing a single tag. Requires two
31             arguments (the input tag to parse and separator arrayref). You shouldn't
32             have to use this method yourself.
33              
34             =cut
35             sub new {
36 73     73 1 115 my ($class, $tag, $opt) = @_;
37              
38 73         269 my $self = bless {
39             input => $tag,
40             separator => $opt->{separator},
41             }, $class;
42            
43 73         140 $self->_process;
44            
45 73         112 *name = \&tag;
46            
47 73         381 return $self;
48             }
49              
50             =item tag
51              
52             =item name
53              
54             Returns the name of the tag (that is, the tag itself) sans the category bit.
55              
56             =cut
57             sub tag {
58 210     210 1 253 my ($self, $v) = @_;
59 210 100       407 $self->{tag} = $v
60             if defined $v;
61 210         762 return $self->{tag};
62             }
63              
64             =item category
65              
66             Returns the category the tag is in. If there is no category, then undef
67             is returned;
68              
69             =cut
70             sub category {
71 218     218 1 307 my ($self, $v) = @_;
72 218 100       412 $self->{category} = $v
73             if defined $v;
74 218         1530 return $self->{category};
75             }
76              
77             sub _process {
78 73     73   75 my $self = shift;
79 73         679 my ($one, $two) = split /$self->{separator}[SPLIT]/, $self->{input};
80 73 100 66     470 if (defined $one and defined $two) {
    50 33        
81 24         55 $self->tag($two);
82 24         44 $self->category($one);
83             }
84             elsif (defined $one and not defined $two) {
85 49         88 $self->tag($one);
86             }
87             else {
88             # Ack! Weird data.
89 0         0 $self->tag($self->{input});
90             }
91             }
92              
93             =item as_string
94              
95             Returns the full tag as a string (that is, the category, the category seperator,
96             and the tag name all concatenated together). Overloading is used as well to
97             automatically call this method if the object is used in a string context.
98              
99             =cut
100             sub as_string {
101 131     131 1 652 my $self = shift;
102            
103 131 100       810 return defined $self
    50          
104             ? defined $self->category
105             ? $self->category . $self->{separator}[JOIN] . $self->tag
106             : $self->tag
107             : undef;
108             }
109              
110             =back
111              
112             =head1 BUGS
113              
114             All bugs, open and resolved, are handled by RT at
115             L.
116              
117             Please report all bugs via
118             L.
119              
120             =head1 LICENSE
121              
122             Copyright 2005, Thomas R. Sibley.
123              
124             You may use, modify, and distribute this package under the same terms as Perl itself.
125              
126             =head1 AUTHOR
127              
128             Thomas R. Sibley, L
129              
130             =cut
131              
132             42;