File Coverage

blib/lib/MongoDBx/Bread/Board/Container.pm
Criterion Covered Total %
statement 15 33 45.4
branch n/a
condition n/a
subroutine 5 7 71.4
pod 0 1 0.0
total 20 41 48.7


line stmt bran cond sub pod time code
1             package MongoDBx::Bread::Board::Container;
2 3     3   2184718 use Moose;
  3         1236399  
  3         25  
3 3     3   25685 use Bread::Board;
  3         3635692  
  3         21  
4 3     3   6737 use MongoDB;
  3         5395385  
  3         1328  
5              
6             extends 'Bread::Board::Container';
7              
8             has '+name' => ( default => 'MongoDB' );
9             has 'host' => ( is => 'ro', isa => 'Str', default => 'mongodb://localhost:27017' );
10              
11             has 'additional_connection_params' => (
12             is => 'ro',
13             isa => 'HashRef',
14             default => sub { +{} }
15             );
16              
17             has 'mongo_connection_class' => (
18             is => 'ro',
19             isa => 'Str',
20             default => 'MongoDB::Connection'
21             );
22              
23             has 'database_layout' => (
24             is => 'ro',
25             isa => 'HashRef[ ArrayRef[ Str ] ]',
26             required => 1,
27             );
28              
29             sub BUILD {
30 0     0 0   my $self = shift;
31              
32 0           my $conn_class = $self->mongo_connection_class;
33              
34             container $self => as {
35              
36 0     0     service 'host' => $self->host;
37              
38             service 'connection' => (
39             lifecycle => 'Singleton',
40             class => $conn_class,
41             block => sub {
42 0           my $s = shift;
43 0           $conn_class->new(
44             host => $s->param('host'),
45 0           %{ $self->additional_connection_params }
46             );
47             },
48 0           dependencies => [ 'host' ],
49             );
50              
51 0           foreach my $db_name ( keys %{ $self->database_layout } ) {
  0            
52              
53 0           my $dbh = "${db_name}_dbh";
54              
55             service $dbh => (
56             block => sub {
57 0           (shift)->param('connection')
58             ->get_database( $db_name );
59             },
60 0           dependencies => [ 'connection' ]
61             );
62              
63             container $db_name => as {
64              
65 0           foreach my $coll_name ( @{ $self->database_layout->{ $db_name } } ) {
  0            
66             service $coll_name => (
67             block => sub {
68 0           (shift)->param( $dbh )
69             ->get_collection( $coll_name );
70             },
71 0           dependencies => [ "../$dbh" ]
72             );
73             }
74              
75 0           };
76             }
77              
78 0           };
79             }
80              
81 3     3   37 no Moose; no Bread::Board; 1;
  3     3   7  
  3         30  
  3         713  
  3         7  
  3         28  
82              
83              
84              
85             =pod
86              
87             =head1 NAME
88              
89             MongoDBx::Bread::Board::Container
90              
91             =head1 VERSION
92              
93             version 0.003
94              
95             =head1 SYNOPSIS
96              
97             use MongoDBx::Bread::Board::Container;
98              
99             # create a container
100              
101             my $c = MongoDBx::Bread::Board::Container->new(
102             name => 'MongoDB',
103             host => $HOST,
104             database_layout => {
105             test => [qw[ foo bar ]],
106             test_too => [qw[ baz gorch ]]
107             }
108             );
109              
110             # fetch the 'foo' collection
111             # from the 'test' database
112             my $foo = $c->resolve( service => 'MongoDB/test/foo');
113              
114             # get the MongoDB::Database
115             # object for the 'test' db
116             my $test = $c->resolve( service => 'MongoDB/test_dbh');
117              
118             # get the MongoDB::Connection
119             # object used for all the above
120             my $conn = $c->resolve( service => 'MongoDB/connection');
121              
122             # you can also create the container
123             # within an existing Bread::Board config
124              
125             container 'MyProject' => as {
126              
127             # embed the Mongo container ...
128             container(
129             MongoDBx::Bread::Board::Container->new(
130             name => 'MyMongoDB',
131             host => $HOST,
132             database_layout => {
133             test => [qw[ foo bar ]],
134             test_too => [qw[ baz gorch ]]
135             }
136             )
137             );
138              
139             # create services that depend
140             # on the MongoDB container
141             service 'foobar' => (
142             class => 'FooBar',
143             dependencies => {
144             collection => 'MyMongoDB/test/foo'
145             }
146             );
147             };
148              
149             =head1 DESCRIPTION
150              
151             This is a subclass of L<Bread::Board::Container> which
152             can be used to provide services for your L<MongoDB> consuming
153             code. It manages your connection and additionally using the
154             C<database_layout> attribute can provide services to access
155             your databases and collections as well.
156              
157             =head1 ATTRIBUTES
158              
159             =head2 name
160              
161             This is inherited from L<Bread::Board::Container>, this
162             defaults to 'MongoDB' in this container.
163              
164             =head2 host
165              
166             The hostname passed to L<MongoDB::Connection>, this
167             defaults to 'mongodb://localhost:27017'.
168              
169             =head2 additional_connection_params
170              
171             If you want to pass additional parameters to the
172             L<MongoDB::Connection> constructor, just supply them
173             here and they will get merged in with the C<host> and
174             C<port> params.
175              
176             =head2 mongo_connection_class
177              
178             This is the name of the MongoDB connection class, it
179             default to MongoDB::Connection, which is what you want
180             to use most of the time, but if you want something else
181             then you put it here.
182              
183             =head2 database_layout
184              
185             This is a data structure that represents the databases
186             and collections you want to access. It is a HASH ref
187             where the keys are the database names and the values
188             are ARRAY refs of collection names. The set of
189             sub-containers and services will then be created based
190             on this information. See the C<SYNOPSIS> and the tests
191             for more detailed examples.
192              
193             This attribute is required.
194              
195             =head1 AUTHOR
196              
197             Stevan Little <stevan.little@iinteractive.com>
198              
199             =head1 COPYRIGHT AND LICENSE
200              
201             This software is copyright (c) 2012 by Infinity Interactive, Inc..
202              
203             This is free software; you can redistribute it and/or modify it under
204             the same terms as the Perl 5 programming language system itself.
205              
206             =cut
207              
208              
209             __END__
210             # ABSTRACT: An easy to use Bread::Board container for MongoDB
211