Yasushi Nakajima > Pogo-0.10 > PogoLink

Download:
Pogo-0.10.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  2
Open  0
View/Report Bugs
Source  

NAME ^

PogoLink - Bidirectional relationship class for objects in a Pogo database

SYNOPSIS ^

  use PogoLink;
  # Define relationships
  package Person;
  sub new {
      my($class, $name) = @_;
      my $self = new_tie Pogo::Hash 8, undef, $class;
      %$self = (
          NAME     => $name,
          FATHER   => new PogoLink::Scalar($self, 'Man',    'CHILDREN'),
          MOTHER   => new PogoLink::Scalar($self, 'Woman',  'CHILDREN'),
          FRIENDS  => new PogoLink::Btree ($self, 'Person', 'FRIENDS', 'NAME'),
      );
      $self;
  }
  package Man;
  @ISA = qw(Person);
  sub new {
      my($class, $name) = @_;
      my $self = $class->SUPER::new($name);
      $self->{CHILDREN} = new PogoLink::Array ($self, 'Person', 'FATHER');
      $self->{WIFE}     = new PogoLink::Scalar($self, 'Woman',  'HUS');
      $self;
  }
  package Woman;
  @ISA = qw(Person);
  sub new {
      my($class, $name) = @_;
      my $self = $class->SUPER::new($name);
      $self->{CHILDREN} = new PogoLink::Array ($self, 'Person', 'MOTHER');
      $self->{HUS}      = new PogoLink::Scalar($self, 'Man',    'WIFE');
      $self;
  }

  # Use relationships
  $Dad = new Man   'Dad';
  $Mom = new Woman 'Mom';
  $Jr  = new Man   'Jr';
  $Gal = new Woman 'Gal';
  # Marriage 
  $Dad->{WIFE}->add($Mom);     # $Mom->{HUS} links to $Dad automatically
  # Birth
  $Dad->{CHILDREN}->add($Jr);  # $Jr->{FATHER} links to $Dad automatically
  $Mom->{CHILDREN}->add($Jr);  # $Jr->{MOTHER} links to $Mom automatically
  # Jr gets friend
  $Jr->{FRIENDS}->add($Gal);   # $Gal->{FRIENDS} links to $Jr automatically
  # Oops! Gal gets Dad
  $Gal->{HUS}->add($Dad);      # $Dad->{WIFE} links to $Gal automatically
                               # $Mom->{HUS} unlinks to $Dad automatically

DESCRIPTION ^

PogoLink makes single-single or single-multi or multi-multi bidirectional relationships between objects in a Pogo database. The relationships are automatically maintained to link each other correctly. You can choose one of Pogo::Array, Pogo::Hash, Pogo::Htree, Pogo::Btree and Pogo::Ntree to make a multi end of link.

Classes

PogoLink::Scalar

This class makes a single end of link.

PogoLink::Array

This class makes a multi end of link as an array. It uses Pogo::Array to have links.

PogoLink::Hash, PogoLink::Htree, PogoLink::Btree, PogoLink::Ntree

These classes make a multi end of link as a hash. Each uses corresponding Pogo::* to have links.

Methods

new PogoLink::* $selfobject, $linkclass, $invfield, $keyfield, $size

Constructor. Class method. $selfobject is a object in the database which possesses this link. It must be a object as a hash reference. $linkclass is a class name of linked object. If $linkclass defaults, any class object is allowed. $invfield is a field (i.e. hash key) name of the linked object which links inversely. $keyfield is only necessary for PogoLink::Hash, PogoLink::Htree, PogoLink::Btree, PogoLink::Ntree. It specifies a field name of the linked object thats value is used as the key of this link hash. $size may be specified for PogoLink::Array, PogoLink::Hash or PogoLink::Htree. $size will be used when internal linking Pogo::Array, Pogo::Hash or Pogo::Htree object will be constructed.

NOTE: You cannot use PogoLink::* constructors as follows in a class constructor.

  sub new {
      my($class) = @_;
      my $self = {};
      bless $self, $class;
      $self->{FOO} = new PogoLink::Scalar $self, 'Foo', 'BAR';
      $self;
  }

Because the self-object which is passed to PogoLink::* constructors must be tied to a Pogo::* object. In the above sample, $self is a Perl object on the memory yet. The right way is as follows.

  sub new {
      my($class) = @_;
      my $self = new_tie Pogo::Hash 8, undef, $class;
      $self->{FOO} = new PogoLink::Scalar $self, 'Foo', 'BAR';
      $self;
  }

You can make a blessed reference which is tied to specified Pogo::* object by using new_tie which takes a class name as arguments.

get $idx_or_key

Get the linked object. For PogoLink::Scalar, $idx_or_key is not necessary. For PogoLink::Array, $idx_or_key is an array index number. For other, $idx_or_key is a hash key string.

getlist

Get the linked object list.

getkeylist

Get the hash key list of linked objects. Only available for PogoLink::Hash, PogoLink::Htree, PogoLink::Btree, PogoLink::Ntree.

find $object

Test the link if it links to $object.

clear

Unlink to all objects in the link.

del $object

Unlink to $object.

add $object

Link to $object. The inverse field (it's name was specified as $invfield by new()) of $object must be a PogoLink::* object. If the inverse field is not defined yet and $object has INIT_fieldname method (e.g. the field name is 'FIELD', the method name is 'INIT_FIELD'), this method calls $object->INIT_fieldname() to initialize the inverse field before linking.

AUTHOR ^

Sey Nakajima <nakajima@netstock.co.jp>

SEE ALSO ^

Pogo(3). sample/person.pl.

syntax highlighting: