Marcos,

I think you are mixing up classes and objects here.

The value of the 'type' attribute is an instance parameter (meaning, an object), while the 'isa' for a 'value' attribute is part of the class.

I would encourage you to look into other ORMs out there such as DBIx::Class and Fey::ORM instead of re-inventing this wheel. Or at the very least take a look at how those two modules are implemented so you avoid some of the more common pitfalls of home-grown ORMs.

- Stevan


On Oct 6, 2010, at 6:38 PM, Marcos Barbeitos wrote:

Hi, folks! i'm a newbie to Moose but I find it extremely useful. I am currently developing a package that interfaces with mysql databases using
DBI. I can instantiate a Field object as follows:

package Field;

use Moose;
use Moose::Util::TypeConstraints;
use MooseX::StrictConstructor;

use lib /SVN/WbMx/trunk';
use Constants qw( DBI_CONSTANT );

subtype 'DBIConstant'
  => as 'Str'
  => where { DBI_CONSTANT( $_ ) }
  => message { "$_ is not a valid DBI type." };

has 'name'         => ( is => 'ro', isa => 'Str', required => 1 );

has 'index'        => ( is => 'ro', isa => 'Int', required => 1 );

has 'type' => ( is => 'ro', isa => 'DBIConstant', required => 1 );

has 'precision'    => ( is => 'ro', isa => 'Int', required => 1 );

has 'is_nullable'  => ( is => 'ro', isa => 'Str', default => 'NO' );

has 'primary_key'  => ( is => 'ro', isa => 'Bool', default => 0, );

has 'foreign_key'  => ( is => 'ro', isa => 'Str', required => 0 );

__PACKAGE__->meta->make_immutable;

The function DBI_CONSTANTS encapsulates the DBI type checking, you may use
either the usual SQL types (VARCHAR, INTEGER, etc.) or their numeric
equivalents. The attribute missing here is 'value', and here's where I am kind of lost. I'd like to be able to set 'isa' dynamically, so that if the Field type was VARCHAR, the new attribute would be 'has 'value' => ( is =>
'ro', isa => 'Str', required => 0 )', if it was INTEGER, I'd have 'has
'value' => ( is => 'ro', isa => 'Int', required => 0 )', etc. I suppose I
could implement that using roles to provide a 'setter' method for the
attribute 'value', but I can't figure out how to tell the role to check the the attribute 'type' before hand so it can set 'isa' accordingly. Any help
will be greatly appreciated.  Thanks!

--
Marcos S. Barbeitos
Post-doctoral researcher
American Museum of Natural History
Department of Invertebrate Zoology
79th st and Central Park West
New York, NY 10024

Reply via email to