Model/Document/Trait/Class.pm

filter: Statementstmt 75.8% 91 / 120 Branchbran 39.1% 18 / 46 Conditioncond 25.0% 7 / 28 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 84.0% 21 / 25 Podpod 70.0% 7 / 10 totaltotal 59.5% 146 / 245 SCAR 48.4
CC
37
Cov
60%
inflate_result
110.0
all_properties_loaded
30.0
get_query_data
27.5
CRAP 126.0
« Model/Document/Set.pm ↑ Summary Model/Document/Role.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::Class;
11 9 $ElasticSearchX::Model::Document::Trait::Class::VERSION = '2.0.0';
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Module::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use Eval::Closure;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 has set_class => ( is => 'ro', builder => '_build_set_class', lazy => 1 );
21 9 has short_name => ( is => 'ro', builder => '_build_short_name', lazy => 1 );
22 9 has _all_properties =>
23 ( is => 'ro', lazy => 1, builder => '_build_all_properties' );
24 9 has _isa_arrayref =>
25 ( is => 'ro', lazy => 1, builder => '_build_isa_arrayref' );
26  
27 has _field_alias => (
28 is => 'ro',
29 traits => ['Hash'],
30 isa => 'HashRef[Str]',
31 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 9 handles => { _add_field_alias => 'set' },
33 );
34 has _reverse_field_alias => (
35 is => 'ro',
36 traits => ['Hash'],
37 isa => 'HashRef[Str]',
38 14 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 9 handles => { _add_reverse_field_alias => 'set' },
40 );
41 9 has _id_attribute => ( is => 'rw', lazy_build => 1 );
42  
43 9 has _attribute_traits => ( is => 'ro', lazy_build => 1 );
44  
45 sub _build__attribute_traits {
46 return {
47 map {
48 6 ▶ Class::Load::load_class($_);
Subroutine _build__attribute_traits: called CC=1 SCAR=0.0
49 24 my ($name) = ( $_ =~ /::(\w+)$/ );
50 24 lc($name) => $_
51 } Module::Find::findallmod(
52 'ElasticSearchX::Model::Document::Trait::Field')
53 };
54 }
55  
56 sub _build_set_class {
57 2 ▶ my $self = shift;
Subroutine _build_set_class: called CC=3 SCAR=11.4
58 2 my $set = $self->name . '::Set';
59 2 ▶ eval { Class::Load::load_class($set); } and return $set
Branch: 1 branch
BranchTrueFalseTotal
eval { do { Class::Load::load_class($set) } } and return $set or return "ElasticSearchX::Model::Document::Set" 2 0 2
Condition50%1/2
eval { and return $set
!ll
20
MC/DC0%0/1
eval { do { Class::Load::load_class($set) } } and return $set
eval { do { Class::Load::load_class($set) } }
Truth table50%1/2
A
A= eval { do { Class::Load::load_class($set) } }
Aresult
00
11
60 or return 'ElasticSearchX::Model::Document::Set';
61 }
62  
63 sub mapping {
64 5 ▶ my $self = shift;
Subroutine mapping: called CC=2 SCAR=6.9
Pod documented
65 5 my $props = { map { $_->mapping } $self->get_all_properties };
66 5 my $parent = $self->get_parent_attribute;
67 return {
68 $parent ? ( _parent => { type => $parent->name } ) : (),
69 dynamic => \0,
70 properties => $props,
71 5 ▶ map { $_->type_mapping } $self->get_all_properties,
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 1 4 5
72 };
73 }
74  
75 sub _build_short_name {
76 11 ▶ my $self = shift;
Subroutine _build_short_name: called CC=1 SCAR=0.0
77 11 ( my $name = $self->name ) =~ s/^.*:://;
78 11 return lc($name);
79 }
80  
81 sub get_id_attribute {
82 4 ▶ return shift->_id_attribute;
Subroutine get_id_attribute: called CC=1 SCAR=0.0
Pod documented
83 }
84  
85 sub _build__id_attribute {
86 1 ▶ my $self = shift;
Subroutine _build__id_attribute: called CC=1 SCAR=0.0
87 my (@id)
88 1 = grep { $_->does('ElasticSearchX::Model::Document::Trait::Field::ID') }
89 $self->get_all_properties;
90 1 return pop @id;
91 }
92  
93 sub get_parent_attribute {
94 6 ▶ my $self = shift;
Subroutine get_parent_attribute: called CC=2 SCAR=7.0
Pod documented
95 6 my ( $id, $more ) = grep { $_->parent } $self->get_all_properties;
96 6 ▶ croak "Cannot have more than one parent field on a class" if ($more);
Branch: 1 branch
BranchTrueFalseTotal
if $more 0 6 6
97 6 return $id;
98 }
99  
100 sub get_version_attribute {
101 0 ▶ shift->get_attribute('_version');
Subroutine get_version_attribute: not called CC=1 SCAR=6.9
Pod undocumented
102 }
103  
104 sub add_property {
105 24 ▶ my ( $self, $name ) = ( shift, shift );
Subroutine add_property: called CC=8 SCAR=20.9
Pod documented
106 24 ▶ Moose->throw_error('Usage: has \'name\' => ( key => value, ... )')
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 0 24 24
107 if @_ % 2 == 1;
108 24 my %options = ( definition_context => _caller_info(), @_ );
109 24 ▶ $options{traits} ||= [];
Condition50%1/2
$options{"traits"} ||= []
l!l
024
MC/DC0%0/1
$options{"traits"} ||= []
$options{"traits"}
Truth table50%1/2
A
A= $options{"traits"}
Aresult
11
00
110 push(
111 23 @{ $options{traits} },
112 'ElasticSearchX::Model::Document::Trait::Attribute'
113 24 ▶ ) if ( $options{property} || !exists $options{property} );
Branch: 1 branch
BranchTrueFalseTotal
if $options{"property"} or not exists $options{"property"} 23 1 24
Condition66%2/3
$options{"property"} or not exists $options{"property"}
l!l&&r!l&&!r
0231
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
114 24 delete $options{property};
115 24 my $attr_traits = $self->_attribute_traits;
116 24 for ( grep { $attr_traits->{$_} } keys %options ) {
117 1 push( @{ $options{traits} }, $attr_traits->{$_} );
118  
119 #(my $class_trait = $attr_traits{$_}) =~ s/::Field::/::Class::/;
120 #Moose::Util::apply_all_roles($meta, $class_trait);
121 }
122 24 ▶ my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'ARRAY' ? : 1 23 24
123 24 $self->add_attribute( $_, %options ) for @$attrs;
124 }
125  
126 sub _caller_info {
127 24 ▶ my $level = @_ ? ( $_[0] + 1 ) : 2;
Subroutine _caller_info: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 24 24
128 24 my %info;
129 24 @info{qw(package file line)} = caller($level);
130 24 return \%info;
131 }
132  
133 sub all_properties_loaded {
134 0 ▶ my ( $self, $instance ) = @_;
Subroutine all_properties_loaded: not called CC=5 SCAR=34.0
Pod documented
135 0 my $loaded = $instance->_loaded_attributes;
136 0 ▶ return 1 unless ($loaded);
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 0 0
137 0 my @properties = $self->get_all_properties;
138 0 for (@properties) {
139 return undef
140 0 ▶ unless ( $loaded->{ $_->name } || $_->has_value($instance) );
Branch: 1 branch
BranchTrueFalseTotal
unless $$loaded{$_->name} or $_->has_value($instance) 0 0 0
Condition0%0/3
$$loaded{$_->name} or $_->has_value($instance)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$loaded{$_->name} or $_->has_value($instance)
$$loaded{$_->name} $_->has_value($instance)
Truth table0%0/3
A or B
A= $$loaded{$_->name}B= $_->has_value($instance)
ABresult
1X1
011
000
141 }
142 0 return 1;
143 }
144  
145 sub get_all_properties {
146 24 ▶ my $self = shift;
Subroutine get_all_properties: called CC=2 SCAR=6.9
Pod documented
147 24 ▶ return @{ $self->_all_properties }
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_immutable 4 20 24
148 if ( $self->is_immutable );
149 20 return @{ $self->_build_all_properties };
150 }
151  
152 sub _build_all_properties {
153 return [
154 22 ▶ grep { $_->does('ElasticSearchX::Model::Document::Trait::Attribute') }
Subroutine _build_all_properties: called CC=1 SCAR=0.0
155 shift->get_all_attributes
156 ];
157 }
158  
159 sub _build_isa_arrayref {
160 0 return { map { $_->name => $_->isa_arrayref }
161 0 ▶ @{ shift->_all_properties } };
Subroutine _build_isa_arrayref: not called CC=1 SCAR=6.9
162 }
163  
164 sub get_data {
165 5 ▶ my ( $self, $instance ) = @_;
Subroutine get_data: called CC=6 SCAR=18.2
Pod documented
166 return {
167 map {
168 7 ▶ my $deflate
Branch: 1 branch
BranchTrueFalseTotal
$_->is_inflated($instance) || $_->is_required && !$_->has_value($instance) ? : 3 4 7
Condition50%3/6
$_->is_inflated($instance) or $_->is_required and not $_->has_value($instance)
l!l&&r!l&&!r
304
$_->is_required and not $_->has_value($instance)
!ll&&!rl&&r
400
MC/DC33%1/3
$_->is_inflated($instance) or $_->is_required and not $_->has_value($instance)
$_->is_inflated($instance) $_->is_required not $_->has_value($instance)
Truth table50%2/4
A or B and not(C)
A= $_->is_inflated($instance)B= $_->is_requiredC= not $_->has_value($instance)
ABCresult
1XX1
0111
00X0
0100
169 = $_->is_inflated($instance)
170 || $_->is_required && !$_->has_value($instance)
171 ? $_->deflate($instance)
172 : $_->get_raw_value($instance);
173 7 ▶ defined $deflate ? ( $_->field_name => $deflate ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $deflate ? : 6 1 7
174 26 ▶ } grep { $_->has_value($instance) || $_->is_required }
Branch: 1 branch
BranchTrueFalseTotal
$_->has_value($instance) or $_->is_required 19 7 26
175 5 grep { $_->property } $self->get_all_properties
176 };
177 }
178  
179 sub get_query_data {
180 1 ▶ my ( $self, $instance ) = @_;
Subroutine get_query_data: called CC=6 SCAR=33.1
Pod undocumented
181 return {
182 map {
183 0 ▶ my $deflate
Branch: 1 branch
BranchTrueFalseTotal
$_->is_inflated($instance) || $_->is_required && !$_->has_value($instance) ? : 0 0 0
Condition0%0/6
$_->is_inflated($instance) or $_->is_required and not $_->has_value($instance)
l!l&&r!l&&!r
000
$_->is_required and not $_->has_value($instance)
!ll&&!rl&&r
000
MC/DC0%0/3
$_->is_inflated($instance) or $_->is_required and not $_->has_value($instance)
$_->is_inflated($instance) $_->is_required not $_->has_value($instance)
Truth table0%0/4
A or B and not(C)
A= $_->is_inflated($instance)B= $_->is_requiredC= not $_->has_value($instance)
ABCresult
1XX1
0111
00X0
0100
184 = $_->is_inflated($instance)
185 || $_->is_required && !$_->has_value($instance)
186 ? $_->deflate($instance)
187 : $_->get_raw_value($instance);
188 0 ( my $field = $_->field_name ) =~ s/^_//;
189 0 ▶ defined $deflate ? ( $field => $deflate ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $deflate ? : 0 0 0
190 0 ▶ } grep { $_->has_value($instance) || $_->is_required }
Branch: 1 branch
BranchTrueFalseTotal
$_->has_value($instance) or $_->is_required 0 0 0
191 1 grep { $_->query_property } $self->get_all_properties
192 };
193 }
194  
195 sub inflate_result {
196 0 ▶ my ( $self, $index, $res ) = @_;
Subroutine inflate_result: not called CC=10 SCAR=47.0
Pod undocumented
197  
198 #my $id = $self->get_id_attribute;
199 0 my $parent = $self->get_parent_attribute;
200 0 my $arrays = $self->_isa_arrayref;
201 0 ▶ my $fields = { %{ $res->{_source} || {} }, %{ $res->{fields} || {} } };
Branch: 2 branches
BranchTrueFalseTotal
$res->{"_source"} or {} 0 0 0
$res->{"fields"} or {} 0 0 0
202 $fields = {
203 map {
204 0 my $is_array = ref $fields->{$_} eq "ARRAY";
205 $arrays->{$_} && !$is_array ? ( $_ => [ $fields->{$_} ] )
206 : !$arrays->{$_} && $is_array ? ( $_ => $fields->{$_}->[0] )
207 0 ▶ : ( $_ => $fields->{$_} );
Branch: 2 branches
BranchTrueFalseTotal
$arrays->{$_} && !$is_array ? : 0 0 0
!$arrays->{$_} && $is_array ? : 0 0 0
Condition0%0/6
$arrays->{$_} and not $is_array
!ll&&!rl&&r
000
not $arrays->{$_} and $is_array
!ll&&!rl&&r
000
MC/DC0%0/2
$arrays->{$_} and not $is_array
$arrays->{$_} not $is_array
MC/DC0%0/2
not $arrays->{$_} and $is_array
not $arrays->{$_} $is_array
Truth table0%0/3
A and not(B)
A= $arrays->{$_}B= not $is_array
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not $arrays->{$_}B= $is_array
ABresult
0X0
100
111
208 } keys %$fields
209 };
210 0 my $map = $self->_reverse_field_alias;
211 map {
212 $fields->{ $map->{$_} }
213 = defined $res->{$_}
214 ? $res->{$_}
215 0 ▶ : $fields->{$_}
Branch: 1 branch
BranchTrueFalseTotal
defined $res->{$_} ? : 0 0 0
216 }
217 0 ▶ grep { defined $fields->{$_} || defined $res->{$_} } keys %$map;
Branch: 1 branch
BranchTrueFalseTotal
defined $fields->{$_} or defined $res->{$_} 0 0 0
218 return $self->name->new(
219 {
220 %$fields,
221 index => $index,
222 _id => $res->{_id},
223 _version => $res->{_version},
224 0 ▶ $parent ? ( $parent->name => $res->{_parent} ) : (),
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 0 0 0
225 }
226 );
227 }
228  
229 9 1;
230  
231 __END__
232  
233 =pod
234  
235 =encoding UTF-8
236  
237 =head1 NAME
238  
239 ElasticSearchX::Model::Document::Trait::Class - Trait that extends the meta class of a document class
240  
241 =head1 VERSION
242  
243 version 2.0.0
244  
245 =head1 ATTRIBUTES
246  
247 =head2 set_class
248  
249 A call to C<< $index->type('tweet') >> returns an instance of C<set_class>. Given a
250 document class C<MyModel::Tweet>, the builder of this attribute tries to find a
251 class named C<MyModel::Tweet::Set>. If it's not found, the default class
252 L<ElasticSearchX::Model::Document::Set> is used.
253  
254 A custum set class (e.g. C<MyModel::Tweet::Set>) B<must> inherit from
255 L<ElasticSearchX::Model::Document::Set>.
256  
257 =head2 short_name
258  
259 MyClass::Tweet->meta->short_name; # tweet
260  
261 The C<short_name> is used as name for the type. It defaults to the lowercased,
262 last segment of the class name.
263  
264 =head1 METHODS
265  
266 =head2 mapping
267  
268 my $mapping = $document->meta->mapping;
269  
270 Builds the type mapping for this document class. It loads all properties
271 using L</get_all_properties> and calls
272 L<ElasticSearchX::Model::Document::Trait::Attribute/build_property>.
273  
274 =head2 add_property
275  
276 $meta->add_property( name => ( is => 'ro', isa => 'Str' ) )
277  
278 Add a property through the C<$meta> object.
279  
280 =head2 all_properties_loaded
281  
282 Returns true if all properties were acutally loaded from storage
283 or whether C<ElasticSearchX::Model::Set/fields> was used to return
284 a partial result set.
285  
286 =head2 get_id_attribute
287  
288 Get the C<id> attribute, i.e. the attribute that has the C<id> option
289 set. Returns undef if it doesn't exist.
290  
291 =head2 get_parent_attribute
292  
293 Get the C<parent> attribute, i.e. the attribute that has the C<parent> option
294 set. Returns undef if it doesn't exist.
295  
296 =head2 get_all_properties
297  
298 Returns a list of all properties in the document class. An attribute is considered
299 a property, if it I<does> the L<ElasticSearchX::Model::Document::Trait::Attribute>
300 role. That means all attributes that don't have the C<property> option explicitly
301 set to C<0>.
302  
303 Since this method is called quite often, the result is cached if the document class
304 is immutable.
305  
306 =head2 get_data
307  
308 L<ElasticSearchX::Model::Document/put> calls this method to get an HashRef of
309 all properties and their values. Values are deflated if a deflator was specified
310 (e.g. L<DateTime> objects are deflated to an ISO8601 string).
311  
312 =head1 AUTHOR
313  
314 Moritz Onken
315  
316 =head1 COPYRIGHT AND LICENSE
317  
318 This software is Copyright (c) 2018 by Moritz Onken.
319  
320 This is free software, licensed under:
321  
322 The (three-clause) BSD License
323  
324 =cut
« Model/Document/Set.pm ↑ Summary Model/Document/Role.pm »