File Coverage

blib/lib/Palm/Progect/Record.pm
Criterion Covered Total %
statement 16 53 30.1
branch 0 18 0.0
condition 0 8 0.0
subroutine 6 10 60.0
pod 3 5 60.0
total 25 94 26.6


line stmt bran cond sub pod time code
1              
2             package Palm::Progect::Record;
3              
4              
5 7     7   39 use base 'Palm::Progect::VersionDelegator';
  7         11  
  7         4066  
6 7     7   38 use Carp;
  7         14  
  7         348  
7 7     7   32 use strict;
  7         14  
  7         195  
8              
9 7     7   5385 use CLASS;
  7         1941  
  7         33  
10 7     7   264 use base qw(Class::Accessor);
  7         15  
  7         12580  
11              
12             sub Accessors {
13 7     7 0 133 qw(
14             description
15             note
16             type
17             priority
18             completed
19             completed_actual
20             completed_limit
21             level
22             has_next
23             has_child
24             has_prev
25             is_opened
26             has_todo
27             date_due
28             todo_link_data
29             )
30             }
31              
32             CLASS->mk_accessors(CLASS->Accessors);
33              
34             my %Categories = ('Unfiled' => 0); # name => id
35              
36             # Class method to set all available category names at once,
37             # using the Palm::StdAppinfo format of a list of hashrefs
38             # in the form of:
39             # { name => 'Some cat', id => 7, renamed => 'who cares' }
40             #
41             sub set_categories {
42 0     0 1   my $class = shift;
43              
44 0           my @categories = @_;
45              
46 0 0 0       if (!@categories or $categories[0]{'name'} !~ /^\s*unfiled\s*$/i ) {
47 0           unshift @categories, { 'name' => 'Unfiled' };
48             }
49              
50 0           %Categories = ();
51              
52             # I don't know what the category ids are used
53             # for, considering the 'category number'
54             # associated with a pdb record refers to
55             # the category's *position* within this array,
56             # not its 'id'.
57              
58 0           for (my $id = 0; $id < @categories; $id++) {
59              
60 0           my $name = $categories[$id]{'name'};
61              
62 0 0         next unless $id; # Skip 'Unfiled'
63 0 0         next unless $name; # Skip blank categories
64              
65 0           $Categories{$name} = $id;
66             }
67              
68             # Add the 'Unfiled' category, which is
69             # always zero.
70 0           $Categories{'Unfiled'} = 0;
71             }
72              
73             # Class method to get all available category names, in
74             # the order of their category ids
75             sub get_categories {
76 0     0 1   my $class = shift;
77              
78             # Since the keys and values are both meant to be
79             # unique, we can reverse the %Categories hash:
80              
81 0           my %categories_by_id = reverse %Categories;
82              
83 0           my @categories;
84 0           for my $id (sort { $a <=> $b } keys %categories_by_id) {
  0            
85 0           push @categories, {
86             id => $id,
87             name => $categories_by_id{$id},
88             renamed => 0,
89             };
90             }
91              
92 0 0         return @categories if wantarray;
93 0           return \@categories;
94             }
95              
96             # object method accessor
97             sub category_name {
98 0     0 1   my $self = shift;
99 0 0         if (defined $_[0]) {
100 0           my $category_name = $_[0];
101              
102 0           $self->{category_name} = $category_name;
103              
104 0 0         if (not exists $Categories{$category_name}) {
105             # Put this category_name in the Class-global %Categories
106             # hash, setting it's category_id to the max number
107             # of categories that are already there, plus one
108 0           $Categories{$category_name} = (scalar keys %Categories);
109             }
110             }
111 0 0 0       if ($self->{category_name} and $self->{category_name} eq 'Unfiled') {
112 0           return '';
113             }
114             else {
115 0           return $self->{category_name};
116             }
117             }
118              
119             # In order to assign a category id to a record, the category
120             # must already exist; i.e. it must have been set via
121             # the class methods set_categories or add_categories
122              
123             sub category_id {
124 0     0 0   my $self = shift;
125 0           my $category_id = shift;
126              
127 0 0         if (defined $category_id) {
128              
129              
130             # Since both keys and values of %Categories are unique,
131             # we can reverse the hash...
132 0           my %cat_lookup = reverse %Categories;
133              
134             # Internally, we only maintain the category_name
135             # and we lookup the id if its requested.
136             # So if someone sets the category id, we actually
137             # look up the category_name for that id and store it instead
138              
139 0 0         if (exists $cat_lookup{$category_id}) {
140 0           $self->{'category_name'} = $cat_lookup{$category_id};
141             }
142             else {
143 0           croak "There is no category with the id #$category_id. Before setting a record's category call Palm::Progect::Records->set_categories with the complete list of categories\n";
144             }
145             }
146              
147 0   0       my $cat_name = $self->{'category_name'} || 'Unfiled';
148 0           return $Categories{$cat_name};
149             }
150              
151             1;
152              
153             __END__