File Coverage

blib/lib/EntityModel/Class/Accessor/Array.pm
Criterion Covered Total %
statement 39 52 75.0
branch 5 16 31.2
condition 2 4 50.0
subroutine 10 11 90.9
pod 2 2 100.0
total 58 85 68.2


line stmt bran cond sub pod time code
1             package EntityModel::Class::Accessor::Array;
2             $EntityModel::Class::Accessor::Array::VERSION = '0.016';
3 1     1   6 use strict;
  1         2  
  1         37  
4 1     1   6 use warnings;
  1         2  
  1         25  
5              
6 1     1   4 use parent qw{EntityModel::Class::Accessor};
  1         2  
  1         7  
7              
8 1     1   65 use EntityModel::Array;
  1         3  
  1         45  
9 1     1   7 use EntityModel::Log ':all';
  1         2  
  1         254  
10 1     1   1060 use Class::ISA;
  1         3329  
  1         732  
11              
12             my %watcher;
13              
14             =head1 NAME
15              
16             EntityModel::Class::Accessor::Array - generic class accessor for arrays
17              
18             =head1 VERSION
19              
20             Version 0.016
21              
22             =head1 DESCRIPTION
23              
24             See L.
25              
26             =head1 METHODS
27              
28             =cut
29              
30             =head2 method_list
31              
32             Returns the method definition to add to the class.
33              
34             =cut
35              
36             sub method_list {
37 1     1 1 6 my ($class, %opt) = @_;
38 1         2 my $k = $opt{k};
39 1 50       5 if(my $pre = $opt{pre}) {
40             return sub {
41 0     0   0 my $self = shift;
42 0 0       0 die "Options not supported for Array" if @_;
43 0 0       0 $opt{pre}->($self, @_) or return;
44 0 0       0 unless($self->{$k}) {
45 0   0     0 my @watchers = map { @{ $watcher{$_}->{$k} // [] } } Class::ISA::self_and_super_path(ref $self);
  0         0  
  0         0  
46 0         0 logDebug("Watcher for [%s] method [%s] has %d entries", ref $self, $k, scalar @watchers);
47             $self->{$k} = EntityModel::Array->new($self->{$k},
48             (@watchers)
49             ? (onchange => [ sub {
50 0         0 logDebug("Check [%s] for [%s]", ref $self, $k);
51             # Pass value only
52 0         0 $_->($self, @_) foreach @watchers;
53 0 0       0 } ]) : ()
54             );
55             }
56 0         0 return $self->{$k};
57 0         0 };
58             } else {
59             return sub {
60 7     7   17 my $self = shift;
61 7 50       19 die "Options not supported for Array" if @_;
62 7 100       105 unless($self->{$k}) {
63 2   100     16 my @watchers = map {
64 1         7 @{ $watcher{$_}->{$k} // [] }
  2         44  
65             } Class::ISA::self_and_super_path(ref $self);
66              
67 1         8 logDebug("Watcher for [%s] method [%s] has %d entries", ref $self, $k, scalar @watchers);
68             $self->{$k} = EntityModel::Array->new($self->{$k},
69             (@watchers)
70             ? (onchange => [ $self->sap(sub {
71 4     4   7 my $self = shift;
72 4         19 logDebug("Check [%s] for [%s]", ref $self, $k);
73             # Pass value only
74 4         163 $_->($self, @_) foreach @watchers;
75 1 50       77 }) ]) : ()
76             );
77             }
78 7         41 return $self->{$k};
79 1         13 };
80             }
81             }
82              
83             =head2 add_watcher
84              
85             Add this to the list of watchers for the class.
86              
87             =cut
88              
89             sub add_watcher {
90 1     1 1 3 my ($class, $pkg, $meth, @sub) = @_;
91 1         6 logDebug("Watching [%s] for [%s]", $meth, $pkg);
92 1         66 push @{$watcher{$pkg}->{$meth}}, @sub;
  1         13  
93 1         6 return 1;
94             }
95              
96             1;
97              
98             __END__