Sam Vilain > Class-Tangram-1.57 > Class::Tangram::Generator

Download:
Class-Tangram-1.57.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.02   Source  

NAME ^

Class::Tangram::Generator - Generate Class::Tangram-based objects at runtime.

SYNOPSIS ^

  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

DESCRIPTION ^

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.

METHODS ^

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.

DISCUSSION ^

Tangram Schema Extensions

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.

Interoperation with existing package files

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).

EXPORT ^

Class::Tangram::Generator does not have any methods to export.

HISTORY ^

0.01

Initial release

AUTHOR ^

Aaron J Mackey <amackey@virginia.edu>

SEE ALSO ^

Class::Tangram, Tangram, Class::Object, perl.

syntax highlighting: