Using Bread::Board to Create a Config API

user-pic

Wiki Extras for this post

I've finally been able to play with the massively fun toy that is Bread::Board.

What is it, you say?

Bread::Board is basically a way to wire application components up in a DRY, and efficient manner. This is extremely useful for application configuration, and application setup (database handles/connection pooling, configuration file location, logger class setup, etc.).

The example I have put together involves the above, with a relatively sane method for setting up a configuration API so it's reusable and not a TOTAL pain in the ass.

Basically, I looked at blawd and stole the configuration bits and mangled them into something I could use.

Check it out (this comes from my minimal CMS Deimos:

package Deimos::ConfigContainer;
use Moose;
use namespace::autoclean;
use Bread::Board;
use Data::Dumper;
use Deimos::Schema; 
use Config::JFDI;
use Template;

has 'log_file_name' => (
  is      => 'ro',
  isa     => 'Str',
  default => "deimos.conf"
);

has 'config' => (
  is => 'ro',
  lazy_build => 1,
);

sub _build_config {
  my $self = shift;
  my $cfg = Config::JFDI->new( path => $something . $self->log_file_name );
  return $cfg->get;
}

sub build {
  my ($self, $service) = @_;
  my $cfg = $self->config;
  my $c = container Deimos => as {

    service 'site_title' => (
        block => sub {
            my $self = shift;
            return $self->config->{'site_title'};
        },
        dependencies => [ depends_on('config') ],
    );

    service 'log_file' => $self->log_file_name;

    service 'config' => $cfg;

    service 'schema' => (
        lifecycle => "Singleton",
        block => sub {
            warn "config: " . Dumper $cfg;
           return Deimos::Schema->connect(
               @{ $cfg->{'Database'}->{'connect_info'} },
            );
          }
    );

    service 'templates' => ( 
        block => sub {
            return Template->new($cfg->{'templates'});
        }
    );

    service 'application' => (
        class        => 'Deimos',
        dependencies => {
            schema     => depends_on('schema'),
            config     => depends_on('config'),
            site_title => depends_on('site_title'),
        }
    );
};

    return $c->fetch($service)->get;
}

 __PACKAGE__->meta->make_immutable;
1;

Basically, this allows you to either use your configcontainer, or, like I plan to, create a role, and apply it as needed and do $self->config->fetch('service_name');

This makes life MUCH much easier on the configuration front.

I hope this helps someone out there like it helped me when I finally got it figured out.

No TrackBacks

TrackBack URL: http://www.catalyzed.org/mt/mt-tb.fcgi/85

Leave a comment

All comments are moderated. Spammers don't waste your time

Sponsored By


Ionzero: Rescue your dev project.

Following

Not following anyone

Note to spammers: all comments are moderated. Don't waste your time