NAME

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

VERSION

version 0.16

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',
      dependencies => {
          template_root => dep(value => './root/templates'),
      },
  );

  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. See Bread::Board::Declare::Meta::Role::Attribute::Service for a full list of additional parameters to has.

If infer => 1 is passed in the attribute definition, the class in the type constraint will be introspected to find its required dependencies, and those dependencies will be automatically fulfilled as much as possible by corresponding services in the container. See Bread::Board::Manual::Concepts::Typemap for more information.

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).

EXPORTS

dep

  dependencies => {
      foo => dep('foo'),
      bar => dep(value => 'bar'),
  }

This is a helper function for specifying dependency lists. Passing a single argument will explicitly mark it as a dependency to be resolved by looking it up in the container. This isn't strictly necessary (the dependency specifications for Bread::Board have a coercion which does this automatically), but being explicit can be easier to understand at times.

This function can also take a hash of arguments. In that case, an anonymous service is created to satisfy the dependency. The hash is passed directly to the constructor for the appropriate service: if the value parameter is passed, a Bread::Board::Literal service will be created, if the block parameter is passed, a Bread::Board::BlockInjection service will be created, and if the class parameter is passed, a Bread::Board::ConstructorInjection service will be created. Note that these anonymous services cannot have dependencies themselves, nor can they be depended on by other services.

BUGS

No known bugs.

Please report any bugs to GitHub Issues at https://github.com/doy/bread-board-declare/issues.

SEE ALSO

Bread::Board

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@tozt.net>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2014 by Jesse Luehrs.

This is free software, licensed under:

  The MIT (X11) License