File Coverage

blib/lib/ElasticSearchX/Model/Document/Role.pm
Criterion Covered Total %
statement 23 61 37.7
branch 3 22 13.6
condition 0 5 0.0
subroutine 6 14 42.8
pod 0 5 0.0
total 32 107 29.9


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