Cagao has asked for the wisdom of the Perl Monks concerning the following question:
All my code these days is object oriented, maybe to the extreme I sometimes think!
In a recent project I'm creating objects to model pretty much everything, be it a customer, a company, a training course, even a basic list of industries ( just id => name ).
Starting with a basic " id => name " scenario, I have a My::Industry class with a get method...
my @industries = My::Industry->get();
Which will return a list of industry objects. All very good so far.
I then introduce filtering such as...
my ($industry) = My::Industry->get( id => 7 );
Note the fact that the interface remains the same, always returning a list, even if only 1, which I kinda like.
A common get() method to save having a get_all(), get_by_id(), get_by_name() arrangment.
Now extending this to the My::Customer class, works exactly the same.
I can even ask for SQL-style modifiers...
my @jobs = My::Customer->get( sex => 'M', employed => 1, sort => [ +'id', 'desc' ] );
You can see I've extended the interface to allow different sort directions, which is simple enough, and makes logical sense, to me anyway.
However, I now want to select objects that are OLDER than a specified DateTime - simply passing in a DateTime would look like I was looking for perfect matches...
my @jobs = My::Customer->get( signed_up => DateTime->now, employed +=> 1 );
So I'd need something to indicate the direction I'm after, perhaps something like...
my @jobs = My::Customer->get( signed_up => [ DateTime->now, '>' ], +employed => 1 );
Maybe I have just answered my own question here, but after some constructive ideas, or what other people have done in similar scenarios.
This is all in Moose btw, incase there's some helpers I'm not yet aware of.
Thanks for reading, and any advice you care to share.
|
---|