Skip to content

Instantly share code, notes, and snippets.

@jeffa
Created October 21, 2015 21:16
Show Gist options
  • Select an option

  • Save jeffa/660c1c29cbc881f681c7 to your computer and use it in GitHub Desktop.

Select an option

Save jeffa/660c1c29cbc881f681c7 to your computer and use it in GitHub Desktop.
ORM proof of concept
package DBIx::Object;
use base 'DBI';
package DBIx::Object::st;
use base 'DBI::st';
package DBIx::Object::db;
use base 'DBI::db';
use Moose;
around 'selectall_arrayref' => sub {
my $orig = shift;
my $self = shift;
return $self->$orig(@_) unless ref($_[1]) eq 'HASH' and exists $_[1]->{Class};
my $class = delete $_[1]->{Class};
$_[1]->{Slice} = {};
my $arrayref = $self->$orig(@_);
bless $_, $class for @$arrayref;
return $arrayref;
};
around 'selectall_hashref' => sub {
my $orig = shift;
my $self = shift;
return $self->$orig(@_) unless ref($_[2]) eq 'HASH' and exists $_[2]->{Class};
my $class = delete $_[2]->{Class};
my $hashref = $self->$orig(@_);
bless $_, $class for values %$hashref;
return $hashref;
};
around 'selectrow_hashref' => sub {
my $orig = shift;
my $self = shift;
return $self->$orig(@_) unless ref($_[1]) eq 'HASH' and exists $_[1]->{Class};
my $class = delete $_[1]->{Class};
my $hashref = $self->$orig(@_);
return bless $hashref, $class;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment