On Mon, 28 Mar 2011 09:49:22 -0700, Sam Brain <[email protected]> wrote:
> (1) Does DBIx::BuildMooseObjects seem a reasonable name, and
I don't see anything Moose-specific here. The interesting bit is the
transformation from a single-level array to a multi-level structure grouped by
specific values, and you could write that without any objects at all, e.g.
> J Smith | M | 35 | A Jones, MD | 12/08/2009 | B.P | 120/80
> J Smith | M | 35 | A Jones, MD | 12/08/2009 | Weight | 156
> J Smith | M | 35 | A Jones, MD | 03/19/2010 | B.P | 122/88
can just be transformed into:
[
{
name => 'J Smith',
sex => 'M',
age => 35,
visits => [
{
md_name => 'A Jones, MD',
date => '12/08/2009',
tests => [ ... ]
},
{
md_name => 'A Jones, MD',
date => '03/19/2010',
tests => [ ... ]
}
]
}
]
So, write a function that does that, though you probably already have one, or
most of one. Then write coercions for your Moose classes from HashRef and
ArrayRef[HashRef]:
coerce 'Test', from 'HashRef', via { Test->new($_) };
subtype 'ArrayRefOfTest', as 'ArrayRef[Test]';
coerce 'ArrayRefOfTest', from 'ArrayRef[HashRef]',
via { [ map { Test->new($_) } @$_ ] };
Assuming you write coercions for Test, Visit, and Patient, all you need then is
to call Patient->new with the nested data structure above:
my @patients = map { Patient->new($_) }
your_grouping_function($dbh->selectall_arrayref, ...);
hdp.