Class::AutoAccess::Deep - automatically creates the accessors reach deep inside the field


Class-AutoAccess-Deep documentation Contained in the Class-AutoAccess-Deep distribution.

Index


Code Index:

NAME

Top

Class::AutoAccess::Deep - automatically creates the accessors reach deep inside the field

SYNOPSIS

Top

  package MyClass;

  # inherit Class::AutoAccess::Deep and that's all
  use base qw(Class::AutoAccess::Deep);

  sub to_check {
      # write your own processing code...
  }

  package main;

  $data = {
      foo      => undef,
      bar      => {
          baz  => undef,
      },
      to_check => undef,
  };

  my $obj = MyClass->new($data);

  # now, you can access the fields as described below
  $obj->foo('new value');        # set "new value"
  my $foo = $obj->foo;           # get "new value"

  # you can access the field chain deeply
  # by joining field name with '->'
  $obj->bar->baz('other value'); # set "other value"
  my $bar_baz = $obj->bar->baz;  # get "other value"

  # your own method called correctly
  my $value = $obj->to_check;

DESCRIPTION

Top

Class::AutoAccess::Deep is the base class for automated accessors implementation. You can access deep inside the object fields to call the method named by joining the object field name with '->' operator.

METHOD

Top

new ( \%fields )

  my $obj = MyClass->new(\%fields);

Creates and returns new object. It takes a hashref which is used to initialize the object. If you don't like it, override it.

EXCEPTION

Top

When you attempt to access the undefined field, Class::AutoAccess::Deep object throws exception using Carp::croak.

TODO

Top

* Store the accessors which is called once into somewhere for performance reason

ACKNOWLEDGEMENTS

Top

* Makamaka, http://www.donzoko.net/
* Naoya Ito, http://d.hatena.ne.jp/naoya/
* Yappo, http://blog.yappo.jp/
* YAMASHINA Hio, http://fleur.hio.jp/
* Topia, http://www.clovery.jp/

AUTHOR

Top

Kentaro Kuribayashi, <kentaro@cpan.org>

COPYRIGHT AND LICENSE

Top


Class-AutoAccess-Deep documentation Contained in the Class-AutoAccess-Deep distribution.

#$Id: Deep.pm 25 2005-09-11 09:48:14Z kentaro $

package Class::AutoAccess::Deep;

use strict;

use Carp ();

our $AUTOLOAD;
our $VERSION = '0.02';

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

    _croak('argument must be passed in as hashref')
        unless defined $fields || ref $fields eq 'HASH';

    return bless $fields, $class;
}

sub AUTOLOAD {
    my $self = shift;
    (my $field = $AUTOLOAD) =~ s/.*:://;

    return if $field eq 'DESTROY';

    # XXX
    # croaks when you attempt to access an undefined field
    # do you need this feature?
    _croak("Field $field does not exists")
        unless exists $self->{$field};

    if (@_) {
        $self->{$field} = _get_linkedobj($_[0]);
    }
    else {
        $self->{$field} = _get_linkedobj($self->{$field});
    }

    return $self->{$field};
}

sub _get_linkedobj {
    my $value= shift;

    if (ref($value) eq 'HASH'){
        return __PACKAGE__->new($value);
    }
    else {
        return $value;
    }
}

sub _croak {
    my $msg = shift;
    Carp::croak($msg);

    return;
}

1;

__END__