
EntityModel::Class - define class definition

version 0.011

package Thing;
use EntityModel::Class {
_isa => [ 'ThingBase' ],
name => 'string',
items => { type => 'array', subclass => 'string' }
};
package main;
my $thing = Thing->new();
$thing->name('A thing');
$thing->items->push('an entry');
$thing->items->push('another entry');
print "Have " . $thing->items->count . " items\n";
1;

Applies a class definition to a package. Automatically includes strict, warnings, error handling and other standard features without needing to copy and paste boilerplate code.

NOTE: This is mainly intended for use with EntityModel only, please consider Moose or similar for other projects.
Add EntityModel::Class near the top of the target package:
package Test;
use EntityModel::Class { };
The hashref parameter contains the class definition. Each key is the name of an attribute for the class, with the exception of the following underscore-prefixed keys:
_vcs - version control system information, a plain string containing information about the last changed revision and author for this file.
use EntityModel::Class { _vcs => '$Id$' };
_isa - set up the parents for this class, similar to use parent.
use EntityModel::Class { _isa => 'DateTime' };
An attribute definition will typically create an accessor with the same name, and depending on type may also include some additional helper methods.
Available types include:
string - simple string scalar value.
use EntityModel::Class { name => { type => 'string' } };
array - an array of objects, provide the object type as the subclass parameter
use EntityModel::Class { childNodes => { type => 'array', subclass => 'Node' } };
hash - hash of objects of subclass type
use EntityModel::Class { authorMap => { type => 'hash', subclass => 'Author' } };
If the type (or subclass) contains '::', or starts with a Capitalised letter, then it will be treated as a class. All internal type names are lowercase.
You can also set the scope on a variable, which defines whether it should be include when exporting or importing:
private - private attributes are not exported or visible in attribute lists
use EntityModel::Class { authorMap => { type => 'hash', subclass => 'Author', scope => 'private' } };
public (default) - public attributes are included in export/import, and will be visible when listing attributes for the class
use EntityModel::Class { name => { type => 'string', scope => 'public' } };
You can also specify actions to take when a variable is changed, to support internal attribute observers, by specifying the watch parameter. This takes a hashref with key corresponding to the attribute to watch, and value indicating the method on that object. For example, page = 'path'> would update whenever the path mutator is called on the page attribute. This is intended for use with hash and array containers, rather than classes or simple types.
package Compendium;
use EntityModel::Class {
authors => { type => 'array', subclass => 'Author' },
authorByName => { type => 'hash', subclass => 'Author', scope => 'private', watch => { authors => 'name' } }
};
package main;
my $c = Compendium->new;
$c->authors->push(Author->new("Adams"));
$c->authors->push(Author->new("Brecht"));
print $c->authorByName->{'Adams'}->id;
Apply supplied attributes, and load in the following modules:
Add an entry for this class in the central class info hash.
Set up inheritance as required for this class.
Load all modules required for classes
Record the VCS revision information from _vcs attribute.
apply_attributesadd_methodAdd a version control system tag to the class.
Standard module setup - enable strict and warnings, and disable 'import' fallthrough.
Basic validation function.
Returns attribute information for a given package's attribute.
Add watchers as required for all package definitions.
Call this after all the class definitions have been loaded.

The following functions will be added to the namespace of the importing package.
Helper function to trim all leading and trailing whitespace from the given string.
Get DateTime value for current time
Helper method for expanding a string

Or rather, "please use instead of this module":

Tom Molesworth <cpan@entitymodel.com>

Copyright Tom Molesworth 2008-2011. Licensed under the same terms as Perl itself.