Class-DBI-Factory

 view release on metacpan or  search on metacpan

lib/Class/DBI/Factory/Ghost.pm  view on Meta::CPAN

package Class::DBI::Factory::Ghost;
use strict;
use vars qw( $VERSION $AUTOLOAD );
$VERSION = '0.04';

=head1 NAME

Class::DBI::Factory::Ghost - a minimal data-container used as a precursor for Class::DBI objects when populating forms or otherwise preparing to create a new object from existing data.

=head1 SYNOPSIS

my $thing = Class::DBI::Factory::Ghost->new({
    id => 'new',
    moniker => $moniker,
    person => $self->session->person,
    parent => $self->param('parent'),
});

# or

my $thing = Class::DBI::Factory::Ghost->from( $other_thing );

$thing->title($input->param('title'));

$thing->solidify if (...);

=head1 INTRODUCTION

The ghost is a loose data-container that can be passed to templates or other processes in place of a full Class::DBI object. Its main purpose is to allow the same forms to be used for both creation and editing of objects, but it can be useful in othe...

It is constructed and queried in largely the same way as a Class::DBI object, except that only the most basic parts of the interface are supported, and it depends on the availability of a L<Class::DBI::Factory> object (or an object of a subclass ther...

More elaborate Class::DBI constructions, such as set_sql prototypes and has_* methods will not work: only the simple get-and-set functionality is duplicated here, and obviously anything which relies on cdbi's internal variables will not work.

=head2 new()

Constructs and returns a ghost object. Accepts a hashref of column => value pairs which must include a 'type' or 'moniker' value that corresponds to one of your data classes. Supplied values for other columns can be but don't have to be objects: they...

  my $temp = Class::DBI::Factory::Ghost->new({
      moniker => 'cd',
      person => $session->person,
  });  

=cut

sub new {
    my ($class, $data) = @_;
    $data->{id} ||= 'new';
    $data->{_moniker} = delete $data->{moniker} || delete $data->{type};
    return unless $data->{_moniker} && $class->factory->has_class($data->{_moniker});
    return bless $data, $class;
}

=head2 from( $object )

Constructs and returns a ghost copy of a real cdbi object. Useful if the object is about to be deleted or otherwise interfered with.

  my $remnant = Class::DBI::Factory::Ghost->from( $foo );  
  ...
  my $bar = $remnant->make;
  
Calling C<make> on the ghost object should give you an object that is not identical to but exactly resembles the original template object. 

But note that any cascading deletes or triggers associated with deletion will have been, er, triggered. 
  
=cut

sub from {



( run in 2.539 seconds using v1.01-cache-2.11-cpan-cdf2f3d4e48 )