File Coverage

blib/lib/App/Addex.pm
Criterion Covered Total %
statement 44 53 83.0
branch 9 14 64.2
condition 2 2 100.0
subroutine 10 12 83.3
pod 5 7 71.4
total 70 88 79.5


line stmt bran cond sub pod time code
1 5     5   76219 use strict;
  5         12  
  5         156  
2 5     5   25 use warnings;
  5         11  
  5         137  
3              
4             # because tests use open to open a string-ref, and I am not interested in ever
5             # supporting this module on ancient perls -- rjbs, 2007-12-17
6 5     5   102 use 5.008;
  5         16  
7              
8             package App::Addex 0.027;
9             # ABSTRACT: generate mail tool configuration from an address book
10              
11 5     5   26 use Carp ();
  5         9  
  5         3346  
12              
13             #pod =head1 DESCRIPTION
14             #pod
15             #pod B<Achtung!> The API to this code may very well change. It is almost certain
16             #pod to be broken into smaller pieces, to support alternate sources of entries, and
17             #pod it might just get plugins.
18             #pod
19             #pod This module iterates through all the entries in an address book and produces
20             #pod configuration file based on the entries in the address book, using configured
21             #pod output plugins.
22             #pod
23             #pod It is meant to be run with the F<addex> command, which is bundled as part of
24             #pod this software distribution.
25             #pod
26             #pod =method new
27             #pod
28             #pod my $addex = App::Addex->new(\%arg);
29             #pod
30             #pod This method returns a new Addex.
31             #pod
32             #pod Valid parameters are:
33             #pod
34             #pod classes - a hashref of plugin/class pairs, described below
35             #pod
36             #pod Valid keys for the F<classes> parameter are:
37             #pod
38             #pod addressbook - the App::Addex::AddressBook subclass to use (required)
39             #pod output - an array of output producers (required)
40             #pod
41             #pod For each class given, an entry in C<%arg> may be given, which will be used to
42             #pod initialize the plugin before use.
43             #pod
44             #pod =cut
45              
46             # sub starting_section_name { 'classes' }
47 0     0 0 0 sub mvp_multivalue_args { qw(output plugin) }
48              
49             sub new {
50 7     7 1 4112 my ($class, $arg) = @_;
51              
52 7         17 my $self = bless {} => $class;
53              
54             # XXX: keep track of seen/unseen classes; carp if some go unused?
55             # -- rjbs, 2007-04-06
56              
57 7         16 for my $core (qw(addressbook)) {
58 7 100       229 my $class = $arg->{classes}{$core}
59             or Carp::confess "no $core class provided";
60              
61 6         24 $self->{$core} = $self->_initialize_plugin($class, $arg->{$class});
62             }
63              
64 6 100       16 my @output_classes = @{ $arg->{classes}{output} || [] }
  6 100       167  
65             or Carp::confess "no output classes provided";
66              
67 5         9 my @output_plugins;
68 5         14 for my $class (@output_classes) {
69 5         22 push @output_plugins, $self->_initialize_plugin($class, $arg->{$class});
70             }
71 4         28 $self->{output} = \@output_plugins;
72              
73 4 50       10 my @plugin_classes = @{ $arg->{classes}{plugin} || [] };
  4         40  
74 4         12 for my $class (@plugin_classes) {
75 0 0       0 eval "require $class" or die;
76 0 0       0 $class->import(%{ $arg->{$class} || {} });
  0         0  
77             }
78              
79 4         20 return $self;
80             }
81              
82             sub from_sequence {
83 0     0 0 0 my ($class, $seq) = @_;
84              
85 0         0 my %arg;
86 0         0 for my $section ($seq->sections) {
87 0         0 $arg{ $section->name } = $section->payload;
88             }
89              
90 0         0 $class->new(\%arg);
91             }
92              
93             sub _initialize_plugin {
94 11     11   36 my ($self, $class, $arg) = @_;
95 11   100     55 $arg ||= {};
96 11         20 $arg->{addex} = $self;
97              
98             # in most cases, this won't be needed, since the App::Addex::Config will have
99             # loaded plugins as a side effect, but let's be cautious -- rjbs, 2007-05-10
100 11 100       657 eval "require $class" or die;
101              
102 10         1455 return $class->new($arg);
103             }
104              
105             #pod =method addressbook
106             #pod
107             #pod my $abook = $addex->addressbook;
108             #pod
109             #pod This method returns the App::Addex::AddressBook object.
110             #pod
111             #pod =cut
112              
113 4     4 1 22 sub addressbook { $_[0]->{addressbook} }
114              
115             #pod =method output_plugins
116             #pod
117             #pod This method returns all of the output plugin objects.
118             #pod
119             #pod =cut
120              
121             sub output_plugins {
122 28     28 1 48 my ($self) = @_;
123 28         40 return @{ $self->{output} };
  28         53  
124             }
125              
126             #pod =method entries
127             #pod
128             #pod This method returns all the entries to be processed. By default it is
129             #pod delegated to the address book object. This method may change a good bit in the
130             #pod future, as we really want an iterator, not just a list.
131             #pod
132             #pod =cut
133              
134             sub entries {
135 4     4 1 10 my ($self) = @_;
136 4         16 return sort { $a->name cmp $b->name } $self->addressbook->entries;
  40         101  
137             }
138              
139             #pod =method run
140             #pod
141             #pod App::Addex->new({ ... })->run;
142             #pod
143             #pod This method performs all the work expected of an Addex: it iterates through the
144             #pod entries, invoking the output plugins for each one.
145             #pod
146             #pod =cut
147              
148             sub run {
149 4     4 1 2311 my ($self) = @_;
150              
151 4         18 for my $entry ($self->entries) {
152 24         180 for my $plugin ($self->output_plugins) {
153 24         57 $plugin->process_entry($self, $entry);
154             }
155             }
156              
157 4         79 for my $plugin ($self->output_plugins) {
158 4         23 $plugin->finalize;
159             }
160             }
161              
162             1;
163              
164             __END__
165              
166             =pod
167              
168             =encoding UTF-8
169              
170             =head1 NAME
171              
172             App::Addex - generate mail tool configuration from an address book
173              
174             =head1 VERSION
175              
176             version 0.027
177              
178             =head1 DESCRIPTION
179              
180             B<Achtung!> The API to this code may very well change. It is almost certain
181             to be broken into smaller pieces, to support alternate sources of entries, and
182             it might just get plugins.
183              
184             This module iterates through all the entries in an address book and produces
185             configuration file based on the entries in the address book, using configured
186             output plugins.
187              
188             It is meant to be run with the F<addex> command, which is bundled as part of
189             this software distribution.
190              
191             =head1 PERL VERSION SUPPORT
192              
193             This module has the same support period as perl itself: it supports the two
194             most recent versions of perl. (That is, if the most recently released version
195             is v5.40, then this module should work on both v5.40 and v5.38.)
196              
197             Although it may work on older versions of perl, no guarantee is made that the
198             minimum required version will not be increased. The version may be increased
199             for any reason, and there is no promise that patches will be accepted to lower
200             the minimum required perl.
201              
202             =head1 METHODS
203              
204             =head2 new
205              
206             my $addex = App::Addex->new(\%arg);
207              
208             This method returns a new Addex.
209              
210             Valid parameters are:
211              
212             classes - a hashref of plugin/class pairs, described below
213              
214             Valid keys for the F<classes> parameter are:
215              
216             addressbook - the App::Addex::AddressBook subclass to use (required)
217             output - an array of output producers (required)
218              
219             For each class given, an entry in C<%arg> may be given, which will be used to
220             initialize the plugin before use.
221              
222             =head2 addressbook
223              
224             my $abook = $addex->addressbook;
225              
226             This method returns the App::Addex::AddressBook object.
227              
228             =head2 output_plugins
229              
230             This method returns all of the output plugin objects.
231              
232             =head2 entries
233              
234             This method returns all the entries to be processed. By default it is
235             delegated to the address book object. This method may change a good bit in the
236             future, as we really want an iterator, not just a list.
237              
238             =head2 run
239              
240             App::Addex->new({ ... })->run;
241              
242             This method performs all the work expected of an Addex: it iterates through the
243             entries, invoking the output plugins for each one.
244              
245             =head1 AUTHOR
246              
247             Ricardo SIGNES <rjbs@semiotic.systems>
248              
249             =head1 CONTRIBUTOR
250              
251             =for stopwords Reini Urban
252              
253             Reini Urban <rurban@x-ray.at>
254              
255             =head1 COPYRIGHT AND LICENSE
256              
257             This software is copyright (c) 2006 by Ricardo SIGNES.
258              
259             This is free software; you can redistribute it and/or modify it under
260             the same terms as the Perl 5 programming language system itself.
261              
262             =cut