File Coverage

blib/lib/Starch/Factory.pm
Criterion Covered Total %
statement 62 62 100.0
branch 7 8 87.5
condition n/a
subroutine 18 18 100.0
pod 3 4 75.0
total 90 92 97.8


line stmt bran cond sub pod time code
1             package Starch::Factory;
2 13     13   215 use 5.008001;
  13         45  
3 13     13   73 use strictures 2;
  13         80  
  13         442  
4             our $VERSION = '0.11';
5              
6             =head1 NAME
7              
8             Starch::Factory - Role applicator and class creator.
9              
10             =head1 DESCRIPTION
11              
12             This class consumes the L role and
13             is used by L to apply specified plugins to manager,
14             state, and store classes.
15              
16             Normally there is no need to interact with this class directly.
17              
18             =cut
19              
20 13     13   8670 use Moo::Role qw();
  13         213458  
  13         400  
21 13     13   1852 use Types::Standard -types;
  13         220652  
  13         128  
22 13     13   61065 use Types::Common::String -types;
  13         68167  
  13         110  
23 13     13   23750 use Moo::Object qw();
  13         8653  
  13         356  
24 13     13   5160 use Starch::Util qw( load_prefixed_module croak );
  13         41  
  13         766  
25 13     13   91 use Module::Runtime qw( require_module );
  13         26  
  13         91  
26              
27 13     13   6171 use Starch::Manager;
  13         51  
  13         528  
28 13     13   108 use Starch::State;
  13         26  
  13         300  
29              
30 13     13   66 use Moo;
  13         26  
  13         91  
31 13     13   5899 use namespace::clean;
  13         30  
  13         103  
32              
33             with qw(
34             Starch::Plugin::Bundle
35             );
36              
37             =head1 OPTIONAL ARGUMENTS
38              
39             =head2 plugins
40              
41             This is the L attribute, but altered
42             to be an argument.
43              
44             =cut
45              
46             has '+plugins' => (
47             init_arg => 'plugins',
48             );
49             sub bundled_plugins {
50 18     18 0 425 return [];
51             }
52              
53             =head2 base_manager_class
54              
55             The base class of the Starch manager object. Default to C.
56              
57             =cut
58              
59             has base_manager_class => (
60             is => 'lazy',
61             isa => NonEmptySimpleStr,
62             default => 'Starch::Manager',
63             );
64              
65             =head2 base_state_class
66              
67             The base class of Starch state objects. Default to C.
68              
69             =cut
70              
71             has base_state_class => (
72             is => 'lazy',
73             isa => NonEmptySimpleStr,
74             default => 'Starch::State',
75             );
76              
77             =head1 ATTRIBUTES
78              
79             =head2 manager_class
80              
81             The anonymous class which extends L and has
82             L applied to it.
83              
84             =cut
85              
86             has manager_class => (
87             is => 'lazy',
88             isa => ClassName,
89             init_arg => undef,
90             );
91             sub _build_manager_class {
92 70     70   911 my ($self) = @_;
93              
94 70         1219 my $roles = $self->manager_roles();
95 70         3598 my $class = $self->base_manager_class();
96 70         4747 require_module( $class );
97              
98 70 100       2685 return $class if !@$roles;
99              
100 23         146 return Moo::Role->create_class_with_roles( $class, @$roles );
101             }
102              
103             =head2 state_class
104              
105             The anonymous class which extends L and has
106             L applied to it.
107              
108             =cut
109              
110             has state_class => (
111             is => 'lazy',
112             isa => ClassName,
113             init_arg => undef,
114             );
115             sub _build_state_class {
116 24     24   294 my ($self) = @_;
117              
118 24         438 my $roles = $self->state_roles();
119 24         1435 my $class = $self->base_state_class();
120 24         1381 require_module( $class );
121              
122 24 100       853 return $class if !@$roles;
123              
124 11         48 return Moo::Role->create_class_with_roles( $class, @$roles );
125             }
126              
127             =head1 METHODS
128              
129             =head2 base_store_class
130              
131             my $class = $factory->base_store_class( '::Memory' );
132             # Starch::Store::Memory
133            
134             my $class = $factory->base_store_class( 'Starch::Store::Memory' );
135             # Starch::Store::Memory
136              
137             Given an absolute or relative store class name this will
138             return the resolved class name.
139              
140             =cut
141              
142             sub base_store_class {
143 120     120 1 262 my ($self, $suffix) = @_;
144              
145 120         396 return load_prefixed_module(
146             'Starch::Store',
147             $suffix,
148             );
149             }
150              
151             =head2 store_class
152              
153             my $class = $factory->store_class( '::Memory' );
154              
155             Given an absolute or relative store class name this will
156             return an anonymous class which extends the store class
157             and has L applied to it.
158              
159             =cut
160              
161             sub store_class {
162 120     120 1 257 my ($self, $suffix) = @_;
163              
164 120         2096 my $roles = $self->store_roles();
165 120         3157 my $class = $self->base_store_class( $suffix );
166              
167 120 100       538 return $class if !@$roles;
168              
169 68         288 return Moo::Role->create_class_with_roles( $class, @$roles );
170             }
171              
172             =head2 new_store
173              
174             my $store = $factory->new_store( class=>'::Memory', %args );
175              
176             Creates and returns a new L object with the
177             factory argument set.
178              
179             Note that since the L argument is
180             required you must specify it.
181              
182             =cut
183              
184             sub new_store {
185 120     120 1 1577 my $self = shift;
186              
187 120         2268 my $args = Moo::Object->BUILDARGS( @_ );
188 120         12313 $args = { %$args };
189 120         321 my $suffix = delete $args->{class};
190 120 50       358 croak "No class key was declared in the Starch store hash ref"
191             if !defined $suffix;
192              
193 120         352 my $class = $self->store_class( $suffix );
194 120         26268 return $class->new(
195             %$args,
196             );
197             }
198              
199             1;
200             __END__