App-Addex

 view release on metacpan or  search on metacpan

lib/App/Addex.pm  view on Meta::CPAN

use strict;
use warnings;

# because tests use open to open a string-ref, and I am not interested in ever
# supporting this module on ancient perls -- rjbs, 2007-12-17
use 5.008;

package App::Addex;
# ABSTRACT: generate mail tool configuration from an address book
$App::Addex::VERSION = '0.026';
use Carp ();

#pod =head1 DESCRIPTION
#pod
#pod B<Achtung!>  The API to this code may very well change.  It is almost certain
#pod to be broken into smaller pieces, to support alternate sources of entries, and
#pod it might just get plugins.
#pod
#pod This module iterates through all the entries in an address book and produces
#pod configuration file based on the entries in the address book, using configured
#pod output plugins.
#pod
#pod It is meant to be run with the F<addex> command, which is bundled as part of
#pod this software distribution.
#pod
#pod =method new
#pod
#pod   my $addex = App::Addex->new(\%arg);
#pod
#pod This method returns a new Addex.
#pod
#pod Valid parameters are:
#pod
#pod   classes    - a hashref of plugin/class pairs, described below
#pod
#pod Valid keys for the F<classes> parameter are:
#pod
#pod   addressbook - the App::Addex::AddressBook subclass to use (required)
#pod   output      - an array of output producers (required)
#pod
#pod For each class given, an entry in C<%arg> may be given, which will be used to
#pod initialize the plugin before use.
#pod
#pod =cut

# sub starting_section_name { 'classes' }
sub mvp_multivalue_args  { qw(output plugin) }

sub new {
  my ($class, $arg) = @_;

  my $self = bless {} => $class;

  # XXX: keep track of seen/unseen classes; carp if some go unused?
  # -- rjbs, 2007-04-06

  for my $core (qw(addressbook)) {
    my $class = $arg->{classes}{$core}
      or Carp::confess "no $core class provided";

    $self->{$core} = $self->_initialize_plugin($class, $arg->{$class});
  }

  my @output_classes = @{ $arg->{classes}{output} || [] }
    or Carp::confess "no output classes provided";

  my @output_plugins;
  for my $class (@output_classes) {
    push @output_plugins, $self->_initialize_plugin($class, $arg->{$class});
  }
  $self->{output} = \@output_plugins;

  my @plugin_classes = @{ $arg->{classes}{plugin} || [] };
  for my $class (@plugin_classes) {
    eval "require $class" or die;
    $class->import(%{ $arg->{$class} || {} });
  }

  return $self;
}

sub from_sequence {
  my ($class, $seq) = @_;

  my %arg;
  for my $section ($seq->sections) {
    $arg{ $section->name } = $section->payload;
  }

  $class->new(\%arg);
}

sub _initialize_plugin {
  my ($self, $class, $arg) = @_;
  $arg ||= {};
  $arg->{addex} = $self;

  # in most cases, this won't be needed, since the App::Addex::Config will have
  # loaded plugins as a side effect, but let's be cautious -- rjbs, 2007-05-10
  eval "require $class" or die;

  return $class->new($arg);
}

#pod =method addressbook
#pod
#pod   my $abook = $addex->addressbook;
#pod
#pod This method returns the App::Addex::AddressBook object.
#pod
#pod =cut

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.950 second using v1.00-cache-1.14-grep-28634ff-cpan-4d46879620f )