Model/Meta/Class/Model.pm

filter: Statementstmt 66.2% 53 / 80 Branchbran 25.0% 8 / 32 Conditioncond 36.8% 7 / 19 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 86.6% 13 / 15 Podpod 100% 3 / 3 totaltotal 54.3% 87 / 160 CC 29 SCAR 46.7
CC
29
Cov
52%
analysis_for_mappings
272.0
_required_analyzers
30.0
__ANON__
7.3
CRAP 106.5
« Model/TypeMap/Base.pm ↑ Summary Doc.pm »
1 package Elastic::Model::Meta::Class::Model;
2 23 $Elastic::Model::Meta::Class::Model::VERSION = '0.52';
3 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use MooseX::Types::Moose qw(HashRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use Data::Dump qw(pp);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 my %defaults = (
11 analyzer => {},
12 tokenizer => {},
13 );
14  
15 23 for my $k (qw(namespace char_filter analyzer filter tokenizer)) {
16 115 ▶ my %default = %{ $defaults{$k} || {} };
Condition100%2/2
$defaults{$k} || {}
l!l
4669
MC/DC100%1/1
$defaults{$k} || {}
$defaults{$k}
Truth table100%2/2
A
A= $defaults{$k}
Aresult
11
00
17  
18 #===================================
19 has "${k}s" => (
20 #===================================
21 is => 'ro',
22 traits => ['Hash'],
23 isa => HashRef,
24 115 ▶ default => sub { +{%default} },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 115 handles => {
26 $k => 'get',
27 "add_${k}" => 'set',
28 "has_${k}" => 'exists',
29 "all_${k}s" => 'keys',
30 }
31 );
32 115 ▶ next if $k eq 'namespace';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "namespace" 23 92 115
33  
34 #===================================
35 8 ▶ before "add_$k" => sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
36 #===================================
37 8 my $class = shift;
38 8 ▶ my %params = ref $_[0] ? { shift() } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 8 8
39 8 for my $defn ( values %params ) {
40 8 ▶ my $type = $defn->{type} || 'custom';
Condition100%2/2
$defn->{'type'} || 'custom'
l!l
44
MC/DC100%1/1
$defn->{'type'} || 'custom'
$defn->{'type'}
Truth table100%2/2
A
A= $defn->{'type'}
Aresult
11
00
41 8 ▶ return if $type eq 'custom' and $k eq 'analyzer';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "custom" and $k eq "analyzer" 4 4 8
Condition66%2/3
$type eq "custom" and $k eq "analyzer"
!ll&&!rl&&r
404
MC/DC50%1/2
$type eq "custom" and $k eq "analyzer"
$type eq "custom" $k eq "analyzer"
Truth table66%2/3
A and B
A= $type eq "custom"B= $k eq "analyzer"
ABresult
0X0
100
111
42 4 ▶ croak "Unknown type ($type) in $k:\n" . pp( \%params ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->is_default($k, $type) 0 4 4
43 unless $class->is_default( $k, $type );
44 }
45 92 };
46 }
47  
48 #===================================
49 has 'classes' => (
50 #===================================
51 is => 'ro',
52 isa => HashRef [Str],
53 traits => ['Hash'],
54 23 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 23 +{ typemap => 'Elastic::Model::TypeMap::Default',
56 domain => 'Elastic::Model::Domain',
57 namespace => 'Elastic::Model::Namespace',
58 store => 'Elastic::Model::Store',
59 view => 'Elastic::Model::View',
60 scope => 'Elastic::Model::Scope',
61 results => 'Elastic::Model::Results',
62 cached_results => 'Elastic::Model::Results::Cached',
63 scrolled_results => 'Elastic::Model::Results::Scrolled',
64 result => 'Elastic::Model::Result',
65 bulk => 'Elastic::Model::Bulk'
66 };
67 },
68 23 handles => {
69 set_class => 'set',
70 get_class => 'get',
71 }
72 );
73  
74 #===================================
75 has 'unique_index' => (
76 #===================================
77 is => 'rw',
78 isa => 'Str',
79 23 ▶ default => sub {'unique_key'}
Subroutine __ANON__: called CC=1 SCAR=0.0
80 23 );
81  
82 23 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 our %DefaultAnalysis = (
85 46 char_filter => { map { $_ => 1 } qw(html_strip mapping) },
86 filter => +{
87 575 map { $_ => 1 }
88 qw(
89 standard asciifolding length lowercase ngram edge_ngram
90 porterStem shingle stop word_delimiter snowball kstem phonetic
91 synonym dictionary_decompounder hyphenation_decompounder
92 reverse elision trim truncate unique pattern_replace
93 icu_normalizer icu_folding icu_collation
94 )
95 },
96 tokenizer => {
97 230 map { $_ => 1 }
98 qw(
99 edge_ngram keyword letter lowercase ngram standard
100 whitespace pattern uax_url_email path_hierarchy
101 )
102 },
103 analyzer => {
104 23 map { $_ => 1 }
105 qw(
106 standard simple whitespace stop keyword pattern snowball
107 arabic armenian basque brazilian bulgarian catalan chinese
108 cjk czech danish dutch english finnish french galician german
109 greek hindi hungarian indonesian italian latvian
110 norwegian persian portuguese romanian russian spanish swedish
111 turkish thai
112 )
113 }
114 );
115  
116 #===================================
117 4 ▶sub is_default {
Subroutine is_default: called CC=4 SCAR=14.6
Pod documented
118 #===================================
119 4 my $self = shift;
120 4 ▶ my $type = shift || '';
Condition50%1/2
shift() || ''
l!l
40
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
121 croak "Unknown type ($type) passed to is_default()"
122 4 ▶ unless exists $DefaultAnalysis{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Elastic::Model::Meta::Class::Model::DefaultAnalysis{$type} 0 4 4
123 4 ▶ my $name = shift or croak "No $type name passed to is_default";
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("No $type name passed to is_default") 4 0 4
124 4 return exists $DefaultAnalysis{$type}{$name};
125 }
126  
127 #===================================
128 0 ▶sub analysis_for_mappings {
Subroutine analysis_for_mappings: not called CC=16 SCAR=56.1
Pod documented
129 #===================================
130 0 my $self = shift;
131 0 my $mappings = shift;
132  
133 0 my %analyzers;
134 0 for my $type ( keys %$mappings ) {
135 0 for my $name ( _required_analyzers( $mappings->{$type} ) ) {
136 next
137 0 ▶ if exists $analyzers{$name}
Branch: 1 branch
BranchTrueFalseTotal
if exists $analyzers{$name} or $self->is_default("analyzer", $name) 0 0 0
Condition0%0/3
exists $analyzers{$name} or $self->is_default("analyzer", $name)
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $analyzers{$name} or $self->is_default("analyzer", $name)
exists $analyzers{$name} $self->is_default("analyzer", $name)
Truth table0%0/3
A or B
A= exists $analyzers{$name}B= $self->is_default("analyzer", $name)
ABresult
1X1
011
000
138 || $self->is_default( 'analyzer', $name );
139 0 ▶ $analyzers{$name} = $self->analyzer($name)
Branch: 1 branch
BranchTrueFalseTotal
$analyzers{$name} = $self->analyzer($name) or die "Unknown analyzer ($name) required by type ($type)" 0 0 0
140 or die "Unknown analyzer ($name) required by type ($type)";
141 }
142 }
143 0 ▶ return unless %analyzers;
Branch: 1 branch
BranchTrueFalseTotal
unless %analyzers 0 0 0
144  
145 0 my %analysis = ( analyzer => \%analyzers );
146 0 for my $type (qw(tokenizer filter char_filter )) {
147 0 my %defn;
148 0 for my $analyzer_name ( keys %analyzers ) {
149 0 ▶ my $vals = $analyzers{$analyzer_name}{$type} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $vals = $analyzers{$analyzer_name}{$type} or next 0 0 0
150 0 ▶ for my $name ( ref $vals ? @$vals : $vals ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals ? : 0 0 0
151 next
152 0 ▶ if exists $defn{$name}
Branch: 1 branch
BranchTrueFalseTotal
if exists $defn{$name} or $self->is_default($type, $name) 0 0 0
Condition0%0/3
exists $defn{$name} or $self->is_default($type, $name)
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $defn{$name} or $self->is_default($type, $name)
exists $defn{$name} $self->is_default($type, $name)
Truth table0%0/3
A or B
A= exists $defn{$name}B= $self->is_default($type, $name)
ABresult
1X1
011
000
153 || $self->is_default( $type, $name );
154 0 ▶ $defn{$name} = $self->$type($name)
Branch: 1 branch
BranchTrueFalseTotal
$defn{$name} = $self->$type($name) or die "Unknown $type ($name) required by analyzer '${analyzer_name}'" 0 0 0
155 or die
156 "Unknown $type ($name) required by analyzer '$analyzer_name'";
157 }
158 }
159 0 ▶ $analysis{$type} = \%defn if %defn;
Branch: 1 branch
BranchTrueFalseTotal
if %defn 0 0 0
160 }
161 0 return \%analysis;
162 }
163  
164 #===================================
165 0 ▶sub _required_analyzers {
Subroutine _required_analyzers: not called CC=5 SCAR=34.0
166 #===================================
167 0 my @analyzers;
168 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
169 0 ▶ my $mapping = shift or next;
Branch: 1 branch
BranchTrueFalseTotal
my $mapping = shift() or next 0 0 0
170 my @sub = (
171 0 ▶ values %{ $mapping->{fields} || {} },
Condition0%0/2
$mapping->{'fields'} || {}
l!l
00
MC/DC0%0/1
$mapping->{'fields'} || {}
$mapping->{'fields'}
Truth table0%0/2
A
A= $mapping->{'fields'}
Aresult
11
00
172 0 ▶ values %{ $mapping->{properties} || {} }
Condition0%0/2
$mapping->{'properties'} || {}
l!l
00
MC/DC0%0/1
$mapping->{'properties'} || {}
$mapping->{'properties'}
Truth table0%0/2
A
A= $mapping->{'properties'}
Aresult
11
00
173 );
174  
175 push @analyzers, _required_analyzers(@sub),
176 0 map { $mapping->{$_} } grep /analyzer/, keys %$mapping;
177 }
178  
179 0 return @analyzers;
180 }
181  
182 23 our $Counter = 1;
183 #===================================
184 220 ▶sub wrapped_class_name {
Subroutine wrapped_class_name: called CC=1 SCAR=0.0
Pod documented
185 #===================================
186 220 return 'Elastic::Model::__WRAPPED_' . $Counter++ . '_::' . $_[1];
187 }
188  
189 23 1;
190  
191 =pod
192  
193 =encoding UTF-8
194  
195 =head1 NAME
196  
197 Elastic::Model::Meta::Class::Model - A meta-class for Models
198  
199 =head1 VERSION
200  
201 version 0.52
202  
203 =head1 DESCRIPTION
204  
205 Holds static information about your model: namespaces and their types,
206 and char_filters, tokenizers, filters and analyzers for analysis.
207  
208 You shouldn't need to use this class directly. Everything you need should
209 be accessible via L<Elastic::Model> or L<Elastic::Model::Role::Model>.
210  
211 =head1 METHODS
212  
213 =head2 is_default()
214  
215 $bool = $meta->is_default($type => $name);
216  
217 Returns C<true> if C<$name> is a C<$type> (analyzer, tokenizer,
218 filter, char_filter) available in Elasticsearch by default.
219  
220 =head3 Default analyzers
221  
222 L<standard|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html>,
223 L<simple|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-simple-analyzer.html>,
224 L<whitespace|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-whitespace-analyzer.html>,
225 L<stop|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-stop-analyzer.html>,
226 L<keyword|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-keyword-analyzer.html>,
227 L<pattern|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-pattern-analyzer.html>,
228 L<snowball|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-snowball-analyzer.html>,
229 and the L<language|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html>
230 analyzers: C<arabic>, C<armenian>, C<basque>, C<brazilian>, C<bulgarian>,
231 C<catalan>, C<chinese>, C<cjk>, C<czech>, C<danish>, C<dutch>, C<english>,
232 C<finnish>, C<french>, C<galician>, C<german>, C<greek>, C<hindi>, C<hungarian>,
233 C<indonesian>, C<italian>, C<latvian>, C<norwegian>, C<persian>,
234 C<portuguese>, C<romanian>, C<russian>, C<spanish>, C<swedish>,
235 C<thai>, C<turkish>
236  
237 =head3 Default tokenizers
238  
239 L<edge_ngram|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenizer.html>,
240 L<keyword|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-keyword-tokenizer.html>,
241 L<letter|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-letter-tokenizer.html>,
242 L<lowercase|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-lowercase-tokenizer.html>,
243 L<ngram|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-ngram-tokenizer.html>,
244 L<path_hierarchy|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-pathhierarchy-tokenizer.html>,
245 L<pattern|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-pattern-tokenizer.html>,
246 L<standard|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-tokenizer.html>,
247 L<uax_url_email|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-uaxurlemail-tokenizer.html>,
248 L<whitespace|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-whitespace-tokenizer.html>
249  
250 =head3 Default token filters
251  
252 L<asciifolding|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-asciifolding-tokenfilter.html>,
253 L<dictionary_decompounder|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-compound-word-tokenfilter.html>,
254 L<edge_ngram|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenfilter.html>,
255 L<elision|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-elision-tokenfilter.html>,
256 L<hyphenation_decompounder|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-compound-word-tokenfilter.html>,
257 L<icu_collation|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-icu-plugin.html>,
258 L<icu_folding|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-icu-plugin.html>,
259 L<icu_normalizer|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-icu-plugin.html>,
260 L<kstem|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-kstem-tokenfilter.html>,
261 L<length|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-length-tokenfilter.html>,
262 L<lowercase|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-lowercase-tokenfilter.html>,
263 L<ngram|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-ngram-tokenfilter.html>,
264 L<pattern_replace|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-pattern_replace-tokenfilter.html>,
265 L<phonetic|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-phonetic-tokenfilter.html>,
266 L<porterStem|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-porterstem-tokenfilter.html>,
267 L<reverse|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-reverse-tokenfilter.html>,
268 L<shingle|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-shingle-tokenfilter.html>,
269 L<snowball|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-snowball-tokenfilter.html>,
270 L<standard|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-standard-tokenfilter.html>,
271 L<stop|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-stop-tokenfilter.html>,
272 L<synonym|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-synonym-tokenfilter.html>,
273 L<trim|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-trim-tokenfilter.html>,
274 L<truncate|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-truncate-tokenfilter.html>,
275 L<unique|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-word-delimiter-tokenfilter.html>,
276 L<word_delimiter|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-word-delimiter-tokenfilter.html>
277  
278 =head3 Default character filters
279  
280 L<html_strip|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-htmlstrip-charfilter.html>,
281 L<mapping|http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-mapping-charfilter.html>
282  
283 =head2 analysis_for_mappings()
284  
285 $analysis = $meta->analysis_for_mappings($mappings)
286  
287 Used to generate the C<analysis> settings for an index, based on which
288 analyzers are used in the C<mappings> for all C<types> in the index.
289  
290 =head2 wrapped_class_name()
291  
292 $new_class = $meta->wrapped_class_name($old_class);
293  
294 Generates a semi-anonymous classname with the format
295 C<Elastic::Model::__WRAPPED_::$n>
296  
297 =head1 ATTRIBUTES
298  
299 =head2 namespaces
300  
301 \%namespaces = $meta->namespaces;
302 \%namespace = $meta->namespace($name);
303 $bool = $meta->has_namespace($name);
304 @names = $meta->all_namespaces;
305  
306 A hash ref containing all namespaces plus their configuration, eg:
307  
308 {
309 myapp => {
310 types => {
311 user => 'MyApp::User'
312 }
313 }
314 }
315  
316 =head2 unique_index
317  
318 $index = $meta->unique_index
319  
320 The name of the index where unique keys will be stored, which defaults
321 to C<unique_key>. A different value can be specified with
322 L<has_unique_index|Elastic::Model/Custom unique key index>.
323  
324 See L<Elastic::Manual::Attributes::Unique> for more.
325  
326 =head2 analyzers
327  
328 \%analyzers = $meta->analyzers;
329 \%analyzer = $meta->analyzer($name);
330 $bool = $meta->has_analyzer($name);
331 @names = $meta->all_analyzers;
332  
333 A hash ref containing all analyzers plus their configuration, eg:
334  
335 {
336 my_analyzer => {
337 type => 'custom',
338 tokenizer => 'standard',
339 filter => ['lower']
340 }
341 }
342  
343 =head2 tokenizers
344  
345 \%tokenizers = $meta->tokenizers;
346 \%tokenizer = $meta->tokenizer($name);
347 $bool = $meta->has_tokenizer($name);
348 @names = $meta->all_tokenizers;
349  
350 A hash ref containing all tokenizers plus their configuration, eg:
351  
352 {
353 my_tokenizer => {
354 type => 'pattern',
355 pattern => '\W'
356 }
357 }
358  
359 =head2 filters
360  
361 \%filters = $meta->filters;
362 \%filter = $meta->filter($name);
363 $bool = $meta->has_filter($name);
364 @names = $meta->all_filters;
365  
366 A hash ref containing all filters plus their configuration, eg:
367  
368 {
369 my_filter => {
370 type => 'edge_ngram',
371 min_gram => 1,
372 max_gram => 20
373 }
374 }
375  
376 =head2 char_filters
377  
378 \%char_filters = $meta->char_filters;
379 \%char_filter = $meta->char_filter($name);
380 $bool = $meta->has_char_filter($name);
381 @names = $meta->all_char_filters;
382  
383 A hash ref containing all char_filters plus their configuration, eg:
384  
385 {
386 my_char_filter => {
387 type => 'mapping',
388 mappings => ['ph=>f','qu=>q']
389 }
390 }
391  
392 =head1 AUTHOR
393  
394 Clinton Gormley <drtech@cpan.org>
395  
396 =head1 COPYRIGHT AND LICENSE
397  
398 This software is copyright (c) 2015 by Clinton Gormley.
399  
400 This is free software; you can redistribute it and/or modify it under
401 the same terms as the Perl 5 programming language system itself.
402  
403 =cut
404  
405 __END__
« Model/TypeMap/Base.pm ↑ Summary Doc.pm »