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.

SYNOPSIS

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

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

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

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 other settings where you might want to make method calls without knowing whether the object had been stored in the database or not.

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 Class::DBI::Factory object (or an object of a subclass thereof, such as Delivery) to provide the necessary information about classes and columns.

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.

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 will be deflated in the usual way.

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

is_ghost()

Returns true, naturally. This isn't of much use unless you put a corresponding is_ghost method in your Class::DBI base class and have it return false.

type()

This is a key value that determines the class a particular object is ghosting, and therefore the columns and relationships it should enter into. It must be set at construction time, so this method just returns the value stored then.

factory()

As usual, calls CDF->instance to get the locally active factory object, whatever locally means in this case.

factory_class()

Override this method in subclass to use a factory class other than CDF (a subclass of it, presumably). Should return a fully qualified Module::Name.

AUTOLOAD()

Very simple: nothing clever here at all. This provides as a get-and-set method for each of the columns defined by the class that this object is ghosting (ie it uses the type parameter to check method names). Nothing else.

find_column()

Exactly as with a normal Class::DBI class, except that it's a remote enquiry mediated by the factory.

just_data()

Returns only that part of the underlying hashref which is needed to create the real version of this object, ie having removed type, id and any extraneous values that have been set but are not columns of the eventual object.

make()

Attempts to produce a real object of the class specified by the type parameter supplied during construction, using the column values of the ghost object.

The created object is returned, but the ghost object remains the same, so it is possible to create several new cdbi objects from one ghost.

for(@addresses) {
  $ghost->address($_);
  $ghost->make;
}

But $ghost will no longer be a ghost, even so. make returns nothing if the creation fails, and the $ghost object remains as it was.

find_or_make()

Behaves exactly as make, except that it calls find_or_create instead of create: if an object of the relevant class exists containing exactly the values currently stored in this object, that object will be returned instead and no new object created.

REQUIRES

Class::DBI::Factory

SEE ALSO

Class::DBI Class::DBI::Factory

AUTHOR

William Ross, wross@cpan.org

COPYRIGHT

Copyright 2001-4 William Ross, spanner ltd.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.