blib/lib/ElasticSearchX/Model/Document/Trait/Attribute.pm

filter: Statementstmt 91.9% 57 / 62 Branchbran 75.0% 9 / 12 Conditioncond 70.5% 12 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 85.7% 12 / 14 Podpod 33.3% 1 / 3 totaltotal 80.5% 95 / 118 CC 13 SCAR 12.5
CC
13
Cov
86%
__ANON__
6.5
_build_isa_arrayref
6.0
__ANON__
4.2
CRAP 3.5
« Build ↑ Summary blib/lib/ElasticSearchX/Model/Document/Types.pm »
1 #
2 # This file is part of ElasticSearchX-Model
3 #
4 # This software is Copyright (c) 2018 by Moritz Onken.
5 #
6 # This is free software, licensed under:
7 #
8 # The (three-clause) BSD License
9 #
10 package ElasticSearchX::Model::Document::Trait::Attribute;
11 9 $ElasticSearchX::Model::Document::Trait::Attribute::VERSION = '1.0.3';
12 # ABSTRACT: Trait that extends the meta class of a document class
13 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use ElasticSearchX::Model::Document::Mapping;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 with 'MooseX::Attribute::LazyInflator::Meta::Role::Attribute';
17  
18 9 ▶use ElasticSearchX::Model::Document::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 ▶use MooseX::Types::Moose qw(ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 9 has index => ( is => 'ro' );
22 9 has boost => ( is => 'ro', isa => 'Num' );
23 9 has store => ( is => 'ro', isa => 'Str', default => 'yes' );
24 9 has type => ( is => 'ro', isa => 'Str', default => 'string' );
25 9 has parent => ( is => 'ro', isa => 'Bool', default => 0 );
26 9 has dynamic => ( is => 'ro', isa => 'Bool', default => 0 );
27 has analyzer =>
28 39 ▶ ( is => 'ro', isa => ArrayRef, coerce => 1, default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29 9 has not_analyzed => ( is => 'ro', isa => 'Bool', default => 1 );
30 9 has term_vector => ( is => 'ro', isa => 'Str' );
31 9 has include_in_all => ( is => 'ro', isa => 'Bool', default => 1 );
32 9 has source_only => ( is => 'ro', isa => 'Bool', default => 0 );
33 9 has include_in_root => ( is => 'ro', isa => 'Bool' );
34 9 has include_in_parent => ( is => 'ro', isa => 'Bool' );
35 9 has property => ( is => 'ro', isa => 'Bool', default => 1 );
36 9 has query_property => ( is => 'ro', isa => 'Bool', default => 0 );
37 has field_name =>
38 14 ▶ ( is => 'ro', isa => 'Str', lazy => 1, default => sub { shift->name } );
Subroutine __ANON__: called CC=1 SCAR=0.0
39 9 has isa_arrayref => (
40 is => 'ro',
41 lazy => 1,
42 isa => 'Bool',
43 builder => '_build_isa_arrayref'
44 );
45  
46 sub _build_isa_arrayref {
47 0 ▶ my $self = shift;
Subroutine _build_isa_arrayref: not called CC=2 SCAR=17.9
48 0 my $tc = $self->type_constraint;
49 0 ▶ return 0 unless $tc;
Branch: 1 branch
BranchTrueFalseTotal
unless $tc 0 0 0
50 0 return $tc->is_a_type_of("ArrayRef");
51 }
52  
53 sub build_property {
54 15 ▶ my $self = shift;
Subroutine build_property: called CC=1 SCAR=0.0
Pod documented
55 return {
56 15 ElasticSearchX::Model::Document::Mapping::maptc(
57 $self, $self->type_constraint
58 )
59 };
60 }
61  
62 before _process_options => sub {
63 40 ▶ my ( $self, $name, $options ) = @_;
Subroutine __ANON__: called CC=6 SCAR=18.7
64 %$options = ( builder => 'build_id', lazy => 1, %$options )
65 40 ▶ if ( $options->{id} && ref $options->{id} eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"id"} and ref $options->{"id"} eq "ARRAY" 0 40 40
Condition66%2/3
$options->{"id"} and ref $options->{"id"} eq "ARRAY"
!ll&&!rl&&r
3910
MC/DC0%0/2
$options->{"id"} and ref $options->{"id"} eq "ARRAY"
$options->{"id"} ref $options->{"id"} eq "ARRAY"
Truth table66%2/3
A and B
A= $options->{"id"}B= ref $options->{"id"} eq "ARRAY"
ABresult
0X0
100
111
66  
67 40 ▶ $options->{traits} ||= [];
Condition50%1/2
$options->{"traits"} ||= []
l!l
400
MC/DC0%0/1
$options->{"traits"} ||= []
$options->{"traits"}
Truth table50%1/2
A
A= $options->{"traits"}
Aresult
11
00
68 push(
69 14 @{ $options->{traits} },
70 'MooseX::Attribute::LazyInflator::Meta::Role::Attribute'
71 40 ▶ ) if ( $options->{property} || !exists $options->{property} );
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"property"} or not exists $options->{"property"} 14 26 40
Condition66%2/3
$options->{"property"} or not exists $options->{"property"}
l!l&&r!l&&!r
01426
MC/DC50%1/2
$options->{"property"} or not exists $options->{"property"}
$options->{"property"} not exists $options->{"property"}
Truth table66%2/3
A or not(B)
A= $options->{"property"}B= not exists $options->{"property"}
ABresult
1X1
011
000
72 9 };
73  
74 after _process_options => sub {
75 40 ▶ my ( $class, $name, $options ) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.2
76 40 ▶ if ( $options->{required}
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"required"} and not $options->{"builder"} and not defined $options->{"default"}) 1 39 40
Condition66%4/6
$options->{"required"} and not $options->{"builder"} and not defined $options->{"default"}
!ll&&!rl&&r
3901
$options->{"required"} and not $options->{"builder"}
!ll&&!rl&&r
3901
MC/DC33%1/3
$options->{"required"} and not $options->{"builder"} and not defined $options->{"default"}
$options->{"required"} not $options->{"builder"} not defined $options->{"default"}
Truth table50%2/4
A and not(B) and not(C)
A= $options->{"required"}B= not $options->{"builder"}C= not defined $options->{"default"}
ABCresult
0XX0
10X0
1100
1111
77 && !$options->{builder}
78 && !defined $options->{default} )
79 {
80 1 $options->{lazy} = 1;
81 1 $options->{required} = 1;
82 $options->{default} = sub {
83 0 ▶ confess "Attribute $name is required";
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 1 };
85 }
86 9 };
87  
88 sub mapping {
89 25 ▶ my $self = shift;
Subroutine mapping: called CC=3 SCAR=11.0
Pod undocumented
90 25 ▶ return ( $self->name => $self->build_property )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->source_only or $self->parent 13 12 25
Condition100%3/3
$self->source_only or $self->parent
l!l&&r!l&&!r
11113
MC/DC100%2/2
$self->source_only or $self->parent
$self->source_only $self->parent
Truth table100%3/3
A or B
A= $self->source_onlyB= $self->parent
ABresult
1X1
011
000
91 unless ( $self->source_only || $self->parent );
92 12 return ();
93 }
94  
95 25 ▶sub type_mapping { () }
Subroutine type_mapping: called CC=1 SCAR=0.0
Pod undocumented
96  
97 after install_accessors => sub {
98 66 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
99 return
100 unless (
101 66 ▶ $self->associated_class->does_role(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->associated_class->does_role("ElasticSearchX::Model::Document::Role") 52 14 66
102 'ElasticSearchX::Model::Document::Role')
103 );
104 14 $self->associated_class->_add_field_alias(
105 $self->name => $self->field_name );
106 14 $self->associated_class->_add_reverse_field_alias(
107 $self->field_name => $self->name );
108 9 };
109  
110 9 1;
111  
112 __END__
113  
114 =pod
115  
116 =encoding UTF-8
117  
118 =head1 NAME
119  
120 ElasticSearchX::Model::Document::Trait::Attribute - Trait that extends the meta class of a document class
121  
122 =head1 VERSION
123  
124 version 1.0.3
125  
126 =head1 ATTRIBUTES
127  
128 =head2 property
129  
130 This defaults to C<1> and marks the attribute as ElasticSearch
131 property and thus will be added to the mapping. If you set this
132 to C<0> the attribute will act as a traditional Moose attribute.
133  
134 =head2 id
135  
136 Usually there is one property which also acts as id for the whole
137 document. If there is no attribute with the C<id> option defined
138 ElasticSearch will assign a random id. This option can either
139 be set to a true value or an arrayref. The former will make the
140 value of the attribute the id. The latter will generate a SHA1
141 digest of the concatenated values of the attributes listed in
142 the arrayref.
143  
144 Only one attribute with the C<id> option set can be present in
145 a document.
146  
147 =head2 type
148  
149 Most of the time L<ElasticSearchX::Model::Document::Mapping> will take
150 care of this option and set the correct value based on the
151 type constriant. In case it doesn't know what to do, this
152 value will be used as the type for the attribute. Defaults
153 to C<string>.
154  
155 =head2 parent
156  
157 The value of this property will be used as C<parent> id.
158 Since the parent id is stored in the C<_parent> field, it
159 is adviced to set L</source_only> to C<1> to prevent the
160 field from being stored redundantly.
161  
162 =head2 source_only
163  
164 A C<source_only> attribute is not added to the type mapping,
165 but its value is included in the C<_source> of a document.
166 This is helpful if you don't want to index the value of this
167 attribute in Elasticsearch, but still want to be able to access
168 its value.
169  
170 =head2 timestamp
171  
172 has timestamp => ( timestamp => 1, is => 'ro' );
173 has timestamp => ( timestamp => { store => 1 }, is => 'ro' );
174  
175 The attribute using this option will become the timestamp field
176 (L<http://www.elasticsearch.org/guide/reference/mapping/timestamp-field.html>).
177  
178 =head2 ttl
179  
180 has expire => ( ttl => 1, is => 'ro' );
181  
182 $document->expire(86400) # document will expire in one day
183  
184 Documents with a ttl field will set the _ttl property on the document in
185 ElasticSearch. This will cause ElasticSearch to remove the document
186 from the index automatically once the time to live has expired.
187  
188 See L<http://www.elasticsearch.org/guide/reference/mapping/ttl-field.html>.
189  
190 =head1 PASS THROUGH ATTRIBUTES
191  
192 The following attributes are passed through - as is - to the
193 type mapping.
194  
195 =head2 store
196  
197 Defaults to C<yes>.
198  
199 =head2 boost
200  
201 =head2 index
202  
203 =head2 dynamic
204  
205 By default, properties are not dynamic. That means that fields that
206 are unknown to the ElasticSearch mapping will not be indexed and
207 no dynamic mapping will be auto generated. Since the mapping is
208 generated by this module, there is no need to have the dynamic
209 mapping enabled. If you want to enable it anyway, set this attribute
210 to C<1> value.
211  
212 This will change the behaviour of C<HashRef> attributes.
213 Instead of deflating to a JSON string they will be stored as object
214 in ElasticSearch.
215  
216 =head2 analyzer
217  
218 If this attribute or L</index> is set to C<analyzed>, the field is stored
219 both analyzed and not analyzed. The not analyzed field can be accessed in
220 queries by the field name. The analyzed field will have a C<.analyzed>
221 postfix if there is only one analyzer. If there are more than one analyzer,
222 the C<.analyzed> postfix refers to the first analyzer. For all other
223 analyzers, its name is prefixed. This is done by using the C<multi_field>
224 feature of ElasticSearch.
225  
226 =head2 include_in_all
227  
228 =head2 include_in_parent
229  
230 =head2 include_in_root
231  
232 =head2 term_vector
233  
234 =head2 not_analyzed
235  
236 By default, a field is not analyzed. That means the raw value is stored
237 in the field. To add analyzed fields, either set L</index> to C<analyzed>
238 or add an L</analyzer>. Set this to C<0> if you don't want to store
239 the not analyzed version of this field along with the analyzed.
240  
241 =head1 METHODS
242  
243 =head2 build_id
244  
245 =head2 build_property
246  
247 This method is called by L<ElasticSearchX::Model::Document::Trait::Class/mapping>
248 and returns the mapping for this property as a HashRef.
249  
250 =head1 AUTHOR
251  
252 Moritz Onken
253  
254 =head1 COPYRIGHT AND LICENSE
255  
256 This software is Copyright (c) 2018 by Moritz Onken.
257  
258 This is free software, licensed under:
259  
260 The (three-clause) BSD License
261  
262 =cut
« Build ↑ Summary blib/lib/ElasticSearchX/Model/Document/Types.pm »