File Coverage

blib/lib/Storage/Abstract/Role/Driver/Meta.pm
Criterion Covered Total %
statement 17 17 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Storage::Abstract::Role::Driver::Meta;
2             $Storage::Abstract::Role::Driver::Meta::VERSION = '0.008';
3 5     5   4086 use v5.14;
  5         20  
4 5     5   29 use warnings;
  5         7  
  5         395  
5              
6 5     5   28 use Mooish::Base -standard, -role;
  5         10  
  5         48  
7              
8 5     5   82884 use List::Util qw(all);
  5         13  
  5         3360  
9              
10             requires qw(
11             source_is_array
12             );
13              
14             my $storage_instance = (InstanceOf ['Storage::Abstract'])
15             ->plus_coercions(HashRef, q{ Storage::Abstract->new($_) });
16              
17             has param 'source' => (
18             coerce => $storage_instance | ArrayRef [$storage_instance],
19             );
20              
21             # empty BUILD in case there is none in the class
22             sub BUILD
23       2 0   {
24             }
25              
26             # make sure this runs even with custom BUILD in the class
27             after BUILD => sub {
28             my ($self) = @_;
29              
30             if ($self->source_is_array) {
31             die 'Source of ' . (ref $self) . ' must be an array'
32             unless ref $self->source eq 'ARRAY';
33             }
34             else {
35             die 'Source of ' . (ref $self) . ' must not be an array'
36             unless ref $self->source ne 'ARRAY';
37             }
38             };
39              
40             sub _build_readonly
41             {
42 6     6   540 my ($self) = @_;
43              
44 6 100       26 if ($self->source_is_array) {
45 2     3   13 return all { $_->readonly } @{$self->source};
  3         143  
  2         87  
46             }
47             else {
48 4         84 return $self->source->readonly;
49             }
50             }
51              
52             around 'set_readonly' => sub {
53             my ($orig, $self, $new_value) = @_;
54              
55             if ($self->source_is_array) {
56             die 'Driver of class ' . (ref $self) . ' holds multiple sources and cannot set_readonly';
57             }
58             else {
59             $self->source->set_readonly($new_value);
60             return $self->$orig($new_value);
61             }
62             };
63              
64             after 'refresh' => sub {
65             my ($self) = @_;
66              
67             # readonly is cached in metadrivers
68             $self->_clear_readonly;
69              
70             if ($self->source_is_array) {
71             foreach my $source (@{$self->source}) {
72             $source->refresh;
73             }
74             }
75             else {
76             $self->source->refresh;
77             }
78             };
79              
80             1;
81