Skip to content

Instantly share code, notes, and snippets.

@xenoterracide
Last active December 25, 2015 22:39
Show Gist options
  • Select an option

  • Save xenoterracide/7051143 to your computer and use it in GitHub Desktop.

Select an option

Save xenoterracide/7051143 to your computer and use it in GitHub Desktop.
package MyContainer;
use 5.010;
use Moose;
use Bread::Board::Declare;
has config => (
isa => 'Config::Merge',
is => 'bare',
lifecycle => 'Singleton'
block => sub { Config::Merge->new('/path/to/config'); },
):
has conn => (
isa => 'DBIx::Connector',
dependencies => [ qw( config ) ],
lifecycle => 'Singleton',
handles => ['dbh'],
is => 'bare',
block => sub {
my $s = shift;
my $config = $s->param('config');
return DBIx::Connector->new( $config->('db.dsn'), $config->('db.user'), $config->('db.pass') );
},
);
},
);
has controller => (
isa => 'MyController',
is => 'bare',
infer => 1,
);
has product_repository => (
isa => 'Repository::Product',
is => 'bare',
lifecycle => 'Singleton',
infer => 1,
);
package Role::DBH;
...
use Module::Runtime qw( use_module );
has _conn => (
isa => 'DBIx::Connector',
is => 'ro',
required => 1,
);
package Repository::Product;
with 'Role::DBH';
sub get {
... #deal with input and sql creation
my $data = $self->_conn->dbh...
... # transform the data structure
return Product->new({ _conn => $self->_conn, %{ $data } });
}
package Product;
with 'Role::DBH';
sub variants {
... #deal with input and sql creation
my $data = $self->_conn->dbh->...
... # transform the data structure
my @variants;
foreach my $params ( @$data ) {
ProductVariant->new({ _conn => $self->_conn, %{ $data } });
}
return @variants;
}
package ProductVariant;
with 'Role::DBH';
sub categories {
... #deal with input and sql creation
my $data = $self->_conn->dbh->...
... # transform the data structure
my @cats;
foreach my $params ( @$data ) {
push @cats, Category->new({ _conn-> => $self->_conn, %{ $data } });
}
return @cats;
}
package Categories;
with 'Role::DBH';
package MyController;
use Moose;
has _products => (
isa => 'Repository::Product',
is => 'ro',
required => 1,
sub do_the_needful {
...
my @variants = $self->_products->get($id)->variants;
foreach my $variant ( @variants ) {
my @cats = $variants->categories;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment