The SQLConnection role provides a DB handle to a mysql database. When an object is instantiated (example my $cell = GSM::Cell->new('LAC' => 203, 'CI' => 20021)), i would like the other attributes (BCCH,NAME) to be initialized with values from the mysql database. This is currently the purpose of the default functions set_name and set_bcch. While this works, i was hoping to use moose meta-programming to arrive at a DRYer solution. See http://babyl.dyndns.org/techblog/2009/05/moose-attribute-meta-meddling.html for a neat idea. The idea is that the resulting attributes would look something like this:package GSM::Cell; use Moose; use MooseX::AttributeHelpers; use Data::Dumper; has 'BCCH' => (is => 'rw', lazy => 1, default => &set_bcch); has 'LAC' => (is => 'ro', required => 1); has 'CI' => (is => 'ro', required => 1); has 'NAME' => (is => 'rw', lazy => 1, default => &set_name); has 'ID' => (is => 'ro', lazy => 1, default => \&set_id); with qw(SQLConnection); sub set_id { my $self = shift; return join(',',$self->LAC,$self->CI); } sub set_bcch { my $self = shift; my $sth = $self->dbh->prepare('select BCCHFrequency from Cell wher +e CI = ? and LAC = ?); $sth->execute($self->CI,$self->LAC); return $sth->fetchrow_array; } sub set_name { my $self = shift; my $sth = $self->dbh->prepare('select UserLabel from Cell where CI + = ? and LAC = ?'); $sth->execute($self->CI,$self->LAC); return $sth->fetchrow_array; } no Mouse; __PACKAGE__->meta->make_immutable; 1; #so the 'require' or 'use' succeeds
with package SQLInitialize extendeding Moose::Meta::Attribute and implementing the necessary logic. The problem is that i would need access to the instance data $self->LAC and $self->CI for the SQL lookup (as can be seen in the set_bcch and set_name methods.has BCCH => ( metaclass => 'SQLInitilaize', is => 'ro', table => 'Cell', )
In reply to Moose, SQL and initialization via Moose::Meta::Attribute by ZAmonk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |