Model/Meta/Class/Doc.pm

filter: Statementstmt 50.9% 27 / 53 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 7 / 12 Podpod 50.0% 1 / 2 totaltotal 35.3% 35 / 99 CC 14 SCAR 30.7
CC
14
Cov
34%
_build_unique_keys
56.0
_inflate
20.0
new_stub
12.0
CRAP 21.6
« Model/Role/Model.pm ↑ Summary Model/0_90/Store.pm »
1 package Elastic::Model::Meta::Class::Doc;
2 23 $Elastic::Model::Meta::Class::Doc::VERSION = '0.52';
3 23 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 ▶use MooseX::Types::Moose qw(Maybe HashRef CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use Variable::Magic 0.51 qw(cast wizard dispell);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 23 my $wiz = wizard( map { $_ => \&_inflate } qw(fetch store exists delete) );
11 23 my %exclude = map { $_ => 1 } qw(uid _can_inflate _source);
12  
13 #===================================
14 23 has 'stub_initializer' => (
15 #===================================
16 isa => CodeRef,
17 is => 'ro',
18 lazy => 1,
19 builder => '_build_stub_initializer'
20 );
21  
22 #===================================
23 has 'mapping' => (
24 #===================================
25 isa => HashRef,
26 is => 'rw',
27 338 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
28 23 );
29  
30 #===================================
31 23 has 'unique_keys' => (
32 #===================================
33 is => 'ro',
34 isa => Maybe [HashRef],
35 lazy => 1,
36 builder => '_build_unique_keys'
37 );
38  
39 #===================================
40 has 'inflators' => (
41 #===================================
42 is => 'ro',
43 isa => HashRef,
44 338 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
45 23 );
46  
47 #===================================
48 0 ▶sub new_stub {
Subroutine new_stub: not called CC=3 SCAR=24.8
Pod documented
49 #===================================
50 0 my ( $self, $uid, $source ) = @_;
51  
52 0 my $obj = $self->stub_initializer->();
53  
54 0 $obj->_set_uid($uid);
55 0 ▶ croak "Invalid UID" unless $uid->from_store;
Branch: 1 branch
BranchTrueFalseTotal
unless $uid->from_store 0 0 0
56  
57 0 ▶ $obj->_set_source($source) if $source;
Branch: 1 branch
BranchTrueFalseTotal
if $source 0 0 0
58 0 $obj->_can_inflate(1);
59 0 cast %$obj, $wiz;
60 0 return $obj;
61 }
62  
63 #===================================
64 0 ▶sub _build_unique_keys {
Subroutine _build_unique_keys: not called CC=7 SCAR=40.3
65 #===================================
66 0 my $self = shift;
67 0 my %keys;
68 my %key_names;
69 0 for my $attr ( $self->get_all_attributes ) {
70 0 ▶ next unless $attr->can('unique_key');
Branch: 1 branch
BranchTrueFalseTotal
unless $attr->can("unique_key") 0 0 0
71 0 my $key = $attr->unique_key;
72 0 ▶ next unless defined $key and length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and length $key 0 0 0
Condition0%0/3
defined $key and length $key
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table0%0/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
73  
74 croak "Duplicate unique_key ($key) in class ("
75 . $self->original_class . ')'
76 0 ▶ if $key_names{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if $key_names{$key}++ 0 0 0
77  
78 0 $keys{ $attr->name } = $key;
79 }
80 0 ▶ return %keys ? \%keys : undef;
Branch: 1 branch
BranchTrueFalseTotal
%keys ? : 0 0 0
81 }
82  
83 #===================================
84 0 ▶sub _build_stub_initializer {
Subroutine _build_stub_initializer: not called CC=2 SCAR=17.9
85 #===================================
86 0 my $self = shift;
87 0 my $src = 'sub {'
88 . $self->_inline_generate_instance( '$instance',
89 '"' . $self->name . '"' )
90 . 'return $instance' . '}';
91 0 ▶ return eval($src) || croak $@;
Condition0%0/2
eval $src || croak($@)
l!l
00
MC/DC0%0/1
eval $src || croak($@)
eval $src
Truth table0%0/2
A
A= eval $src
Aresult
11
00
92 }
93  
94 #===================================
95 0 ▶sub _inflate {
Subroutine _inflate: not called CC=4 SCAR=30.0
96 #===================================
97 0 my ( $obj, undef, $key ) = @_;
98 0 ▶ return if $exclude{ $key || '' };
Branch: 1 branch
BranchTrueFalseTotal
if $exclude{$key or ""} 0 0 0
Condition0%0/2
$key || ''
l!l
00
MC/DC0%0/1
$key || ''
$key
Truth table0%0/2
A
A= $key
Aresult
11
00
99  
100 0 dispell %$obj, $wiz;
101 0 ▶ $obj->_inflate_doc if $obj->{_can_inflate};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"_can_inflate"} 0 0 0
102 }
103  
104 #===================================
105 0 ▶sub inflator_for {
Subroutine inflator_for: not called CC=2 SCAR=17.9
Pod undocumented
106 #===================================
107 0 my ( $self, $model, $name ) = @_;
108 0 ▶ $self->inflators->{$name} ||= $model->typemap->find_inflator(
Condition0%0/3
$self->inflators->{$name} ||= $model->typemap->find_inflator($self->find_attribute_by_name($name))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->inflators->{$name} ||= $model->typemap->find_inflator($self->find_attribute_by_name($name))
$self->inflators->{$name} $model->typemap->find_inflator($self->find_attribute_by_name($name))
Truth table0%0/3
A ||= B
A= $self->inflators->{$name}B= $model->typemap->find_inflator($self->find_attribute_by_name($name))
ABresult
1X1
011
000
109 $self->find_attribute_by_name($name) );
110 }
111 23 1;
112  
113 =pod
114  
115 =encoding UTF-8
116  
117 =head1 NAME
118  
119 Elastic::Model::Meta::Class::Doc - A meta-class for Docs
120  
121 =head1 VERSION
122  
123 version 0.52
124  
125 =head1 DESCRIPTION
126  
127 Extends the meta-class for classes which do L<Elastic::Model::Role::Doc>.
128 You shouldn't need to use anything from this class directly.
129  
130 =head1 ATTRIBUTES
131  
132 =head2 mapping
133  
134 \%mapping = $meta->mapping($mapping);
135  
136 Used to store custom mapping config for a class. Use the
137 L<Elastic::Doc/"has_mapping"> sugar instead of calling this method directly.
138  
139 =head1 unique_keys
140  
141 \%unique_keys = $meta->unique_keys
142  
143 Returns a hashref whose keys are the attribute names, and whose values are
144 the value specified in L<Elastic::Manual::Attributes/unique_key>. If there
145 are no unique keys, returns C<undef>.
146  
147 =head1 METHODS
148  
149 =head2 new_stub()
150  
151 $stub_doc = $meta->new_stub($uid);
152 $stub_doc = $meta->new_stub($uid, $source);
153  
154 Creates a stub instance of the class, which auto-inflates when any accessor
155 is called. If the C<$source> param is defined, then it is used to inflate
156 the attributes of the instance, otherwise the attributes are fetched from
157 Elasticsearch when an attribute is accessed.
158  
159 =head1 AUTHOR
160  
161 Clinton Gormley <drtech@cpan.org>
162  
163 =head1 COPYRIGHT AND LICENSE
164  
165 This software is copyright (c) 2015 by Clinton Gormley.
166  
167 This is free software; you can redistribute it and/or modify it under
168 the same terms as the Perl 5 programming language system itself.
169  
170 =cut
171  
172 __END__
« Model/Role/Model.pm ↑ Summary Model/0_90/Store.pm »