Class::Tangram::Generator - Generate Class::Tangram-based objects at runtime.
use Class::Tangram::Generator;
my $schema = { ... }; # a Tangram schema definition hashref,
# including all classes
my $gen = new Class::Tangram::Generator $schema;
my $orange = $gen->new('Orange');
$orange->juicyness(10); # $orange is a Class::Tangram-based Orange object
The purpose of Class::Tangram::Generator is to facilitate the rapid development
of Class::Tangram-based objects in the Tangram framework. Instead of having to
write class modules for all your Tangram objects, many of which only inherit
from Class::Tangram for accessor and constraint checking, you use
Class::Tangram::Generator to dynamically instantiate each class as necessary,
at runtime. This also alleviates the long litany of 'use Orange; use Apple;
... ' statements in all of your scripts.
- new($schema, [$base]) [ Class method ]
- new( { Schema => $schema, Base => $base } ) [ Class method ]
- Initialize and return a new Class::Tangram::Generator object, using the
Tangram schema hashref provided. Newly generated objects will have
"Class::Tangram" added to their @ISA
variable, unless an alternative base class is specified in
$base (that way you can subclass Class::Tangram
and still use Class::Tangram::Generator).
- new($classname) [ Object method ]
- Obtain a new object of the provided class. Additional arguments are passed
to Class::Tangram's new function (for attribute manipulation). Any errors
thrown by Class::Tangram will be propagated by
Class::Tangram::Generator.
To provide custom methods for each class, add subroutine references to the
'methods' key in the schema:
Orange => {
fields => { int => [ qw(juicyness ripeness) ] },
methods => {
squeeze => sub {
my $self = shift;
$self->juicyness($self->juicyness() - 1);
},
eviscerate => sub {
my $self = shift;
$self->juicyness(0);
}
}
}
The subroutines will be automatically installed into the class's
namespace.
If a .pm module file corresponding to the requested class can be found by Perl
(looking in the usual places defined by @INC,
PERL5LIB, etc.), it will be loaded before Class::Tangram::Generator has
finished dynamically generating the package. This means that any schema and/or
methods found in the .pm module file will be overriden by those specified in
the schema given to Class::Tangram::Generator. For example, there may be an
Orange.pm module file that looks like:
package Orange;
sub rehydrate { shift->juicyness(10) }
1;
This allows the addition of more lengthy subroutines without
filling up the schema with lots of code. But a "rehydrate" method
specified in the schema would entirely replace this subroutine (and it would
not be available via SUPER).
Class::Tangram::Generator does not have any methods to export.
Aaron J Mackey <amackey@virginia.edu>
Class::Tangram, Tangram, Class::Object, perl.