EntityModel/Gather.pm

filter: Statementstmt 9.6% 6 / 62 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 8.3% 1 / 12 Podpod 28.5% 2 / 7 totaltotal 8.0% 9 / 112 CC 15 SCAR 25.9
CC
15
Cov
7%
add_pending
30.0
mark_ready
20.0
new
12.0
CRAP 13.3
« EntityModel/Query/Field.pm ↑ Summary EntityModel/DB.pm »
1 package EntityModel::Gather;
2 {
3 2 $EntityModel::Gather::VERSION = '0.102';
4 }
5 2 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 };
7  
8 =head1 NAME
9  
10 EntityModel::Gather - asynchronous helper functions for L<EntityModel>
11  
12 =head1 VERSION
13  
14 version 0.102
15  
16 =head1 SYNOPSIS
17  
18 use EntityModel::Gather;
19  
20 =head1 DESCRIPTION
21  
22 Wrapper class for executing code once data values have been populated.
23  
24 Given a set of data key => value pairs, this module will request population from each provider.
25 Once all data values are available, the on_ready event is triggered, and the data values are
26 passed to the provided handler. If it is impossible to retrieve data values for some reason,
27 the on_error handler is called instead.
28  
29 Each of the values passed must be one of the following:
30  
31 =over 4
32  
33 =item * Subclass of L<EntityModel::Pending> - requests callback when data value has been populated
34  
35 =item * Subclass of L<EntityModel::EntityBase> - requests callback when populated
36  
37 =item * Scalar value - this will be used as-is
38  
39 =item * undef - this will be used as-is
40  
41 =back
42  
43 Any other value will cause the scalar handling to be used.
44  
45 =head1 METHODS
46  
47 =cut
48  
49 =head2 new
50  
51 Create a new instance. Takes a list of key, value pairs indicating which data values to wait for.
52  
53 =cut
54  
55 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
56 0 my $class = shift;
57 0 my $self = bless {
58 pending => { },
59 ready => { },
60 }, $class;
61  
62 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
63 0 my ($k, $v) = splice @_, 0, 2;
64 0 $self->add_pending($k => $v);
65 }
66 0 ▶ $self->dispatch('ready') if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 0 0 0
67 0 return $self;
68 }
69  
70 =head2 add_pending
71  
72 Adds the given key and value to the pending list. If the value is immediately available (simple scalar, for example) then this
73 will pass the value through immediately.
74  
75 =cut
76  
77 0 ▶sub add_pending {
Subroutine add_pending: not called CC=5 SCAR=34.0
Pod documented
78 0 my $self = shift;
79 0 my ($k, $v) = @_;
80 0 ▶ die "Undefined key" unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
81 0 ▶ die "Reference given where string expected: $k" if ref $k;
Branch: 1 branch
BranchTrueFalseTotal
if ref $k 0 0 0
82  
83 0 ▶ if(eval { $v->isa('EntityModel::Deferred') }) {
Branch: 2 branches
BranchTrueFalseTotal
if (eval { do { $v->isa('EntityModel::Deferred') } }) { } 0 0 0
elsif (eval { do { $v->isa('EntityModel::Support::Perl::Base') } }) { } 0 0 0
84 0 $self->{pending}->{$k} = $v;
85 0 ▶ $v->queue_callback('ready', $self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 my $self = shift;
87 0 $self->mark_ready($k);
88 0 ▶ }), 'error', $self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 my $self = shift;
90 0 $self->raise_error($k);
91 0 }));
92 0 } elsif(eval { $v->isa('EntityModel::Support::Perl::Base') }) {
93 0 $self->{pending}->{$k} = $v;
94 $v->_request_load(
95 0 ▶ on_complete => $self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 my $self = shift;
97 0 $self->mark_ready($k);
98 }),
99 0 ▶ on_error => $self->sap(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 my $self = shift;
101 0 $self->raise_error($k);
102 })
103 0 );
104 } else {
105 0 $self->{pending}->{$k} = $v;
106 0 $self->mark_ready($k);
107 };
108 0 return $self;
109 }
110  
111 0 ▶sub mark_ready {
Subroutine mark_ready: not called CC=4 SCAR=30.0
Pod undocumented
112 0 my $self = shift;
113 0 my $k = shift;
114 0 ▶ die "Tried to mark non-existent key $k as ready" unless exists $self->{pending}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"pending"}{$k} 0 0 0
115 0 ▶ die "Key $k exists already" if exists $self->{ready}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"ready"}{$k} 0 0 0
116  
117 0 my $v = delete $self->{pending}->{$k};
118 0 $self->{ready}->{$k} = $self->extract_value($v);
119 0 ▶ $self->dispatch('ready') if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 0 0 0
120 0 return $self;
121 }
122  
123 0 ▶sub extract_value {
Subroutine extract_value: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my $self = shift;
125 0 my $v = shift;
126 0 ▶ return $v->value if eval { $v->isa('EntityModel::Deferred'); };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $v->isa("EntityModel::Deferred") } } 0 0 0
127 0 return $v;
128 }
129  
130 0 ▶sub is_ready {
Subroutine is_ready: not called CC=2 SCAR=17.9
Pod undocumented
131 0 my $self = shift;
132 0 ▶ return keys %{$self->{pending}} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$self{'pending'};} ? : 0 0 0
133 }
134  
135 0 ▶sub when_ready {
Subroutine when_ready: not called CC=2 SCAR=17.9
Pod undocumented
136 0 my $self = shift;
137 0 $self->{on_ready} = shift;
138 0 ▶ $self->dispatch('ready') if $self->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ready 0 0 0
139 0 return $self;
140 }
141  
142 0 ▶sub dispatch {
Subroutine dispatch: not called CC=3 SCAR=24.8
Pod undocumented
143 0 my $self = shift;
144 0 my $evt = shift;
145 0 my $method = 'on_' . $evt;
146 0 ▶ my $code = $self->{$method} || $self->can($method);
Condition0%0/3
$self->{$method} || $self->can($method)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$method} || $self->can($method)
$self->{$method} $self->can($method)
Truth table0%0/3
A || B
A= $self->{$method}B= $self->can($method)
ABresult
1X1
011
000
147 0 ▶ $code->(%{$self->{ready}}) if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
148 0 return $self;
149 }
150  
151 2 1;
152  
153 __END__
« EntityModel/Query/Field.pm ↑ Summary EntityModel/DB.pm »