Model/TypeMap/Base.pm

filter: Statementstmt 37.5% 74 / 197 Branchbran 7.5% 5 / 66 Conditioncond 1.6% 1 / 61 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 56.7% 21 / 37 Podpod 84.2% 16 / 19 totaltotal 28.0% 117 / 417 CC 63 SCAR 48.3
CC
63
Cov
25%
attribute_mapping
506.0
find_mapper
42.0
find
42.0
CRAP 124.6
« Model/Index.pm ↑ Summary Model/Meta/Class/Model.pm »
1 package Elastic::Model::TypeMap::Base;
2 23 $Elastic::Model::TypeMap::Base::VERSION = '0.52';
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Sub::Exporter qw(build_exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Class::MOP();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Class::Load();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Moose::Util qw(does_role);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Moose::Util::TypeConstraints qw(find_type_constraint);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 609 ▶sub deflate_via (&) { deflator => $_[0] }
Subroutine deflate_via: called CC=1 SCAR=0.0
Pod undocumented
16 609 ▶sub inflate_via (&) { inflator => $_[0] }
Subroutine inflate_via: called CC=1 SCAR=0.0
Pod undocumented
17 666 ▶sub map_via (&) { mapper => $_[0] }
Subroutine map_via: called CC=1 SCAR=0.0
Pod undocumented
18  
19 #===================================
20 149 ▶sub import {
Subroutine import: called CC=5 SCAR=16.2
21 #===================================
22 149 my ( $class, @args ) = @_;
23 149 my $callee = caller;
24 149 ▶ return if $callee->isa(__PACKAGE__) || @args == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $callee->isa(__PACKAGE__) or @args == 0 0 149 149
Condition33%1/3
$callee->isa(__PACKAGE__) or @args == 0
l!l&&r!l&&!r
00149
MC/DC0%0/2
$callee->isa(__PACKAGE__) or @args == 0
$callee->isa(__PACKAGE__) @args == 0
Truth table33%1/3
A or B
A= $callee->isa(__PACKAGE__)B= @args == 0
ABresult
1X1
011
000
25  
26 {
27 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 149 unshift @{ $callee . '::ISA' }, __PACKAGE__;
29 }
30  
31 build_exporter(
32 { into => $callee,
33 exports => [
34 qw(deflate_via inflate_via map_via),
35 149 ▶ has_type => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 758 ▶ sub { $callee->_has_type(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 149 },
38 149 ]
39 }
40 )->( $class, ':all' );
41  
42 149 for (@args) {
43 252 ▶ next if /^[:-]/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[:-]/ 126 126 252
44 126 Class::Load::load_class($_);
45 126 $callee->import_types( $_->typemap );
46 }
47 }
48  
49 #===================================
50 0 ▶sub find_deflator {
Subroutine find_deflator: not called CC=2 SCAR=17.9
Pod documented
51 #===================================
52 0 my ( $map, $attr ) = @_;
53 0 my $deflator = $map->find_raw_deflator($attr);
54 0 ▶ return $deflator if ref $deflator;
Branch: 1 branch
BranchTrueFalseTotal
if ref $deflator 0 0 0
55 0 Eval::Closure::eval_closure(
56 source => [ 'sub { my $val = $_[0];', $deflator, '}' ] );
57 }
58  
59 #===================================
60 0 ▶sub find_raw_deflator {
Subroutine find_raw_deflator: not called CC=4 SCAR=30.0
Pod documented
61 #===================================
62 0 my ( $map, $attr ) = @_;
63 $attr->can('deflator') && $attr->deflator
64 0 ▶ || eval { $map->find( 'deflator', $attr->type_constraint, $attr ) }
Branch: 1 branch
BranchTrueFalseTotal
$attr->can("deflator") and $attr->deflator or eval { do { $map->find("deflator", $attr->type_constraint, $attr) } } or die _type_error("deflator", $attr, $@) 0 0 0
Condition0%0/6
$attr->can("deflator") and $attr->deflator or eval { do { $map->find("deflator", $attr->type_constraint, $attr) } }
l!l&&r!l&&!r
000
$attr->can("deflator") and $attr->deflator
!ll&&!rl&&r
000
MC/DC0%0/3
$attr->can("deflator") and $attr->deflator or eval { do { $map->find("deflator", $attr->type_constraint, $attr) } }
$attr->can("deflator") $attr->deflator eval { do { $map->find("deflator", $attr->type_constraint, $attr) } }
Truth table0%0/5
A and B or C
A= $attr->can("deflator")B= $attr->deflatorC= eval { do { $map->find("deflator", $attr->type_constraint, $attr) } }
ABCresult
11X1
0X11
1011
0X00
1000
65 || die _type_error( 'deflator', $attr, $@ );
66 }
67  
68 #===================================
69 0 ▶sub find_inflator {
Subroutine find_inflator: not called CC=2 SCAR=17.9
Pod documented
70 #===================================
71 0 my ( $map, $attr ) = @_;
72 0 my $inflator = $map->find_raw_inflator($attr);
73 0 ▶ return $inflator if ref $inflator;
Branch: 1 branch
BranchTrueFalseTotal
if ref $inflator 0 0 0
74 0 Eval::Closure::eval_closure(
75 source => [ 'sub { my $val = $_[0];', $inflator, '}' ] );
76 }
77  
78 #===================================
79 0 ▶sub find_raw_inflator {
Subroutine find_raw_inflator: not called CC=4 SCAR=30.0
Pod documented
80 #===================================
81 0 my ( $map, $attr ) = @_;
82 $attr->can('inflator') && $attr->inflator
83 0 ▶ || eval { $map->find( 'inflator', $attr->type_constraint, $attr ) }
Branch: 1 branch
BranchTrueFalseTotal
$attr->can("inflator") and $attr->inflator or eval { do { $map->find("inflator", $attr->type_constraint, $attr) } } or die _type_error("inflator", $attr, $@) 0 0 0
Condition0%0/6
$attr->can("inflator") and $attr->inflator or eval { do { $map->find("inflator", $attr->type_constraint, $attr) } }
l!l&&r!l&&!r
000
$attr->can("inflator") and $attr->inflator
!ll&&!rl&&r
000
MC/DC0%0/3
$attr->can("inflator") and $attr->inflator or eval { do { $map->find("inflator", $attr->type_constraint, $attr) } }
$attr->can("inflator") $attr->inflator eval { do { $map->find("inflator", $attr->type_constraint, $attr) } }
Truth table0%0/5
A and B or C
A= $attr->can("inflator")B= $attr->inflatorC= eval { do { $map->find("inflator", $attr->type_constraint, $attr) } }
ABCresult
11X1
0X11
1011
0X00
1000
84 || die _type_error( 'inflator', $attr, $@ );
85 }
86  
87 #===================================
88 0 ▶sub find_mapper {
Subroutine find_mapper: not called CC=6 SCAR=37.4
Pod documented
89 #===================================
90 0 my ( $map, $attr ) = @_;
91 0 my $mapping;
92 0 ▶ $mapping = $attr->mapping if $attr->can('mapping');
Branch: 1 branch
BranchTrueFalseTotal
if $attr->can("mapping") 0 0 0
93 0 ▶ return %$mapping if $mapping && %$mapping;
Branch: 1 branch
BranchTrueFalseTotal
if $mapping and %$mapping 0 0 0
Condition0%0/3
$mapping and %$mapping
!ll&&!rl&&r
000
MC/DC0%0/2
$mapping and %$mapping
$mapping %$mapping
Truth table0%0/3
A and B
A= $mappingB= %$mapping
ABresult
0X0
100
111
94  
95 0 my %mapping;
96 0 eval {
97 0 my @mapping = $map->find( 'mapper', $attr->type_constraint, $attr );
98 0 ▶ die "Expected an even number of elements "
Branch: 1 branch
BranchTrueFalseTotal
if @mapping % 2 0 0 0
99 . "to be returned by the mapper.\n"
100 if @mapping % 2;
101 0 %mapping = @mapping;
102 };
103  
104 0 ▶ die _type_error( 'mapper', $_[1], $@ )
Branch: 1 branch
BranchTrueFalseTotal
unless %mapping 0 0 0
105 unless %mapping;
106  
107 0 return %mapping;
108 }
109  
110 #===================================
111 0 ▶sub _type_error {
Subroutine _type_error: not called CC=2 SCAR=17.9
112 #===================================
113 0 my ( $type, $attr, $error ) = @_;
114 0 my $name = $attr->name;
115 0 my $class = $attr->associated_class->name;
116 0 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
117 ? "Error finding $type for attribute ($name) in class $class:\n$error"
118 : "No $type found for attribute ($name) in class $class";
119 }
120  
121 #===================================
122 0 ▶sub find {
Subroutine find: not called CC=6 SCAR=37.4
Pod documented
123 #===================================
124 0 my ( $map, $type, $tc, $attr ) = @_;
125  
126 0 ▶ $tc ||= find_type_constraint('Any');
Condition0%0/3
$tc ||= find_type_constraint("Any")
l!l&&r!l&&!r
000
MC/DC0%0/2
$tc ||= find_type_constraint("Any")
$tc find_type_constraint("Any")
Truth table0%0/3
A ||= B
A= $tcB= find_type_constraint("Any")
ABresult
1X1
011
000
127  
128 0 my $types = $type . 's';
129 0 my $handlers = $map->$types;
130 0 my $name = $tc->name;
131  
132 0 ▶ if ( my $handler = $handlers->{$name} || $handlers->{ ref $tc } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $handlers->{$name} || $$handlers{ref $tc}) 0 0 0
Condition0%0/3
$handlers->{$name} || $$handlers{ref $tc}
l!l&&r!l&&!r
000
MC/DC0%0/2
$handlers->{$name} || $$handlers{ref $tc}
$handlers->{$name} $$handlers{ref $tc}
Truth table0%0/3
A || B
A= $handlers->{$name}B= $$handlers{ref $tc}
ABresult
1X1
011
000
133 0 return $handler->( $tc, $attr, $map );
134 }
135  
136 0 ▶ my $parent = $tc->can('parent') && $tc->parent or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $tc->can("parent") && $tc->parent or return 0 0 0
Condition0%0/3
$tc->can('parent') && $tc->parent
!ll&&!rl&&r
000
MC/DC0%0/2
$tc->can('parent') && $tc->parent
$tc->can('parent') $tc->parent
Truth table0%0/3
A && B
A= $tc->can('parent')B= $tc->parent
ABresult
0X0
100
111
137 0 $map->find( $type, $parent, $attr );
138 }
139  
140 #===================================
141 0 ▶sub class_deflator {
Subroutine class_deflator: not called CC=6 SCAR=37.4
Pod documented
142 #===================================
143 0 my ( $map, $class, $attrs ) = @_;
144  
145 0 ▶ $attrs ||= $map->indexable_attrs($class);
Condition0%0/3
$attrs ||= $map->indexable_attrs($class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs ||= $map->indexable_attrs($class)
$attrs $map->indexable_attrs($class)
Truth table0%0/3
A ||= B
A= $attrsB= $map->indexable_attrs($class)
ABresult
1X1
011
000
146  
147 0 my %deflators = map { $_ => $map->find_raw_deflator( $attrs->{$_} ) }
148 keys %$attrs;
149  
150 0 my @src = ( 'sub {', 'my $self = shift;', 'my (%hash,$attr,$val);',
151 'eval {', );
152  
153 0 my %readers;
154 0 for my $name ( sort keys %deflators ) {
155 0 my $attr = $attrs->{$name};
156 0 my $deflator = $deflators{$name};
157 0 ▶ if ( ref $deflator ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $deflator) 0 0 0
158 0 $deflator = '$deflators{"' . $name . '"}->($val)';
159 }
160 0 my $is_lazy = $attr->is_lazy;
161 0 my $get_raw = $attr->_inline_instance_get('$self');
162 0 my $exists = $attr->_inline_instance_has('$self');
163  
164 0 push @src, '$attr="' . $name . '";';
165 0 ▶ if ($is_lazy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_lazy) { } 0 0 0
166 0 $readers{$name} = $attr->get_read_method_ref;
167 0 push @src, '$readers{"' . $name . '"}->($self);';
168 }
169 else {
170 0 push @src, "if ($exists) {";
171 }
172 0 push @src, '$val = ' . $get_raw . ';',
173 '$hash{"' . $name . '"} = ' . $deflator . ';';
174 0 ▶ push @src, '}' unless $is_lazy;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_lazy 0 0 0
175 }
176  
177 0 push @src,
178 (
179 '1;} ',
180 'or die "Error deflating attribute ($attr) in class ".',
181 'Scalar::Util::blessed($self).',
182 '":\n ".($@ || "Unknown error");',
183 'return \%hash',
184 '}'
185 );
186  
187 0 Eval::Closure::eval_closure(
188 source => \@src,
189 environment => {
190 '%deflators' => \%deflators,
191 '%readers' => \%readers
192 }
193 );
194 }
195  
196 #===================================
197 0 ▶sub class_inflator {
Subroutine class_inflator: not called CC=4 SCAR=30.0
Pod documented
198 #===================================
199 0 my ( $map, $class, $attrs ) = @_;
200  
201 0 ▶ $attrs ||= $map->indexable_attrs($class);
Condition0%0/3
$attrs ||= $map->indexable_attrs($class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs ||= $map->indexable_attrs($class)
$attrs $map->indexable_attrs($class)
Truth table0%0/3
A ||= B
A= $attrsB= $map->indexable_attrs($class)
ABresult
1X1
011
000
202 0 my %inflators = map { $_ => $map->find_raw_inflator( $attrs->{$_} ) }
203 keys %$attrs;
204  
205 0 my @src = (
206 'sub {',
207 'my ($self,$hash) = @_;',
208 'my ($attr,$val,$res);',
209 'eval {'
210 );
211 0 for my $name ( sort keys %inflators ) {
212 0 my $attr = $attrs->{$name};
213 0 my $inflator = $inflators{$name};
214 0 my $set_raw = $attr->_inline_instance_set( '$self', '$res' );
215 0 ▶ if ( ref $inflator ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inflator) 0 0 0
216 0 $inflator = '$inflators{"' . $name . '"}->($val)';
217 }
218 0 push @src,
219 (
220 '$attr = "' . $name . '";',
221 'if (exists $hash->{"' . $name . '"}) {',
222 '$val = $hash->{"' . $name . '"};',
223 '$res = ' . $inflator . ';',
224 $set_raw . ';',
225 $attr->_inline_weaken_value( '$self', '$res' ),
226 '}'
227 );
228 }
229 0 push @src,
230 (
231 '1}',
232 'or die "Error inflating attribute ($attr) in class ".',
233 'Scalar::Util::blessed($self).',
234 '":\n ".($@ || "Unknown error");',
235 'return $self',
236 '}'
237 );
238  
239 0 Eval::Closure::eval_closure(
240 source => \@src,
241 environment => { '%inflators' => \%inflators, }
242 );
243 }
244  
245 23 our %Allowed_Attrs = (
246 string => {
247 'index_name' => 1,
248 'store' => 1,
249 'index' => 1,
250 'term_vector' => 1,
251 'boost' => 1,
252 'null_value' => 1,
253 'analyzer' => 1,
254 'index_analyzer' => 1,
255 'search_analyzer' => 1,
256 'search_quote_analyzer' => 1,
257 'include_in_all' => 1,
258 'multi' => 1,
259 },
260 integer => {
261 'index_name' => 1,
262 'store' => 1,
263 'index' => 1,
264 'precision_step' => 1,
265 'boost' => 1,
266 'null_value' => 1,
267 'include_in_all' => 1,
268 'multi' => 1,
269 },
270 date => {
271 'index_name' => 1,
272 'format' => 1,
273 'store' => 1,
274 'index' => 1,
275 'precision_step' => 1,
276 'boost' => 1,
277 'null_value' => 1,
278 'include_in_all' => 1,
279 'multi' => 1,
280 },
281 boolean => {
282 'index_name' => 1,
283 'store' => 1,
284 'index' => 1,
285 'boost' => 1,
286 'null_value' => 1,
287 'include_in_all' => 1,
288 'multi' => 1,
289 },
290 binary => {
291 'index_name' => 1,
292 'store' => 1
293 },
294 object => {
295 'dynamic' => 1,
296 'enabled' => 1,
297 'path' => 1,
298 'include_in_all' => 1,
299 },
300 nested => {
301 'include_in_parent' => 1,
302 'include_in_root' => 1,
303 'include_in_all' => 1,
304 'dynamic' => 1,
305 'path' => 1,
306 },
307 ip => {
308 'index_name' => 1,
309 'store' => 1,
310 'index' => 1,
311 'precision_step' => 1,
312 'boost' => 1,
313 'null_value' => 1,
314 'include_in_all' => 1,
315 'multi' => 1,
316 },
317 geo_point => {
318 'lat_lon' => 1,
319 'geohash' => 1,
320 'geohash_precision' => 1,
321 'index_name' => 1,
322 'store' => 1,
323 'multi' => 1,
324 },
325  
326 # TODO: attachment => {}
327 );
328  
329 23 our @All_Keys = uniq map { keys %$_ } values %Allowed_Attrs;
330  
331 $Allowed_Attrs{$_} = $Allowed_Attrs{integer}
332 23 for qw(long float double short byte);
333  
334 #===================================
335 0 ▶sub class_mapping {
Subroutine class_mapping: not called CC=3 SCAR=24.8
Pod documented
336 #===================================
337 0 my ( $map, $class, $attrs ) = @_;
338  
339 0 ▶ $attrs ||= $map->indexable_attrs($class);
Condition0%0/3
$attrs ||= $map->indexable_attrs($class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$attrs ||= $map->indexable_attrs($class)
$attrs $map->indexable_attrs($class)
Truth table0%0/3
A ||= B
A= $attrsB= $map->indexable_attrs($class)
ABresult
1X1
011
000
340  
341 0 my %props = map { $_ => $map->attribute_mapping( $attrs->{$_} ) }
342 keys %$attrs;
343  
344 0 ▶ return ( type => 'object', enabled => 0 )
Branch: 1 branch
BranchTrueFalseTotal
unless %props 0 0 0
345 unless %props;
346  
347 return (
348 0 type => 'object',
349 dynamic => 'strict',
350 properties => \%props
351 );
352 }
353  
354 #===================================
355 0 ▶sub attribute_mapping {
Subroutine attribute_mapping: not called CC=22 SCAR=62.3
Pod documented
356 #===================================
357 0 my ( $map, $attr ) = @_;
358  
359 0 ▶ my $mapping = $attr->can('mapping') && $attr->mapping
Condition0%0/5
$attr->can('mapping') && $attr->mapping || {$map->find_mapper($attr)}
l!l
00
$attr->can("mapping") and $attr->mapping
!ll&&!rl&&r
000
MC/DC0%0/2
$attr->can('mapping') && $attr->mapping || {$map->find_mapper($attr)}
$attr->can("mapping") $attr->mapping
Truth table0%0/3
A and B
A= $attr->can("mapping")B= $attr->mapping
ABresult
111
0X0
100
360 || { $map->find_mapper($attr) };
361  
362 0 ▶ my $type = $mapping->{type} || '';
Condition0%0/2
$mapping->{'type'} || ''
l!l
00
MC/DC0%0/1
$mapping->{'type'} || ''
$mapping->{'type'}
Truth table0%0/2
A
A= $mapping->{'type'}
Aresult
11
00
363 0 ▶ if ( $attr->can('type') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->can("type")) 0 0 0
364 0 my $new_type = $attr->type;
365 0 ▶ if ( $new_type and $new_type ne $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_type and $new_type ne $type) 0 0 0
Condition0%0/3
$new_type and $new_type ne $type
!ll&&!rl&&r
000
MC/DC0%0/2
$new_type and $new_type ne $type
$new_type $new_type ne $type
Truth table0%0/3
A and B
A= $new_typeB= $new_type ne $type
ABresult
0X0
100
111
366 delete $mapping->{enabled}
367 0 ▶ if $type eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "object" 0 0 0
368 0 $mapping->{type} = $type = $new_type;
369 }
370 }
371  
372 0 ▶ die "Missing field type" unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
373  
374 0 ▶ my $allowed = $Allowed_Attrs{$type} or die "Unknown field type ($type)";
Branch: 1 branch
BranchTrueFalseTotal
my $allowed = $Elastic::Model::TypeMap::Base::Allowed_Attrs{$type} or die "Unknown field type ($type)" 0 0 0
375  
376 0 ▶ return $mapping unless does_role( $attr, 'Elastic::Model::Trait::Field' );
Branch: 1 branch
BranchTrueFalseTotal
unless does_role($attr, "Elastic::Model::Trait::Field") 0 0 0
377  
378 0 for my $key (@All_Keys) {
379 0 my $val = $attr->$key;
380 0 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
381 die "Attribute has type '$type', which doesn't "
382 . "understand '$key'\n"
383 0 ▶ unless $allowed->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed->{$key} 0 0 0
384 0 $mapping->{$key} = $val;
385 }
386  
387 0 $mapping = $map->_fixup_mapping($mapping);
388  
389 my $multi = delete $mapping->{multi}
390 0 ▶ or return $mapping;
Branch: 1 branch
BranchTrueFalseTotal
my $multi = delete $mapping->{"multi"} or return $mapping 0 0 0
391  
392 0 my $main = $attr->name;
393 0 my %new = ( type => 'multi_field', fields => { $main => $mapping } );
394 0 for my $name ( keys %$multi ) {
395 0 ▶ die "Multi-field name '$name' clashes with the attribute name\n"
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $main 0 0 0
396 if $name eq $main;
397 0 my $defn = $multi->{$name};
398 0 ▶ my $multi_type = $defn->{type} ||= $type;
Condition0%0/3
$defn->{"type"} ||= $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$defn->{"type"} ||= $type
$defn->{"type"} $type
Truth table0%0/3
A ||= B
A= $defn->{"type"}B= $type
ABresult
1X1
011
000
399  
400 0 ▶ $allowed = $Allowed_Attrs{$multi_type}
Branch: 1 branch
BranchTrueFalseTotal
$allowed = $Elastic::Model::TypeMap::Base::Allowed_Attrs{$multi_type} or die "Uknown field type ($type)" 0 0 0
401 or die "Uknown field type ($type)";
402  
403 0 for my $key ( keys %$defn ) {
404 die "Attribute has type '$multi_type', which doesn't "
405 . "understand '$key'\n"
406 0 ▶ unless $allowed->{$key} || $key eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
unless $allowed->{$key} or $key eq "type" 0 0 0
Condition0%0/3
$allowed->{$key} or $key eq "type"
l!l&&r!l&&!r
000
MC/DC0%0/2
$allowed->{$key} or $key eq "type"
$allowed->{$key} $key eq "type"
Truth table0%0/3
A or B
A= $allowed->{$key}B= $key eq "type"
ABresult
1X1
011
000
407 }
408  
409 0 $new{fields}{$name} = $map->_fixup_mapping($defn);
410 }
411 0 return \%new;
412 }
413  
414 #===================================
415 0 ▶sub _fixup_mapping {
Subroutine _fixup_mapping: not called CC=3 SCAR=24.8
416 #===================================
417 0 my ( $map, $defn ) = @_;
418  
419 delete $defn->{analyzer}
420 0 ▶ if $defn->{index_analyzer} && $defn->{search_analyzer};
Branch: 1 branch
BranchTrueFalseTotal
if $defn->{"index_analyzer"} and $defn->{"search_analyzer"} 0 0 0
Condition0%0/3
$defn->{"index_analyzer"} and $defn->{"search_analyzer"}
!ll&&!rl&&r
000
MC/DC0%0/2
$defn->{"index_analyzer"} and $defn->{"search_analyzer"}
$defn->{"index_analyzer"} $defn->{"search_analyzer"}
Truth table0%0/3
A and B
A= $defn->{"index_analyzer"}B= $defn->{"search_analyzer"}
ABresult
0X0
100
111
421  
422 0 return $defn;
423 }
424  
425 #===================================
426 0 ▶sub deflators { shift->typemap->{deflator} }
Subroutine deflators: not called CC=1 SCAR=6.9
Pod documented
427 0 ▶sub inflators { shift->typemap->{inflator} }
Subroutine inflators: not called CC=1 SCAR=6.9
Pod documented
428 0 ▶sub mappers { shift->typemap->{mapper} }
Subroutine mappers: not called CC=1 SCAR=6.9
Pod documented
429 #===================================
430  
431 #===================================
432 758 ▶sub _has_type {
Subroutine _has_type: called CC=2 SCAR=6.9
433 #===================================
434 758 my ( $class, $type, %params ) = @_;
435 758 my $map = $class->typemap();
436 758 for ( keys %params ) {
437 1884 $map->{$_}{$type} = $params{$_};
438 }
439 }
440  
441 #===================================
442 0 ▶sub indexable_attrs {
Subroutine indexable_attrs: not called CC=3 SCAR=24.8
Pod documented
443 #===================================
444 0 my $map = shift;
445 0 my $class = shift;
446 0 ▶ $class = $map->model->class_for($class) || $class;
Condition0%0/3
$map->model->class_for($class) || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
$map->model->class_for($class) || $class
$map->model->class_for($class) $class
Truth table0%0/3
A || B
A= $map->model->class_for($class)B= $class
ABresult
1X1
011
000
447  
448 0 my $meta = Class::MOP::class_of($class);
449  
450 return {
451 0 map { $_->name => $_ }
452 0 ▶ grep { !( $_->can('exclude') && $_->exclude ) }
Condition0%0/3
$_->can('exclude') && $_->exclude
!ll&&!rl&&r
000
MC/DC0%0/2
$_->can('exclude') && $_->exclude
$_->can('exclude') $_->exclude
Truth table0%0/3
A && B
A= $_->can('exclude')B= $_->exclude
ABresult
0X0
100
111
453 $meta->get_all_attributes
454 };
455 }
456  
457 #===================================
458 1010 ▶sub typemap {
Subroutine typemap: called CC=2 SCAR=7.0
Pod documented
459 #===================================
460 1010 my $class = shift;
461 1010 ▶ $class = $class->original_class
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("original_class") 0 1010 1010
462 if $class->can('original_class');
463  
464 # return a reference to the storage in ourself
465 {
466 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
467 1010 return \%{ $class . '::__ES_TYPE_MAP' };
468 }
469 }
470  
471 #===================================
472 126 ▶sub import_types {
Subroutine import_types: called CC=3 SCAR=11.0
Pod documented
473 #===================================
474 126 my $class = shift;
475 126 my $import = shift;
476 126 my $map = $class->typemap;
477 126 for (qw(deflator inflator mapper)) {
478 378 ▶ my $types = $import->{$_} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $types = $import->{$_} or next 378 0 378
479 378 @{ $map->{$_} }{ keys %$types } = values %$types;
480 }
481 }
482  
483 23 1;
484  
485 =pod
486  
487 =encoding UTF-8
488  
489 =head1 NAME
490  
491 Elastic::Model::TypeMap::Base - A base class for all TypeMaps
492  
493 =head1 VERSION
494  
495 version 0.52
496  
497 =head1 SYNOPSIS
498  
499 =head2 Define your own type map
500  
501 package MyApp::TypeMap;
502  
503 use Elastic::Model::TypeMap::Base qw(
504 Elastic::Model::TypeMap::Default
505 );
506  
507 has_type 'MyCustomType',
508 deflate_via { sub { ... }},
509 inflate_via { sub { ... }},
510 map_via { type => 'string' };
511  
512 # Inlined in/deflation
513 has_type 'MyCustomType',
514 deflate_via { '$val * 1000'},
515 inflate_via { '$val / 1000'},
516 map_via { type => 'string' };
517  
518 =head2 Use your type map by declaring it in your Model
519  
520 package MyApp;
521  
522 use Elastic::Model;
523  
524 has_namespace 'myapp', {
525 user => 'MyApp::User',
526 post => 'MyApp::Post'
527 };
528  
529 has_typemap 'MyApp::TypeMap';
530  
531 =head1 DESCRIPTION
532  
533 Moose's L<type constraints|Moose::Util::TypeConstraints> and introspection
534 allows Elastic::Model to figure out how to map your data model to the
535 Elasticsearch backend with the minimum of effort on your part.
536  
537 What YOU need to do is: B<Be specific about the type constraint for each attribute.>
538  
539 For instance, if you have an attribute called C<count>, then specify the
540 type constraint C<< isa => 'Int' >>.
541 That way, we know how to define the field in Elasticsearch, and how to deflate
542 and inflate the value.
543  
544 Type constraints can inherit their mapping, inflator and deflator from
545 their parent type-constraints. For instance, if you were to assign
546 C<count> the type constraint C<PositiveInt>, although we don't know about that
547 constraint, we do know about its parent, C<Int>, so we could
548 still handle the field correctly.
549  
550 Type maps are used to define:
551  
552 =over
553  
554 =item *
555  
556 what mapping Elastic::Model will generate for each attribute when you
557 L<create an index|Elastic::Model::Domain::Admin/"create_index()">
558 or L<update the mapping|Elastic::Model::Domain::Admin/"update_mapping()"> of an
559 existing index.
560  
561 =item *
562  
563 how Elastic::Model will deflate and inflate each attribute when saving or
564 retrieving docs stored in Elasticsearch.
565  
566 =back
567  
568 =head1 BUILT-IN TYPE MAPS
569  
570 See L<Elastic::Model::TypeMap::Default> for the type-maps provided by
571 default in Elastic::Model.
572  
573 =head1 DEFINING YOUR OWN TYPE MAP
574  
575 If you define your own types which need custom mapping or custom deflators/inflators
576 then you can add these definitions in your own type-map, while still falling
577 back to the built-in type-maps for other types.
578  
579 First, you need to name your type map class:
580  
581 package MyApp::TypeMap;
582  
583 Then import the helper functions from L<Elastic::Model::TypeMap::Base>
584 and load any other typemaps that you want to inherit from:
585  
586 use Elastic::Model::TypeMap::Base qw(
587 Elastic::Model::TypeMap::Default
588 Useful::TypeMap::From::CPAN
589 );
590  
591 Now you can define your type maps:
592  
593 has_type 'MyCustomType',
594 deflate_via { sub { ... }},
595 inflate_via { sub { ... }},
596 map_via { type => 'string' };
597  
598 The type name passed to C<has_type> should be a string, eg C<'Str'> for the
599 core Moose string type, or the fully qualified name for the types you have
600 defined with L<MooseX::Types>, eg C<'MyApp::Types::SomeType'>.
601  
602 C<deflate_via> and C<inflate_via> each expect a coderef which, when called
603 returns a coderef:
604  
605 sub {
606 my ($type_constraint, $attr, $typemap_class) = @_;
607 return sub {
608 my ($val) = @_;
609 return do_something($val)
610 }
611 }
612  
613 Alternatively, the C<deflate_via> and C<inflate_via> coderefs can, when
614 executed, return a string of Perl code which can be inlined for more
615 efficient flation. Both the inflator and deflator are passed the value
616 that needs to be converted in C<$val>. For instance:
617  
618 has_type 'MyDateType',
619 deflate_via { '$val->to_string' },
620 inflate_via { 'My::Date::Class->new($val)' },
621 map_via { 'type' => 'date' };
622  
623 C<map_via> expects a coderef which returns the mapping for that type as a list,
624 not as a hashref:
625  
626 sub {
627 my ($type_constraint, $attr, $typemap_class) = @_;
628 return (type => 'string', ..... );
629 }
630  
631 =head2 A simple example
632  
633 Here is an example of how to define a type map for DateTime objects:
634  
635 use DateTime;
636  
637 has_type 'DateTime',
638  
639 deflate_via {
640 sub { $_[0]->set_time_zone('UTC')->iso8601 };
641 },
642  
643 inflate_via {
644 sub {
645 my %args;
646 @args{ (qw(year month day hour minute second)) } = split /\D/, shift;
647 DateTime->new(%args);
648 };
649 },
650  
651 map_via { type => 'date' };
652  
653 =head2 An inlined example:
654  
655 use DateTime;
656  
657 has_type 'DateTime',
658  
659 deflate_via { '$val->set_time_zone("UTC")->iso8601' },
660 inflate_via {
661 'do {'
662 . ' my %args;'
663 . ' @args{ (qw(year month day hour minute second)) } = split /\D/, $val;'
664 . ' DateTime->new(%args);'
665 . '}'
666 },
667  
668 map_via { type => 'date' };
669  
670 =head1 ATTRIBUTES
671  
672 It is unlikely that you will need to know about any of these attributes, but
673 they are documented here for completeness.
674  
675 =head2 deflators
676  
677 $deflators = $class->deflators
678  
679 Returns a hashref of all deflators known to C<$class>.
680  
681 =head2 inflators
682  
683 $inflators = $class->inflators
684  
685 Returns a hashref of all inflators known to C<$class>.
686  
687 =head2 mappers
688  
689 $mappers = $class->mappers
690  
691 Returns a hashref of all mappers known to C<$class>.
692  
693 =head2 typemap
694  
695 $map = $class->typemap
696  
697 Returns a hashref containing the L</"deflators">, L</"inflators"> and
698 L</"mappers"> known to C<$class>.
699  
700 =head1 METHODS
701  
702 It is unlikely that you will need to know about any of these methods, but
703 they are documented here for completeness.
704  
705 L<Elastic::Model::TypeMap::Base> only has class methods, no instance methods,
706 and no C<new()>.
707  
708 =head2 find_deflator()
709  
710 $deflator = $class->find_deflator($attr)
711  
712 Returns a coderef which knows how to deflate C<$attr>, or throws an exception.
713 Any inlined deflators are eval'ed into an anonymous sub.
714  
715 =head2 find_raw_deflator()
716  
717 $deflator = $class->find_deflator($attr)
718  
719 Returns a coderef which knows how to deflate C<$attr>, or throws an exception.
720 Any inlined deflators are returned as strings.
721  
722 =head2 find_inflator()
723  
724 $inflator = $class->find_inflator($attr)
725  
726 Returns a coderef which knows how to inflate C<$attr>, or throws an exception.
727 Any inlined inflators are eval'ed into an anonymous sub.
728  
729 =head2 find_raw_inflator()
730  
731 $inflator = $class->find_inflator($attr)
732  
733 Returns a coderef which knows how to inflate C<$attr>, or throws an exception.
734 Any inlined inflators are returned as strings.
735  
736 =head2 find_mapper()
737  
738 $mapping = $class->find_mapper($attr)
739  
740 Returns a mapping for C<$attr>, or throws an exception.
741  
742 =head2 find()
743  
744 $result = $class->find($thing, $type_constraint, $attr);
745  
746 Finds a C<$thing> (C<deflator>, C<inflator>, C<mapper>) or returns C<undef>.
747  
748 =head2 class_deflator()
749  
750 $deflator = $class->class_deflator( $class_to_deflate, $attrs );
751  
752 Returns a coderef which knows how to deflate an object of class
753 C<$class_to_deflate>, including the attributes listed in C<$attr> (or all
754 attributes if not specified).
755  
756 =head2 class_inflator()
757  
758 $inflator = $class->class_inflator( $class_to_inflate, $attrs );
759  
760 Returns a coderef which knows how to inflate deflated data for class
761 C<$class_to_inflate>, including the attributes listed in C<$attr> (or all
762 attributes if not specified).
763  
764 =head2 class_mapping()
765  
766 $mapping = $class->class_mapping( $class_to_map, $attrs );
767  
768 Returns a hashref of the mapping for class C<$class_to_map>,
769 including the attributes listed in C<$attr> (or all attributes if not specified).
770  
771 =head2 attribute_mapping()
772  
773 $mapping = $class->attribute_mapping($attr);
774  
775 Returns a hashref of the mapping for attribute C<$attr>.
776  
777 =head2 indexable_attrs()
778  
779 $attrs = $class->indexable_attrs($some_class);
780  
781 Returns an array ref all all attributes in C<$some_class> which don't
782 have C<exclude> set to true.
783  
784 =head2 import_types()
785  
786 $class->import_types($other_class);
787  
788 Imports the deflators, inflators and mappers from another typemap class into
789 the current class.
790  
791 =head1 AUTHOR
792  
793 Clinton Gormley <drtech@cpan.org>
794  
795 =head1 COPYRIGHT AND LICENSE
796  
797 This software is copyright (c) 2015 by Clinton Gormley.
798  
799 This is free software; you can redistribute it and/or modify it under
800 the same terms as the Perl 5 programming language system itself.
801  
802 =cut
803  
804 __END__
« Model/Index.pm ↑ Summary Model/Meta/Class/Model.pm »