File Coverage

blib/lib/OurCal/Provider.pm
Criterion Covered Total %
statement 9 61 14.7
branch 0 8 0.0
condition 0 6 0.0
subroutine 3 15 20.0
pod 11 11 100.0
total 23 101 22.7


line stmt bran cond sub pod time code
1             package OurCal::Provider;
2              
3 1     1   5 use strict;
  1         2  
  1         35  
4 1     1   945 use UNIVERSAL::require;
  1         1963  
  1         10  
5 1         9 use Module::Pluggable sub_name => '_providers',
6 1     1   1088 search_path => 'OurCal::Provider';
  1         17780  
7              
8             =head1 NAME
9              
10             OurCal::Provider - class for getting events and TODOs from the system
11              
12             =head1 CONFIGURATION
13              
14             Teh default provider is a Multi provider named C. This means
15             that you can do
16              
17             [providers]
18             providers=default birthday
19              
20             [default]
21             dsn=dbi:SQLite:ourcal
22             type=dbi
23              
24             [birthday]
25             file=birthday.ics
26             type=icalendar
27              
28             Alternatively you can specify another default provider using the
29             provider config option
30              
31             provider=cache_everything
32              
33             [cache_everything]
34             child=providers
35             type=cache
36              
37             [providers]
38             providers=default birthday
39             type=multi
40              
41             [default]
42             dsn=dbi:SQLite:ourcal
43             type=dbi
44              
45             [birthday]
46             file=birthday.ics
47             type=icalendar
48              
49              
50             Read individual providers for config options.
51              
52             =head1 METHODS
53              
54             =cut
55              
56             =head2 new
57              
58             Requires an C object as config param.
59              
60             Authomatically instantiates the default provider.
61              
62             =cut
63              
64              
65             # TODO if the child of a cache is an icalendar provider
66             # and the icalendar provider has the same cache a sa cache then there'll be
67             # a deep recursion. We should fix this somehow.
68             sub new {
69 0     0 1   my $class = shift;
70 0           my %what = @_;
71              
72             # first work out what provider we're using
73 0           my $conf = $what{config};
74 0           my @args ;
75 0           my $name = $conf->{_}->{provider};
76 0 0         if (defined $name) {
77 0           push @args, $name;
78             } else {
79 0           push @args, ("providers", $conf, type => "multi");
80             }
81             # then load it
82 0           $what{_provider} = $class->load_provider(@args);
83 0           return bless \%what, $class;
84             }
85              
86             =head2 providers
87              
88             Returns a hash of all providers installed on the system as key-value
89             pairs of the name of the provider and class it represents.
90              
91             =cut
92              
93             sub providers {
94 0     0 1   my $self = shift;
95 0 0         my $class = (ref $self)? ref($self) : $self;
96              
97 0           my %providers;
98 0           foreach my $provider ($self->_providers) {
99 0           my $name = $provider;
100 0           $name =~ s!^${class}::!!;
101 0           $providers{lc($name)} = $provider;
102             }
103 0           return %providers;
104             }
105              
106             =head2 load_provider
107              
108             Load a provider with a given name as defined in the config and returns
109             it as an object.
110              
111             =cut
112              
113             sub load_provider {
114 0     0 1   my $self = shift;
115 0           my $name = shift;
116 0           my $conf = shift;
117 0           my %opts = @_;
118 0   0       my $pconf = $conf->config($name) || die "Don't know about provider $name\n";
119 0   0       my $type = $pconf->{type} || $opts{type} || die "Couldn't work out type for provider $name - you must provide a 'type' config\n";
120 0           my %provs = $self->providers;
121 0   0       my $class = $provs{lc($type)} || die "Couldn't get a class for provider $name of type $type\n";
122 0 0         $class->require || die "Couldn't require class $class: $@\n";
123 0           return $class->new(config => $conf, name => $name);
124             }
125              
126              
127             =head2 todos
128              
129             Returns all the todos on the system.
130              
131             =cut
132              
133             sub todos {
134 0     0 1   my $self = shift;
135 0           return $self->_do_default('todos', @_);
136             }
137              
138             =head2 has_events
139              
140             Returns whether there are events given the params.
141              
142             =cut
143              
144             sub has_events {
145 0     0 1   my $self = shift;
146 0           return $self->_do_default('has_events', @_);
147             }
148              
149             =head2 events
150              
151             Returns all the events for the given params.
152              
153             =cut
154              
155             sub events {
156 0     0 1   my $self = shift;
157 0           my %opts = @_;
158 0           my @events = sort { $b->date cmp $a->date } $self->_do_default('events', %opts);
  0            
159 0 0         @events = splice @events, 0, $opts{limit} if defined $opts{limit};
160 0           return @events;
161             }
162              
163             =head2 users
164              
165             Returns the name of all the users on the system.
166              
167             =cut
168              
169             sub users {
170 0     0 1   my $self = shift;
171 0           return $self->_do_default('users', @_);
172             }
173              
174             =head2 save_todo
175              
176             Save a todo.
177              
178             =cut
179              
180             sub save_todo {
181 0     0 1   my $self = shift;
182 0           return $self->_do_default('save_todo', @_);
183             }
184              
185             =head2 del_todo
186              
187             Delete a todo.
188              
189             =cut
190              
191             sub del_todo {
192 0     0 1   my $self = shift;
193 0           return $self->_do_default('del_todo', @_);
194             }
195              
196             =head2 save_event
197              
198             Save an event.
199              
200             =cut
201              
202             sub save_event {
203 0     0 1   my $self = shift;
204 0           return $self->_do_default('save_event', @_);
205             }
206              
207             =head2 del_event
208              
209             Delete an event..
210              
211             =cut
212              
213             sub del_event {
214 0     0 1   my $self = shift;
215 0           return $self->_do_default('del_event', @_);
216             }
217              
218             sub _do_default {
219 0     0     my $self = shift;
220 0           my $sub = shift;
221 0           my $thing = shift;
222 0           $self->{_provider}->$sub($thing, @_);
223             }
224             1;
225