PopulateMore/Visitor.pm

filter: Statementstmt 94.0% 47 / 50 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod 100% 4 / 4 totaltotal 90.0% 72 / 80 CC 8 SCAR 9.5
CC
8
Cov
91%
visit
5.0
match_or_not
3.1
__ANON__
2.0
CRAP 2.6
« PopulateMore/Test/Schema.pm ↑ Summary PopulateMore/Inflator/Index.pm »
1 package DBIx::Class::Schema::PopulateMore::Visitor;
2  
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Scalar::Util qw/refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Type::Library -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 DBIx::Class::Schema::PopulateMore::Visitor - Visitor for the Populate Data
12  
13 =head1 SYNOPSIS
14  
15 ##Example Usage
16  
17 See Tests for more example usage.
18  
19 =head1 DESCRIPTION
20  
21 When populating a table, sometimes we need to inflate values that we won't
22 know of in advance. For example we might have a column that is FK to another
23 column in another table. We want to make it easy to 'tag' a value as something
24 other than a real value to be inserted into the table.
25  
26 Right now we only have one substitution to do, which is the FK one mentioned
27 above, but we might eventually create other substitution types so we've broken
28 this out to make it neat and easy to do so.
29  
30 =head1 ATTRIBUTES
31  
32 This class defines the following attributes.
33  
34 =head2 update_callback
35  
36 The coderef to be execute should the match condition succeed
37  
38 =cut
39  
40 has 'update_callback' => (
41 is=>'rw',
42 required=>1,
43 lazy=>1,
44 isa=>CodeRef,
45 default=> sub {
46 return sub {
47 0 ▶ return shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 },
50 3 );
51  
52 =head2 match_condition
53  
54 How we know the value is really something to inflate or perform a substitution
55 on. This get's the namespace of the substitution plugin and it's other data.
56  
57 The default behavior (where there is no substitution namespace, is to do the
58 inflate to resultset. This is the most common usecase.
59  
60 =cut
61  
62 3 has 'match_condition' => (
63 is=>'ro',
64 required=>1,
65 isa=>RegexpRef,
66 );
67  
68 =head2 seen
69  
70 Used to collect up ref addresses of arrays/hashes we have already seen
71  
72 =cut
73  
74 has seen => (
75 is => 'rw',
76 isa => HashRef,
77 5 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 3 );
79  
80 =head1 METHODS
81  
82 This module defines the following methods.
83  
84 =head2 callback
85  
86 Given a coderef, sets the current callback and returns self so that we can chain
87  
88 =cut
89  
90 sub callback
91 {
92 16 ▶ my $self = shift @_;
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
93 16 $self->update_callback(shift @_);
94 16 return $self;
95 }
96  
97 =head2 visit
98  
99 A simple visitor that only expects to perform replacements on values
100  
101 =cut
102  
103 sub visit
104 {
105 119 ▶ my ( $self, $target ) = @_;
Subroutine visit: called CC=5 SCAR=16.1
Pod documented
106 119 ▶ if ( ref $target eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $target eq 'ARRAY') { } 25 94 119
elsif (ref $target eq 'HASH') { } 30 64 94
107 25 my $addr = refaddr $target;
108 25 ▶ return $self->seen->{$addr} if defined $self->seen->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->seen->{$addr} 0 25 25
109 25 my $new_array = $self->seen->{$addr} = [];
110 25 @$new_array = map { $self->visit($_) } @$target;
111 25 return $new_array;
112 }
113 elsif ( ref $target eq 'HASH' ) {
114 30 my $addr = refaddr $target;
115 30 ▶ return $self->seen->{$addr} if defined $self->seen->{$addr};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->seen->{$addr} 0 30 30
116 30 my $new_hash = $self->seen->{$addr} = {};
117 30 %$new_hash = map { $_ => $self->visit( $target->{$_} ) } keys %$target;
118 30 return $new_hash;
119 }
120 else {
121 64 $self->visit_value($target);
122 }
123 }
124  
125 =head2 visit_value
126  
127 Here is where we make the choice as to if this value needs to be inflated via a plugin
128  
129 =cut
130  
131 sub visit_value
132 {
133 64 ▶ my ($self, $data) = @_;
Subroutine visit_value: called CC=2 SCAR=6.9
Pod documented
134
135 64 ▶ if(my $item = $self->match_or_not($data))
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = $self->match_or_not($data)) 33 31 64
136 {
137 33 return $self->update_callback->($item);
138 }
139  
140 31 return $data;
141 }
142  
143  
144 =head2 match_or_not
145  
146 We break this out to handle the ugliness surrounding dealing with undef values
147 and also to make it easier on subclassers.
148  
149 =cut
150
151 sub match_or_not
152 {
153 64 ▶ my ($self, $data) = @_;
Subroutine match_or_not: called CC=3 SCAR=11.2
Pod documented
154 64 my $match_condition = $self->match_condition;
155
156 64 ▶ if( !defined $data )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $data) { } 0 64 64
elsif (my($item) = $data =~ /$match_condition/) { } 33 31 64
157 {
158 0 return;
159 }
160 elsif(my ($item) = ($data=~m/$match_condition/))
161 {
162 33 return $item;
163 }
164
165 31 return;
166 }
167  
168  
169 =head1 AUTHOR
170  
171 Please see L<DBIx::Class::Schema::PopulateMore> For authorship information
172  
173 visit method culled from code in L<Data::Visitor::Lite> which is copyright 2011 Daichi Hiroki <hirokidaichi {at} gmail.com>
174  
175 =head1 LICENSE
176  
177 Please see L<DBIx::Class::Schema::PopulateMore> For licensing terms.
178  
179 =cut
180  
181  
182 3 1;
« PopulateMore/Test/Schema.pm ↑ Summary PopulateMore/Inflator/Index.pm »