File Coverage

blib/lib/ElasticSearchX/Model/Document/Role.pm
Criterion Covered Total %
statement 20 58 34.4
branch 3 22 13.6
condition 0 5 0.0
subroutine 5 13 38.4
pod 0 5 0.0
total 28 103 27.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of ElasticSearchX-Model
3             #
4             # This software is Copyright (c) 2019 by Moritz Onken.
5             #
6             # This is free software, licensed under:
7             #
8             # The (three-clause) BSD License
9             #
10             package ElasticSearchX::Model::Document::Role;
11             $ElasticSearchX::Model::Document::Role::VERSION = '2.0.1';
12 7     7   8166 use Moose::Role;
  7         21  
  7         56  
13              
14 7     7   36274 use Carp;
  7         18  
  7         461  
15 7     7   3118 use ElasticSearchX::Model::Util ();
  7         38  
  7         162  
16 7     7   58 use List::MoreUtils ();
  7         16  
  7         5161  
17              
18 0     0   0 sub _does_elasticsearchx_model_document_role {1}
19              
20             has _inflated_attributes =>
21             ( is => 'rw', isa => 'HashRef', lazy => 1, default => sub { {} } );
22              
23             has _loaded_attributes => (
24             is => 'rw',
25             isa => 'HashRef',
26             clearer => '_clear_loaded_attributes',
27             );
28              
29             has index => (
30             isa => 'ElasticSearchX::Model::Index',
31             is => 'rw'
32             );
33              
34             has _id => (
35             is => 'ro',
36             property => 0,
37             source_only => 1,
38             traits => [
39             'ElasticSearchX::Model::Document::Trait::Attribute',
40             'ElasticSearchX::Model::Document::Trait::Field::ID',
41             ],
42             );
43             has _version => (
44             is => 'ro',
45             property => 0,
46             source_only => 1,
47             traits => [
48             'ElasticSearchX::Model::Document::Trait::Attribute',
49             'ElasticSearchX::Model::Document::Trait::Field::Version',
50             ],
51             );
52              
53             sub update {
54 0     0 0 0 my $self = shift;
55 0 0       0 die "cannot update partially loaded document"
56             unless ( $self->meta->all_properties_loaded($self) );
57 0         0 return $self->put( { $self->_update(@_) } );
58             }
59              
60             sub _update {
61 0     0   0 my ( $self, $qs ) = @_;
62 0   0     0 $qs ||= {};
63 0 0       0 return %$qs if ( exists $qs->{version} );
64 0         0 my $version = $self->_version;
65 0 0       0 die "cannot update document without a version"
66             unless ($version);
67             return (
68 0         0 version => $version,
69             %$qs
70             );
71             }
72              
73             sub create {
74 0     0 0 0 my $self = shift;
75 0         0 return $self->put( { $self->_create(@_) } );
76             }
77              
78             sub _create {
79 0     0   0 my ( $self, $qs ) = @_;
80 0         0 my $version = $self->_version;
81             return (
82             create => 1,
83 0 0       0 %{ $qs || {} }
  0         0  
84              
85             );
86             }
87              
88             sub put {
89 0     0 0 0 my ( $self, $qs ) = @_;
90             my $method
91             = $qs
92             && ref $qs eq "HASH"
93 0 0 0     0 && ( delete $qs->{create} ) ? "create" : "index";
94 0         0 my $return = $self->index->model->es->$method( $self->_put($qs) );
95 0         0 $self->_clear_loaded_attributes;
96 0         0 my $id = $self->meta->get_id_attribute;
97 0 0       0 $id->set_value( $self, $return->{_id} ) if ($id);
98 0         0 $self->meta->get_attribute('_id')->set_value( $self, $return->{_id} );
99             $self->meta->get_attribute('_version')
100 0         0 ->set_value( $self, $return->{_version} );
101 0         0 return $self;
102             }
103              
104             sub _put {
105 1     1   3 my ( $self, $qs ) = @_;
106 1         5 my $id = $self->meta->get_id_attribute->get_value($self);
107 1         172 my $parent = $self->meta->get_parent_attribute;
108 1         5 my $data = $self->meta->get_data($self);
109 1 50       3 $qs = { %{ $self->meta->get_query_data($self) }, %{ $qs || {} } };
  1         4  
  1         6  
110             return (
111 1 50       27 index => $self->index->name,
    50          
112             type => $self->meta->short_name,
113             $id ? ( id => $id ) : (),
114             body => $data,
115             $parent ? ( parent => $parent->get_value($self) ) : (),
116             %$qs,
117             );
118             }
119              
120             sub delete {
121 0     0 0   my ( $self, $qs ) = @_;
122 0           my $id = $self->meta->get_id_attribute;
123             my $return = $self->index->model->es->delete(
124             index => $self->index->name,
125             type => $self->meta->short_name,
126             id => $self->_id,
127 0 0         %{ $qs || {} },
  0            
128             );
129 0           return $self;
130             }
131              
132             sub build_id {
133 0     0 0   my $self = shift;
134 0           my $id = $self->meta->get_id_attribute;
135 0 0         carp "Need an arrayref of fields for the id, not " . $id->id
136             unless ( ref $id->id eq 'ARRAY' );
137 0           my @fields = map { $self->meta->get_attribute($_) } @{ $id->id };
  0            
  0            
138 0           return ElasticSearchX::Model::Util::digest( map { $_->deflate($self) }
  0            
139             @fields );
140             }
141              
142             1;
143              
144             __END__
145              
146             =pod
147              
148             =encoding UTF-8
149              
150             =head1 NAME
151              
152             ElasticSearchX::Model::Document::Role
153              
154             =head1 VERSION
155              
156             version 2.0.1
157              
158             =head1 AUTHOR
159              
160             Moritz Onken
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is Copyright (c) 2019 by Moritz Onken.
165              
166             This is free software, licensed under:
167              
168             The (three-clause) BSD License
169              
170             =cut