| 1 | package Model::Envoy; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use MooseX::Role::Parameterized; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Module::Runtime 'use_module'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use List::AllUtils qw( first first_result ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | our $VERSION = '0.5.3'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 Model::Envoy | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | A Moose Role that can be used to build a model layer which keeps business logic separate from your storage layer. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | =head2 Synopsis | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | package My::Model::Widget; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | with 'Model::Envoy' => { storage => { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 'DBIC' => { | ||||||||||||||||||||||||||||||||||||||||||
| 20 | schema => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 21 | my ( $class ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | My::DB->db_connect(...); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub dbic { 'My::DB::Result::Widget' } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | has 'id' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 32 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 33 | traits => ['Envoy','DBIC'], | ||||||||||||||||||||||||||||||||||||||||||
| 34 | primary_key => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | has 'name' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 39 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 40 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 41 | traits => ['Envoy','DBIC'], | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | has 'no_storage' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 46 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 47 | traits => ['Envoy'], | ||||||||||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | has 'parts' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | isa => 'ArrayRef[My::Model::Part]', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | traits => ['Envoy','DBIC'], | ||||||||||||||||||||||||||||||||||||||||||
| 54 | rel => 'has_many', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | has 'envoy_ignores_me' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 58 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 59 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | package My::Models; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | with 'Model::Envoy::Set' => { namespace => 'My::Envoy' }; | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | ....then later... | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | my $widget = My::Models->m('Widget')->build({ | ||||||||||||||||||||||||||||||||||||||||||
| 71 | id => 1 | ||||||||||||||||||||||||||||||||||||||||||
| 72 | name => 'foo', | ||||||||||||||||||||||||||||||||||||||||||
| 73 | no_storage => 'bar', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | parts => [ | ||||||||||||||||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | id => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 77 | name => 'baz', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ], | ||||||||||||||||||||||||||||||||||||||||||
| 80 | envoy_ignores_me => 'hi there', | ||||||||||||||||||||||||||||||||||||||||||
| 81 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | $widget->name('renamed'); | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $widget->save; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | Mixing database logic with business rules is a common hazard when building an application's model layer. Beyond | ||||||||||||||||||||||||||||||||||||||||||
| 87 | the violation of the ideal separation of concerns, it also ties a developer's hands when needing to transition | ||||||||||||||||||||||||||||||||||||||||||
| 88 | between different storage mechanisms, or support more than one. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Model::Envoy provides an Moose-based object layer to manage business logic, and a plugin system to add one or more | ||||||||||||||||||||||||||||||||||||||||||
| 91 | persistence methods under the hood. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =head2 Setting up storage | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | Indicating which storage back ends you are using for your models is done when you include the role. It makes the most sense to | ||||||||||||||||||||||||||||||||||||||||||
| 96 | do this in a base class which your models can inherit from: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | package My::Model; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | use My::DB; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | my $schema; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | with 'Model::Envoy' => { storage => { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 'DBIC' => { | ||||||||||||||||||||||||||||||||||||||||||
| 107 | schema => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | my ( $class ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | $schema ||= My::DB->db_connect(...); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } }; | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # then.... | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | package My::Model::Widget; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | extends 'My::Model' | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | =head2 Model attributes | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Model::Envoy classes use normal Moose attribute declarations. Depending on the storage layer plugin, they may add attribute traits or other methods | ||||||||||||||||||||||||||||||||||||||||||
| 128 | your models need to implement to indicate how each attribute finds its way into and out of storage. All attributes that you want Model::Envoy to manage | ||||||||||||||||||||||||||||||||||||||||||
| 129 | do need to have at least this one trait specified: | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =head3 The 'Envoy' trait | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | This trait indicates an attribute is part of your model's data, rather than being a secondary or utility attribute (such as a handle to a logging utility). | ||||||||||||||||||||||||||||||||||||||||||
| 134 | It also provides some automatic coercion for class attributes that represent other Model::Envoy | ||||||||||||||||||||||||||||||||||||||||||
| 135 | enabled classes (or arrays of them). It will allow you to pass in hashrefs | ||||||||||||||||||||||||||||||||||||||||||
| 136 | (or arrays of hashrefs) to those attributes and have them automagically elevated | ||||||||||||||||||||||||||||||||||||||||||
| 137 | into an instance of the intended class. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | has 'parts' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 140 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 141 | isa => 'ArrayRef[My::Model::Part]', | ||||||||||||||||||||||||||||||||||||||||||
| 142 | traits => ['Envoy'], | ||||||||||||||||||||||||||||||||||||||||||
| 143 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =head2 Adding caching | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | The role inclusion can also specify a cache plugin. These operate just like storage plugins, but are checked before your storage plugins on fetches, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | and updated with results from your storage plugins on a cache miss. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | with 'Model::Envoy' => { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | storage => { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 'DBIC' => { ... }, | ||||||||||||||||||||||||||||||||||||||||||
| 153 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 154 | cache => { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 'Memory' => { ... } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =head2 Class Methods | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head3 build() | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | C<build> is a more flexible version of C<new>. It can take a standard hashref of properties just as C<new> would, but it can also take | ||||||||||||||||||||||||||||||||||||||||||
| 164 | classes that are used by your storage layer plugins and, if those plugins support this, convert them into an instance of your model object. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | =head3 get_storage('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | Passes back the storage plugin specified by C<Plugin> being used by the class. Follows the same namespace resolution | ||||||||||||||||||||||||||||||||||||||||||
| 169 | process as the instance method below. | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head3 get_cache('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Passes back the cache plugin specified by C<Plugin> being used by the class. Follows the same namespace resolution | ||||||||||||||||||||||||||||||||||||||||||
| 174 | process as the C<get_storage> instance method below. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =head2 Instance Methods | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =head3 save() | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | Save the instance to your persistent storage layer. | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head3 update($hashref) | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | Given a supplied hashref of attributes, bulk update the attributes of your instance object. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head3 delete() | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | Remove the instance from your persistent storage layer. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | =head3 dump() | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Provide an unblessed copy of the datastructure of your instance object. If any attributes are an instance of a Model::Envoy-using class (or an array of same), | ||||||||||||||||||||||||||||||||||||||||||
| 193 | they will be recursively dumped. Other blessed objects will return undef unless they have a C<stringify> or C<to_string> method, in which case those will be | ||||||||||||||||||||||||||||||||||||||||||
| 194 | used to represent the object. | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | =head3 get_storage('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | Given the namespace of a storage plugin, return the instance of it that's backing the current object. If the plugin is in the | ||||||||||||||||||||||||||||||||||||||||||
| 199 | C<Model::Envoy::Storage::> namespace, it can be abbreviated: | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | $model->get_storage('DBIC') # looks for Model::Envoy::Storage::DBIC | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | otherwise, prefix your plugin name with a C<+> to get something outside of the default namespace: | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | $model->get_storage('+My::Storage::WhatsIt'); | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =head3 get_cache('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | Works just like C<get_storage> but looks for a cache plugin instead of a storage plugin | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | =head3 in_storage('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | Returns true if the storage plugin reports your model is saved in its storage mechanism. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | =head3 in_cache('Plugin') | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | Returns true if the cache plugin reports your model is saved in its storage mechanism. | ||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | =head2 Aggregate methods | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | For operations that fetch and search for one or more models, see C<Model::Envoy::Set>. | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | 13 | parameter storage => ( | |||||||||||||||||||||||||||||||||||||||||
| 226 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 227 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 13 | parameter cache => ( | |||||||||||||||||||||||||||||||||||||||||
| 231 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 232 | required => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | 13 | my $abs_module_prefix = qr/^\+/; | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 13 | ▶ | role { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 238 | 13 | my $p = shift; | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 13 | my %plugins; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 13 | my @stores = qw( storage cache ); | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 13 | for my $store ( @stores ) { | |||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 26 | ▶ | if ( my $storage = $p->$store ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 14 | ▶ | while ( my ( $package, $conf ) = each %$storage ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 11 | my $role = _resolve_namespace($package); | |||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 11 | use_module( $role ); | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 11 | $plugins{$store}{$role} = $conf; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | has '_storage' => ( | ||||||||||||||||||||||||||||||||||||||||||
| 259 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 260 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 50 | ▶ | default => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 50 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 50 | my $instances = {}; | |||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | 50 | for my $store ( @stores ) { | |||||||||||||||||||||||||||||||||||||||||
| 267 | 100 | ▶ | next unless $plugins{$store}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 268 | 79 | $instances->{$store} = { map { $_ => undef } keys %{$plugins{$store}} }; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | 50 | return $instances; | |||||||||||||||||||||||||||||||||||||||||
| 272 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 273 | 13 | ); | |||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 116 | ▶ | method '_plugins' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_plugins: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 116 | \%plugins; | |||||||||||||||||||||||||||||||||||||||||
| 278 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 13 | }; | |||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 149 | ▶ | sub _resolve_namespace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_namespace: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 149 | my ( $namespace ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | 149 | $namespace =~ s/^Model::Envoy::Storage:://; | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | return $namespace =~ $abs_module_prefix | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 149 | ▶ | ? do { $namespace =~ s/$abs_module_prefix//; $namespace } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | : 'Model::Envoy::Storage::' . $namespace; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 65 | ▶ | sub get_storage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_storage: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 65 | my ( $self, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | 65 | ▶ | if ( ! ref $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | return $self->_get_configured_plugin_class('storage', $package); | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 63 | return $self->_plugin_instance('storage',$package); | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 3 | ▶ | sub get_cache { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cache: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 3 | my ( $self, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 3 | ▶ | if ( ! ref $self ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | return $self->_get_configured_plugin_class('cache', $package); | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | return $self->_plugin_instance('cache',$package); | |||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 13 | ▶ | sub in_storage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_storage: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 13 | my ( $self, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | 13 | ▶ | if( my $storage = $self->get_storage($package) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | 12 | return $storage->in_storage; | |||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | die "model does not use '$package' to persist data"; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | 9 | ▶ | sub in_cache { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_cache: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 9 | my ( $self, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | 9 | ▶ | if( my $storage = $self->get_storage($package) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 9 | return $storage->in_storage; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | die "model does not use '$package' to persist data"; | |||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 35 | ▶ | sub build { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build: called CC=8 SCAR=25.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 35 | my( $class, $params, $no_rel ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | 35 | ▶ | if ( ! ref $params ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | ▶ | return undef unless defined $params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | die "Cannot build a ". $class ." from '$params'"; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | elsif( ref $params eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 6 | return $class->new(%$params); | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | elsif( ref $params eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | die "Cannot build a ". $class ." from an Array Ref"; | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | ▶ | elsif( blessed $params && $params->isa( $class ) ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($params) and $params->isa($class)
MC/DC0%0/2
builtin::blessed($params) and $params->isa($class)
builtin::blessed($params)
$params->isa($class)
Truth table33%1/3
A and B
A= builtin::blessed($params)B= $params->isa($class)
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 0 | return $params; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | elsif( my $model = $class->_dispatch('build', $params, $no_rel ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 28 | return $model; | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | die "Cannot coerce a " . ( ref $params ) . " into a " . $class; | |||||||||||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 7 | ▶ | sub save { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 363 | 7 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 7 | $self->_dispatch('save', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 15 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 371 | 15 | my ( $self, $hashref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | 15 | foreach my $attr ( grep { $_->get_write_method } $self->_get_all_attributes ) { | |||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | 30 | my $name = $attr->name; | |||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 30 | ▶ | if ( exists $hashref->{$name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | 24 | $self->$name( $hashref->{$name} ); | |||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | 15 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | 2 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 388 | 2 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 2 | $self->_dispatch('delete', @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 34 | ▶ | sub dump { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 34 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 85 | map { $_ => $self->_dump_property( $self->$_ ) } | |||||||||||||||||||||||||||||||||||||||||
| 400 | 103 | grep { defined $self->$_ } | |||||||||||||||||||||||||||||||||||||||||
| 401 | 34 | map { $_->name } | |||||||||||||||||||||||||||||||||||||||||
| 402 | $self->_get_all_attributes | ||||||||||||||||||||||||||||||||||||||||||
| 403 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 92 | ▶ | sub _dump_property { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dump_property: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 408 | 92 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 92 | ▶ | return $value if ! ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 24 | ▶ | return [ map { $self->_dump_property($_) } @$value ] if ref $value eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | return { map { $_ => $self->_dump_property( $value->{$_} ) } keys %$value } if ref $value eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | 12 | ▶ | return $value->dump if $value->can('does') && $value->does('Model::Envoy'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value->can("does") and $value->does("Model::Envoy")
MC/DC50%1/2
$value->can("does") and $value->does("Model::Envoy")
$value->can("does")
$value->does("Model::Envoy")
Truth table66%2/3
A and B
A= $value->can("does")B= $value->does("Model::Envoy")
| |||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 5 | ▶ | if ( my $method = first { $value->can($_) } qw( stringify to_string as_string to_str ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||
| 420 | 4 | return $value->$method; | |||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | 50 | ▶ | sub _get_all_attributes { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_all_attributes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 50 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 50 | return grep { $_->does('Envoy') } $self->meta->get_all_attributes; | |||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | 56 | ▶ | sub _dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dispatch: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 433 | 56 | my ( $self, $method, @params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | 56 | ▶ | return $self->_class_dispatch($method,@params) unless ref $self; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 9 | for my $store ( qw( storage cache ) ) { | |||||||||||||||||||||||||||||||||||||||||
| 438 | 18 | ▶ | next unless $self->_storage->{$store}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | 12 | for my $package ( keys %{$self->_storage->{$store}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 441 | 15 | $self->_plugin_instance($store,$package)->$method(); | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | } | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | 47 | ▶ | sub _class_dispatch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_class_dispatch: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 47 | my ( $self, $method, @params ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | my $result = | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 6 | ▶ | first_result { $self->_get_configured_plugin_class('cache',$_)->$method( $self, @params ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 47 | keys %{$self->_plugins->{cache}}; | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 47 | ▶ | return $result if $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||
| 455 | $result = | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 44 | ▶ | first_result { $self->_get_configured_plugin_class('storage',$_)->$method( $self, @params ) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 44 | keys %{$self->_plugins->{storage}}; | |||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 42 | ▶ | if ( blessed $result && $result->can('does') && $result->does('Model::Envoy') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
builtin::blessed($result) and $result->can("does") and $result->does("Model::Envoy")
builtin::blessed($result) and $result->can("does")
MC/DC33%1/3
builtin::blessed($result) and $result->can("does") and $result->does("Model::Envoy")
builtin::blessed($result)
$result->can("does")
$result->does("Model::Envoy")
Truth table50%2/4
A and B and C
A= builtin::blessed($result)B= $result->can("does")C= $result->does("Model::Envoy")
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 33 | for my $plugin ( keys %{$self->_plugins->{cache}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 461 | 2 | $result->get_cache($plugin)->save(); | |||||||||||||||||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 42 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 81 | ▶ | sub _plugin_instance { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plugin_instance: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 81 | my ( $self, $store, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 81 | $package = $self->_get_configured_plugin_class($store,$package); | |||||||||||||||||||||||||||||||||||||||||
| 473 | 80 | my $conf = $self->_plugins->{$store}{$package}; | |||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||
| 475 | $self->_storage->{$store}{$package} = $package->new( %$conf, model => $self ) | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 80 | ▶ | unless $self->_storage->{$store}{$package}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 80 | return $self->_storage->{$store}{$package}; | |||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | 133 | ▶ | sub _get_configured_plugin_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_configured_plugin_class: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | 133 | my ( $self, $store, $package ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | 133 | $package = _resolve_namespace($package); | |||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 133 | my $conf = $self->_plugins->{$store}{$package}; | |||||||||||||||||||||||||||||||||||||||||
| 487 | 133 | ▶ | if ( ! $conf->{_configured} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | 24 | $package->configure($self,$conf); | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | 132 | return $package; | |||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | package Model::Envoy::Meta::Attribute::Trait::Envoy; | ||||||||||||||||||||||||||||||||||||||||||
| 495 | 13 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 13 | Moose::Util::meta_attribute_alias('Envoy'); | |||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 13 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | 13 | ▶ | use List::AllUtils 'any'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | 13 | has moose_class => ( | |||||||||||||||||||||||||||||||||||||||||
| 502 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 503 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 504 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | 64 | ▶ | around '_process_options' => sub { _install_types(@_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 64 | ▶ | sub _install_types { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_install_types: called CC=10 SCAR=23.4
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 64 | my ( $orig, $self, $name, $options ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 64 | ▶ | return unless grep { $_ eq __PACKAGE__ } @{$options->{traits} || []}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$options->{'traits'} || []
MC/DC0%0/1
$options->{'traits'} || []
$options->{'traits'}
Truth table50%1/2
A
A= $options->{'traits'}
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | 64 | ▶ | unless( find_type_constraint('Array_of_Hashref') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | 64 | subtype 'Array_of_HashRef', | |||||||||||||||||||||||||||||||||||||||||
| 516 | as 'ArrayRef[HashRef]'; | ||||||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||||||
| 518 | 64 | ▶ | unless ( find_type_constraint('Array_of_Object') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | 12 | subtype 'Array_of_Object', | |||||||||||||||||||||||||||||||||||||||||
| 521 | as 'ArrayRef[Object]'; | ||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||
| 523 | 64 | ▶ | if ( $options->{isa} =~ / ArrayRef \[ (.+?) \]/x ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | 9 | $options->{moose_class} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 525 | 9 | $options->{isa} = $self->_coerce_array($1); | |||||||||||||||||||||||||||||||||||||||||
| 526 | } | ||||||||||||||||||||||||||||||||||||||||||
| 527 | elsif( $options->{isa} =~ / Maybe \[ (.+?) \]/x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 31 | $options->{moose_class} = $1; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 31 | $options->{isa} = $self->_coerce_maybe($1); | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | elsif( ! find_type_constraint( $options->{isa} ) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 532 | 0 | $self->_coerce_class($options->{isa}); | |||||||||||||||||||||||||||||||||||||||||
| 533 | } | ||||||||||||||||||||||||||||||||||||||||||
| 534 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 24 | return $self->$orig($name,$options); | |||||||||||||||||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 40 | ▶ | $options->{coerce} = 1 unless $options->{moose_class} && any { $options->{moose_class} eq $_ } qw( HashRef ArrayRef ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition66%2/3
$options->{"moose_class"} and &any(sub {
$options->{"moose_class"} eq $_;
}
, "HashRef", "ArrayRef")
MC/DC50%1/2
$options->{"moose_class"} and &any(sub {
$options->{"moose_class"} eq $_;
}
, "HashRef", "ArrayRef")
$options->{"moose_class"}
&any(sub {
$options->{"moose_class"} eq $_;
}
, "HashRef", "ArrayRef")
Truth table66%2/3
A and B
A= $options->{"moose_class"}B= &any(sub {
$options->{"moose_class"} eq $_;
}
, "HashRef", "ArrayRef")
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||
| 540 | 40 | return $self->$orig($name,$options); | |||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||
| 543 | 9 | ▶ | sub _coerce_array { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_array: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 544 | 9 | my ( $self, $class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 9 | my $type = ( $class =~ / ( [^:]+ ) $ /x )[0]; | |||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | 9 | ▶ | unless( find_type_constraint("Array_of_$type") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | 7 | subtype "Array_of_$type", | |||||||||||||||||||||||||||||||||||||||||
| 551 | as "ArrayRef[$class]"; | ||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | coerce "Array_of_$type", | ||||||||||||||||||||||||||||||||||||||||||
| 554 | from "Array_of_Object", | ||||||||||||||||||||||||||||||||||||||||||
| 555 | 0 | ▶ | via { [ map { $class->build($_) } @{$_} ] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||
| 557 | from 'Array_of_HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 558 | 7 | ▶ | via { [ map { $class->new($_) } @{$_} ] }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | 9 | return "Array_of_$type"; | |||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | 31 | ▶ | sub _coerce_maybe { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_maybe: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | 31 | my ( $self, $class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 566 | 31 | ▶ | my $type = ( $class =~ /\:\:([^:]+)$/ )[0] || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
($class =~ /\:\:([^:]+)$/)[0] || $class
MC/DC0%0/2
($class =~ /\:\:([^:]+)$/)[0] || $class
($class =~ /\:\:([^:]+)$/)[0]
$class
Truth table66%2/3
A || B
A= ($class =~ /\:\:([^:]+)$/)[0]B= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||||||||||||||||
| 568 | 31 | ▶ | unless( find_type_constraint("Maybe_$type") ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | 17 | subtype "Maybe_$type", | |||||||||||||||||||||||||||||||||||||||||
| 571 | as "Maybe[$class]"; | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | coerce "Maybe_$type", | ||||||||||||||||||||||||||||||||||||||||||
| 574 | from 'Object', | ||||||||||||||||||||||||||||||||||||||||||
| 575 | 5 | ▶ | via { $class->build($_) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | from 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 578 | 0 | ▶ | via { $class->new($_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 31 | return "Maybe_$type"; | |||||||||||||||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||
| 584 | 0 | ▶ | sub _coerce_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coerce_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | my ( $self, $class ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | my $type = ( $class =~ /\:\:([^:]+)$/ )[0]; | |||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | coerce $class, | ||||||||||||||||||||||||||||||||||||||||||
| 589 | from 'Object', | ||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | ▶ | via { $class->build($_) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||
| 592 | from 'HashRef', | ||||||||||||||||||||||||||||||||||||||||||
| 593 | 0 | ▶ | via { $class->new($_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | package Moose::Meta::Attribute::Custom::Trait::Envoy; | ||||||||||||||||||||||||||||||||||||||||||
| 597 | sub register_implementation { | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 'MooseX::Meta::Attribute::Trait::Envoy' | ||||||||||||||||||||||||||||||||||||||||||
| 599 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||