Thank you very much for this. I tinkered around with my code last night and realized that I should not be exporting like that. This is what I came up with:
package Table; use DBI; use Moose; use Moose::Exporter; Moose::Exporter->setup_import_methods( with_caller => [ 'has_table' ] +); sub has_table { my $class = shift; my $table = shift; my %args = @_; my $rows = _get_table_rows( $table ); # this is just so client doesn't have to pass table name to load() $class->meta->add_attribute( '_table', is => 'ro', reader => 'tabl +e', default => $table ); for my $row (@$rows) { $class->meta->add_attribute( $row->{Field}, is => 'rw' ); } } sub load { my $self = shift; my %args = @_; my %fields = map { /^set_(\w+)$/; $1 => $_ } grep /^set_/, map $_->name, $self->meta->get_all_methods ; my $dbh = DBI->connect( ... ); my $table = $self->table; my $fields = join( ', ', keys %fields ); my $record = $dbh->selectrow_hashref( "select $fields from $table +where id = ?", undef, $self->get_id ); for my $field (keys %fields) { my $writer = $fields{$field}; $self->$writer( $record->{$field} ); } } package Table::Loader; use Moose::Role; sub BUILD { my $self = shift; if ($self->get_id) { Table::load( $self, id => $self->get_id ); } } package MyTable; use Moose::Policy 'Moose::Policy::FollowPBP'; use Moose; use Table; with 'Table::Loader'; has_table 'my_table'; # psuedo code package main; my $table = MyTable->new( id => 1 );
I wonder what kind of contracts I am breaking with any class that wants to use this ... but it seems to work. The trick is loading the data into the object after it has been defined.

Question if you have the gumption ... how would you handle loading the data? How would you pass the unique identifier of the database table row to be loaded into your MooseX::Role::Parameterized solution? That's the trouble I seem to have now, and hence why I added the Table::Loader role.

Thanks again!


In reply to Re^4: Moose + ORM by Anonymous Monk
in thread Moose + ORM by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.