File Coverage

blib/lib/Elastic/Model/Meta/Class/Doc.pm
Criterion Covered Total %
statement 15 41 36.5
branch 0 16 0.0
condition 0 11 0.0
subroutine 5 10 50.0
pod 1 2 50.0
total 21 80 26.2


line stmt bran cond sub pod time code
1             package Elastic::Model::Meta::Class::Doc;
2             $Elastic::Model::Meta::Class::Doc::VERSION = '0.52';
3 23     23   161645 use Moose::Role;
  23         67  
  23         258  
4              
5 23     23   128828 use MooseX::Types::Moose qw(Maybe HashRef CodeRef);
  23         59  
  23         299  
6 23     23   129869 use Carp;
  23         56  
  23         1732  
7 23     23   141 use namespace::autoclean;
  23         57  
  23         268  
8 23     23   2294 use Variable::Magic 0.51 qw(cast wizard dispell);
  23         824  
  23         17836  
9              
10             my $wiz = wizard( map { $_ => \&_inflate } qw(fetch store exists delete) );
11             my %exclude = map { $_ => 1 } qw(uid _can_inflate _source);
12              
13             #===================================
14             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                 default => sub { {} }
28             );
29              
30             #===================================
31             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                 default => sub { {} }
45             );
46              
47             #===================================
48             sub new_stub {
49             #===================================
50 0     0 1       my ( $self, $uid, $source ) = @_;
51              
52 0               my $obj = $self->stub_initializer->();
53              
54 0               $obj->_set_uid($uid);
55 0 0             croak "Invalid UID" unless $uid->from_store;
56              
57 0 0             $obj->_set_source($source) if $source;
58 0               $obj->_can_inflate(1);
59 0               cast %$obj, $wiz;
60 0               return $obj;
61             }
62              
63             #===================================
64             sub _build_unique_keys {
65             #===================================
66 0     0         my $self = shift;
67 0               my %keys;
68                 my %key_names;
69 0               for my $attr ( $self->get_all_attributes ) {
70 0 0                 next unless $attr->can('unique_key');
71 0                   my $key = $attr->unique_key;
72 0 0 0               next unless defined $key and length $key;
73              
74                     croak "Duplicate unique_key ($key) in class ("
75                         . $self->original_class . ')'
76 0 0                     if $key_names{$key}++;
77              
78 0                   $keys{ $attr->name } = $key;
79                 }
80 0 0             return %keys ? \%keys : undef;
81             }
82              
83             #===================================
84             sub _build_stub_initializer {
85             #===================================
86 0     0         my $self = shift;
87 0               my $src = 'sub {'
88                     . $self->_inline_generate_instance( '$instance',
89                     '"' . $self->name . '"' )
90                     . 'return $instance' . '}';
91 0   0           return eval($src) || croak $@;
92             }
93              
94             #===================================
95             sub _inflate {
96             #===================================
97 0     0         my ( $obj, undef, $key ) = @_;
98 0 0 0           return if $exclude{ $key || '' };
99              
100 0               dispell %$obj, $wiz;
101 0 0             $obj->_inflate_doc if $obj->{_can_inflate};
102             }
103              
104             #===================================
105             sub inflator_for {
106             #===================================
107 0     0 0       my ( $self, $model, $name ) = @_;
108 0   0           $self->inflators->{$name} ||= $model->typemap->find_inflator(
109                     $self->find_attribute_by_name($name) );
110             }
111             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__
173            
174             # ABSTRACT: A meta-class for Docs
175            
176