Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save xenoterracide/7049630 to your computer and use it in GitHub Desktop.
package MyContainer;
use 5.010;
use Moose;
use Bread::Board::Declare;
sub instance {
state $locator = __PACKAGE__->new;
return $locator;
}
has config => (
isa => 'Config::Merge',
is => 'ro',
lifecycle => 'Singleton'
block => sub { Config::Merge->new('/path/to/config'); },
):
has conn => (
isa => 'DBIx::Connector',
dependencies => [ qw( config ) ],
lifecycle => 'Singleton',
handles => ['dbh'],
block => sub {
my $s = shift;
my $config = $s->param('config');
return DBIx::Connector->new( $config->('db.dsn'), $config->('db.user'), $config->('db.pass') );
},
);
},
);
package Role::Locator;
...
use Module::Runtime qw( use_module );
has _locator_class => (
isa => 'Str',
is => 'ro',
lazy => 1,
default => sub { 'MyContainer' },
);
has _locator => (
isa => 'Bread::Board::Container',
is => 'ro',
lazy => 1,
default => sub { use_module( $self->_locator_class )->instance },
);
package ProductVariant;
with 'Role::Locator';
sub _load_categories {
...
my $dbh = $self->_locator->dbh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment