AddressBook

 view release on metacpan or  search on metacpan

lib/AddressBook/Entry.pm  view on Meta::CPAN

Generating values in calculated fields:
  
  $entry->calculate;

Comparing entries:

  AddressBook::Entry::Compare($entry1,$entry2);

Dumping an entry:

  $entry->dump;

Note: Each attribute contains a reference to an array of values.

=cut

use strict;
use Carp;

use vars qw($VERSION);

$VERSION = '0.12';

=head2 new

    $entry=AddressBook->new();
    $entry=AddressBook::Entry->new(attr=>\%attr);
    $entry=AddressBook::Entry->new(attr=>\%attr,db=>$db)

Create and optionally load an entry object.
  
All of the following parameters are optional:

=over 4

=item %attr

An optional hash containing the attributes to add to the entry.  The attribute 
values may be scalars or array references.

=item $config

An AddressBook::Config reference.  If supplied, this configuration will be used
and any  $config_file paramater is ignored.

=item $config_file

The configuration file to use instead of the default (/etc/AddressBook.conf).

=item $db

Can be used to specify that the keys of %attr are those for a specific backend.

=back

=cut

sub new {
  my $class = shift;
  my $self = {};
  bless ($self,$class);
  my %args = @_;
  if ($args{config}) {
    $self->{config} = $args{config};
  } else {
    $self->{config} = AddressBook::Config->new(config_file=>$args{config_file});
  }
  if ($args{attr}) {
    $self->add(attr=>$args{attr},db=>$args{db});
  }
  return $self;
}

=head2 add

    $entry->add(attr=>\%attr);
    $entry->add(attr=>\%attr,db=>$db);

Adds attributes to the entry object.  New data is added to attributes which already 
exist

=over 4

=item %attr

Required hash containing the attributes to add to the entry.  The attribute values
may be specified as scalars or array references.

=item $db

Can be used to specify that the keys of %attr are those for a specific backend.

=back

=cut

sub add {
  my $self = shift;
  my $class = ref $self || croak "Not a method call";
  my (%args) = @_;
  my $attr=$args{attr};
  foreach (keys %{$attr}) {
    if (ref $attr->{$_} ne "ARRAY") {
      $attr->{$_} = [$attr->{$_}];
    }
    next unless (@{$attr->{$_}});
    if (defined $args{db}) {
      if (defined $self->{config}->{db2generic}->{$args{db}}->{$_}) {
	push @{$self->{attr}->{$self->{config}->{db2generic}->{$args{db}}->{$_}}}, @{$attr->{$_}};
      } else {
	croak "Error:  \"$_\" is not a defined attribute for $args{db}";
      }
    } else {
      if (defined $self->{config}->{meta}->{$_}) {
	push @{$self->{attr}->{$_}}, @{$attr->{$_}}
      } else {
	croak "Error: \"$_\" is not a defined attribute";
      }
    }
  }
}



( run in 3.969 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )