File Coverage

blib/lib/OurCal/View.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 4 0.0
condition 0 3 0.0
subroutine 3 6 50.0
pod 3 3 100.0
total 15 43 34.8


line stmt bran cond sub pod time code
1             package OurCal::View;
2              
3 1     1   874 use strict;
  1         3  
  1         48  
4 1     1   7 use UNIVERSAL::require;
  1         2  
  1         10  
5 1         9 use Module::Pluggable sub_name => '_views',
6 1     1   30 search_path => 'OurCal::View';
  1         3  
7              
8              
9             =head2 NAME
10              
11             OurCal::View - base class for all OourCal views
12              
13             =head1 METHODS
14              
15             =cut
16              
17             =head2 new
18              
19             =cut
20              
21             sub new {
22 0     0 1   my $class = shift;
23 0           my %what = @_;
24 0           return bless \%what, $class;
25             }
26              
27              
28             =head2 views
29              
30             Returns a has with key-value pairs representing the shortname and
31             equivalent class for all views installed.
32              
33             =cut
34              
35             sub views {
36 0     0 1   my $self = shift;
37 0 0         my $class = (ref $self)? ref($self) : $self;
38              
39 0           my %views;
40 0           foreach my $view ($self->_views) {
41 0           my $name = $view;
42 0           $name =~ s!^${class}::!!;
43 0           $views{lc($name)} = $view;
44             }
45 0           return %views;
46             }
47              
48             =head2 load_view
49              
50             Returns an object representing a view of the same name of a type,
51             defined in the config and found in C. Authomatically passes in
52             the correct config.
53              
54             =cut
55              
56             sub load_view {
57 0     0 1   my $self = shift;
58 0           my $name = shift;
59 0           my %opts = @_;
60 0           my %views = $self->views;
61 0   0       my $class = $views{lc($name)} || die "Couldn't get a class for view of type $name\n";
62 0 0         $class->require || die "Couldn't require class $class: $@\n";
63 0           return $class->new(%opts);
64             }
65              
66             1;
67