dagolden / Meerkat

MongoDB documents as Moose objects
6 stars 4 forks source link

Add to_json support from Document Role #9

Open jmmills opened 9 years ago

jmmills commented 9 years ago

Add a default but sub-classable serializing method to Meerkat::Role::Document... perhaps just expose the storage serializer? I'm already defining the structure of the object in Mongo via Moose attributes, why should I have to do the same for a hash? This would make developing REST interfaces around Meerkat objects super simple:

For example:

package My::Model::Foo;
use Moose;
with 'Meerkat::Role::Document';

has somekey => (is => 'ro', isa => 'String');
has otherkey => (is => 'ro, isa => 'Bool');

__PACKAGE__->meta->make_immutable;

package Inside::Some::Dancer::Like::REST::Framework;

get 'things' => sub {
   my $obj = meerkat->collection('foo')->find_one({somekey => 'doodad'});
   return $obj? $obj->to_hash : sub {
      status 404;
      { errormsg => "Can't find this thing" };
   }->();
}
dagolden commented 9 years ago

Interesting idea. I'll consider this more when I'm back to work in the new year. :-)