| 1 | package OurCal::Provider; | ||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 4 | 2 | ▶ | use UNIVERSAL::require; | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 5 | 2 | ▶ | use Module::Pluggable sub_name => '_providers', | ||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 6 | 2 | search_path => 'OurCal::Provider'; | |||||||||||||||||||||||
| 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<providers>. 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 <param[s]> | ||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||
| 58 | Requires an C<OurCal::Config> 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 | 0 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 69 | 0 | 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 | ▶ | if (defined $name) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 | 0 | ▶ | sub providers { | ||||||||||||||||||||||
|
Subroutine
providers: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||||||||||||||
| 95 | 0 | ▶ | my $class = (ref $self)? ref($self) : $self; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 <name> | ||||||||||||||||||||||||
| 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 | 0 | ▶ | sub load_provider { | ||||||||||||||||||||||
|
Subroutine
load_provider: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||
| 114 | 0 | my $self = shift; | |||||||||||||||||||||||
| 115 | 0 | my $name = shift; | |||||||||||||||||||||||
| 116 | 0 | my $conf = shift; | |||||||||||||||||||||||
| 117 | 0 | my %opts = @_; | |||||||||||||||||||||||
| 118 | 0 | ▶ | my $pconf = $conf->config($name) || die "Don't know about provider $name\n"; | ||||||||||||||||||||||
|
Condition0%0/2
$conf->config($name) || die("Don't know about provider $name\n")
MC/DC0%0/1
$conf->config($name) || die("Don't know about provider $name\n")
$conf->config($name)
Truth table0%0/2
A
A= $conf->config($name)
| |||||||||||||||||||||||||
| 119 | 0 | ▶ | my $type = $pconf->{type} || $opts{type} || die "Couldn't work out type for provider $name - you must provide a 'type' config\n"; | ||||||||||||||||||||||
|
Condition0%0/5
$pconf->{'type'} || $opts{'type'} || die("Couldn't work out type for provider $name - you must provide a 'type' config\n")
$pconf->{"type"} or $opts{"type"}
MC/DC0%0/2
$pconf->{'type'} || $opts{'type'} || die("Couldn't work out type for provider $name - you must provide a 'type' config\n")
$pconf->{"type"}
$opts{"type"}
Truth table0%0/3
A or B
A= $pconf->{"type"}B= $opts{"type"}
| |||||||||||||||||||||||||
| 120 | 0 | my %provs = $self->providers; | |||||||||||||||||||||||
| 121 | 0 | ▶ | my $class = $provs{lc($type)} || die "Couldn't get a class for provider $name of type $type\n"; | ||||||||||||||||||||||
|
Condition0%0/2
$provs{lc $type} || die("Couldn't get a class for provider $name of type $type\n")
MC/DC0%0/1
$provs{lc $type} || die("Couldn't get a class for provider $name of type $type\n")
$provs{lc $type}
Truth table0%0/2
A
A= $provs{lc $type}
| |||||||||||||||||||||||||
| 122 | 0 | ▶ | $class->require || die "Couldn't require class $class: $@\n"; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 | 0 | ▶ | sub todos { | ||||||||||||||||||||||
|
Subroutine
todos: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||
| 135 | 0 | return $self->_do_default('todos', @_); | |||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||
| 138 | =head2 has_events <param[s]> | ||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||
| 140 | Returns whether there are events given the params. | ||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||
| 144 | 0 | ▶ | sub has_events { | ||||||||||||||||||||||
|
Subroutine
has_events: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 145 | 0 | my $self = shift; | |||||||||||||||||||||||
| 146 | 0 | return $self->_do_default('has_events', @_); | |||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||
| 149 | =head2 events <param[s]> | ||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||
| 151 | Returns all the events for the given params. | ||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||
| 155 | 0 | ▶ | sub events { | ||||||||||||||||||||||
|
Subroutine
events: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||
| 156 | 0 | my $self = shift; | |||||||||||||||||||||||
| 157 | 0 | my %opts = @_; | |||||||||||||||||||||||
| 158 | 0 | my @events = sort { $b->date cmp $a->date } $self->_do_default('events', %opts); | |||||||||||||||||||||||
| 159 | 0 | ▶ | @events = splice @events, 0, $opts{limit} if defined $opts{limit}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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 | 0 | ▶ | sub users { | ||||||||||||||||||||||
|
Subroutine
users: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 170 | 0 | my $self = shift; | |||||||||||||||||||||||
| 171 | 0 | return $self->_do_default('users', @_); | |||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||
| 174 | =head2 save_todo <OurCal::Todo> | ||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||
| 176 | Save a todo. | ||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||
| 180 | 0 | ▶ | sub save_todo { | ||||||||||||||||||||||
|
Subroutine
save_todo: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 181 | 0 | my $self = shift; | |||||||||||||||||||||||
| 182 | 0 | return $self->_do_default('save_todo', @_); | |||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||
| 185 | =head2 del_todo <OurCal::Todo> | ||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||
| 187 | Delete a todo. | ||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||
| 191 | 0 | ▶ | sub del_todo { | ||||||||||||||||||||||
|
Subroutine
del_todo: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 192 | 0 | my $self = shift; | |||||||||||||||||||||||
| 193 | 0 | return $self->_do_default('del_todo', @_); | |||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||
| 196 | =head2 save_event <OurCal::Event> | ||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||
| 198 | Save an event. | ||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||
| 202 | 0 | ▶ | sub save_event { | ||||||||||||||||||||||
|
Subroutine
save_event: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 203 | 0 | my $self = shift; | |||||||||||||||||||||||
| 204 | 0 | return $self->_do_default('save_event', @_); | |||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||
| 207 | =head2 del_event <OurCal::Event> | ||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||
| 209 | Delete an event.. | ||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||
| 213 | 0 | ▶ | sub del_event { | ||||||||||||||||||||||
|
Subroutine
del_event: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||
| 214 | 0 | my $self = shift; | |||||||||||||||||||||||
| 215 | 0 | return $self->_do_default('del_event', @_); | |||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||
| 218 | 0 | ▶ | sub _do_default { | ||||||||||||||||||||||
|
Subroutine
_do_default: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 219 | 0 | my $self = shift; | |||||||||||||||||||||||
| 220 | 0 | my $sub = shift; | |||||||||||||||||||||||
| 221 | 0 | my $thing = shift; | |||||||||||||||||||||||
| 222 | 0 | $self->{_provider}->$sub($thing, @_); | |||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||
| 224 | 2 | 1; | |||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||