File Coverage

blib/lib/Starch/Factory.pm
Criterion Covered Total %
statement 60 60 100.0
branch 7 8 87.5
condition n/a
subroutine 17 17 100.0
pod 3 4 75.0
total 87 89 97.7


line stmt bran cond sub pod time code
1             package Starch::Factory;
2             our $VERSION = '0.14';
3              
4             =encoding utf8
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   6112 use Module::Runtime qw( require_module );
  13         21221  
  13         70  
21 13     13   612 use Moo::Object qw();
  13         25  
  13         178  
22 13     13   5605 use Moo::Role qw();
  13         173928  
  13         304  
23 13     13   5345 use Starch::Manager;
  13         48  
  13         549  
24 13     13   109 use Starch::State;
  13         26  
  13         357  
25 13     13   70 use Starch::Util qw( load_prefixed_module croak );
  13         28  
  13         801  
26 13     13   84 use Types::Common::String -types;
  13         23  
  13         115  
27 13     13   18645 use Types::Standard -types;
  13         29  
  13         78  
28              
29 13     13   54719 use Moo;
  13         71  
  13         77  
30 13     13   5498 use strictures 2;
  13         115  
  13         547  
31 13     13   2604 use namespace::clean;
  13         26  
  13         103  
32              
33             with 'Starch::Plugin::Bundle';
34              
35             =head1 OPTIONAL ARGUMENTS
36              
37             =head2 plugins
38              
39             This is the L attribute, but altered
40             to be an argument.
41              
42             =cut
43              
44             has '+plugins' => (
45             init_arg => 'plugins',
46             );
47             sub bundled_plugins {
48 18     18 0 456 return [];
49             }
50              
51             =head2 base_manager_class
52              
53             The base class of the Starch manager object. Default to C.
54              
55             =cut
56              
57             has base_manager_class => (
58             is => 'lazy',
59             isa => NonEmptySimpleStr,
60             default => 'Starch::Manager',
61             );
62              
63             =head2 base_state_class
64              
65             The base class of Starch state objects. Default to C.
66              
67             =cut
68              
69             has base_state_class => (
70             is => 'lazy',
71             isa => NonEmptySimpleStr,
72             default => 'Starch::State',
73             );
74              
75             =head1 ATTRIBUTES
76              
77             =head2 manager_class
78              
79             The anonymous class which extends L and has
80             L applied to it.
81              
82             =cut
83              
84             has manager_class => (
85             is => 'lazy',
86             isa => ClassName,
87             init_arg => undef,
88             );
89             sub _build_manager_class {
90 70     70   856 my ($self) = @_;
91              
92 70         1318 my $roles = $self->manager_roles();
93 70         3527 my $class = $self->base_manager_class();
94 70         4728 require_module( $class );
95              
96 70 100       2693 return $class if !@$roles;
97              
98 23         130 return Moo::Role->create_class_with_roles( $class, @$roles );
99             }
100              
101             =head2 state_class
102              
103             The anonymous class which extends L and has
104             L applied to it.
105              
106             =cut
107              
108             has state_class => (
109             is => 'lazy',
110             isa => ClassName,
111             init_arg => undef,
112             );
113             sub _build_state_class {
114 24     24   328 my ($self) = @_;
115              
116 24         457 my $roles = $self->state_roles();
117 24         1414 my $class = $self->base_state_class();
118 24         1376 require_module( $class );
119              
120 24 100       867 return $class if !@$roles;
121              
122 11         90 return Moo::Role->create_class_with_roles( $class, @$roles );
123             }
124              
125             =head1 METHODS
126              
127             =head2 base_store_class
128              
129             my $class = $factory->base_store_class( '::Memory' );
130             # Starch::Store::Memory
131            
132             my $class = $factory->base_store_class( 'Starch::Store::Memory' );
133             # Starch::Store::Memory
134              
135             Given an absolute or relative store class name this will
136             return the resolved class name.
137              
138             =cut
139              
140             sub base_store_class {
141 120     120 1 266 my ($self, $suffix) = @_;
142              
143 120         402 return load_prefixed_module(
144             'Starch::Store',
145             $suffix,
146             );
147             }
148              
149             =head2 store_class
150              
151             my $class = $factory->store_class( '::Memory' );
152              
153             Given an absolute or relative store class name this will
154             return an anonymous class which extends the store class
155             and has L applied to it.
156              
157             =cut
158              
159             sub store_class {
160 120     120 1 271 my ($self, $suffix) = @_;
161              
162 120         2013 my $roles = $self->store_roles();
163 120         3160 my $class = $self->base_store_class( $suffix );
164              
165 120 100       432 return $class if !@$roles;
166              
167 68         288 return Moo::Role->create_class_with_roles( $class, @$roles );
168             }
169              
170             =head2 new_store
171              
172             my $store = $factory->new_store( class=>'::Memory', %args );
173              
174             Creates and returns a new L object with the
175             factory argument set.
176              
177             Note that since the L argument is
178             required you must specify it.
179              
180             =cut
181              
182             sub new_store {
183 120     120 1 1575 my $self = shift;
184              
185 120         2563 my $args = Moo::Object->BUILDARGS( @_ );
186 120         12239 $args = { %$args };
187 120         321 my $suffix = delete $args->{class};
188 120 50       381 croak "No class key was declared in the Starch store hash ref"
189             if !defined $suffix;
190              
191 120         336 my $class = $self->store_class( $suffix );
192 120         25409 return $class->new(
193             %$args,
194             );
195             }
196              
197             1;
198             __END__