The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Bread::Board::Declare - create Bread::Board containers as normal Moose objects

VERSION

version 0.05

SYNOPSIS

  package MyApp;
  use Moose;
  use Bread::Board::Declare;

  has dsn => (
      is    => 'ro',
      isa   => 'Str',
      value => 'dbi:mysql:my_db',
  );

  has dbic => (
      is           => 'ro',
      isa          => 'MyApp::Model::DBIC',
      dependencies => ['dsn'],
      lifecycle    => 'Singleton',
  );

  has tt => (
      is  => 'ro',
      isa => 'MyApp::View::TT',
  );

  has controller => (
      is           => 'ro',
      isa          => 'MyApp::Controller',
      dependencies => {
          model => 'dbic',
          view  => 'tt',
      },
  );

  MyApp->new->controller; # new controller object with new model and view
  MyApp->new(
      model => MyApp::Model::KiokuDB->new,
  )->controller; # new controller object with new view and kioku model

DESCRIPTION

This module is a Moose extension which allows for declaring Bread::Board container classes in a more straightforward and natural way. It sets up Bread::Board::Container as the superclass, and creates services associated with each attribute that you create, according to these rules:

  • If the service => 0 option is passed to has, no service is created.

  • If the value option is passed to has, a Bread::Board::Literal service is created, with the given value.

  • If the block option is passed to has, a Bread::Board::BlockInjection service is created, with the given coderef as the block. In addition to receiving the service object (as happens in Bread::Board), this coderef will also be passed the container object.

  • If the attribute has a type constraint corresponding to a class, a Bread::Board::ConstructorInjection service is created, with the class corresponding to the type constraint.

  • Otherwise, a BlockInjection service is created which throws an exception. This allows services to be created for the sole purpose of being set through the attribute, without requiring a default to be specified. Note that required => 1 is still valid on these attributes.

Constructor parameters for services (dependencies, lifecycle, etc) can also be passed into the attribute definition; these will be forwarded to the service constructor.

In addition to creating the services, this module also modifies the attribute reader generation, so that if the attribute has no value, a value will be resolved from the associated service. It also modifies the get method on services so that if the associated attribute has a value, that value will be returned immediately. This allows for overriding service values by passing replacement values into the constructor, or by calling setter methods.

Note that default/builder doesn't make a lot of sense in this setting, so they are explicitly disabled. In addition, multiple inheritance would just cause a lot of problems, so it is also disabled (although single inheritance and role application works properly).

NOTE: When using this module in roles with Moose versions prior to 2.0, the attribute trait will need to be applied explicitly to attributes that should become services, as in:

  has attr => (
      traits => ['Service'],
      is     => 'ro',
      isa    => 'Str',
      value  => 'value',
  )

BUGS

No known bugs.

Please report any bugs through RT: email bug-bread-board-declare at rt.cpan.org, or browse to http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bread-Board-Declare.

SEE ALSO

Please see those modules/websites for more information related to this module.

SUPPORT

You can find this documentation for this module with the perldoc command.

    perldoc Bread::Board::Declare

You can also look for information at:

AUTHOR

Jesse Luehrs <doy at tozt dot net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Jesse Luehrs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.