blib/lib/DBIx/Class/ElasticSync/Role/ElasticResult.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 105 CC 9 SCAR 28.6
CC
9
Cov
0%
es_delete
42.0
es_index
12.0
es_index_transfer
6.0
CRAP 17.4
1 package DBIx::Class::ElasticSync::Role::ElasticResult;
2  
3 use strict;
4 use warnings;
5  
6 use Moose::Role;
7  
8 =head2
9  
10 dispatches the Result, builds the needed DBIC ResultSet and gives it to the Indexing function
11  
12 =cut
13  
14 sub es_index {
15  
16 my $self = shift;
17  
18 my $schema = $self->result_source->schema;
19 my $class = $self->result_source->source_name;
20  
21 my $elastic_rs = $schema->dispatcher->{$class};
22  
23 for my $rs (@$elastic_rs) {
24  
25 next unless $self->schema->es_is_registered_rs($rs);
26  
27 eval "use $rs";
28  
29 die $@ if $@;
30  
31 my $obj = $self->es_obj_builder($rs);
32 my $dbic_rs = $self->es_dbic_builder( $rs, $obj );
33  
34 $rs->es_index($dbic_rs);
35 }
36 }
37  
38 =head2 es_obj_builder
39  
40 Reloads the Object from the Database to get a clean Row Object
41  
42 =cut
43  
44 sub es_obj_builder {
45  
46 my $self = shift;
47 my $rs = shift;
48  
49 my $class = $self->result_source->source_name;
50  
51 my $obj = $self;
52  
53 # Reload from db
54 $obj->discard_changes;
55  
56 return $obj;
57  
58 }
59  
60 =head2 es_dbic_builder
61  
62 Builds the Resultset for Indexing
63  
64 =cut
65  
66 sub es_dbic_builder {
67  
68 my $self = shift;
69 my $rs = shift;
70 my $obj = shift;
71  
72 my $dbic_rs = $rs->index_rs;
73 my $me = $dbic_rs->current_source_alias;
74 my $dbic_params = { map { $me . "." . $_ => $obj->$_ } $obj->result_source->primary_columns };
75  
76 $dbic_rs = $dbic_rs->search_rs($dbic_params);
77  
78 return $dbic_rs;
79 }
80  
81 =head2 es_delete
82  
83 Dispatches and give the DBIC Resultset to the delete function
84  
85 =cut
86  
87 sub es_delete {
88  
89 my ( $self, $orig ) = @_;
90  
91 my $schema = $self->result_source->schema;
92 my $class = $self->result_source->source_name;
93  
94 my $elastic_rs = $schema->dispatcher->{$class};
95  
96 my $is_primary = 0;
97  
98 for my $rs (@$elastic_rs) {
99  
100 eval "use $rs";
101  
102 die $@ if $@;
103  
104 my $obj = $self->es_obj_builder($rs);
105 my $dbic_rs = $self->es_dbic_builder( $rs, $obj );
106  
107 if ( $rs->es_is_primary($class) ) {
108  
109 $rs->es_delete($dbic_rs);
110 $is_primary = 1;
111  
112 } elsif ( $rs->es_is_nested($class) ) {
113  
114 $self->$orig if $self->in_storage;
115 $rs->es_index($dbic_rs);
116 }
117 }
118  
119 $self->$orig if $is_primary;
120 }
121  
122 after 'insert' => sub {
123 my $self = shift;
124  
125 warn "Inserting ...\n";
126 $self->es_index;
127 };
128  
129 after 'update' => sub {
130 my $self = shift;
131  
132 warn "Updating ...\n";
133 $self->es_index;
134 };
135  
136 around 'delete' => sub {
137 my $orig = shift;
138 my $self = shift;
139  
140 warn "Deleting...\n";
141 $self->es_delete($orig);
142 };
143  
144 sub es_index_transfer {
145  
146 my ( $self, $body ) = @_;
147  
148 my $type = $self->result_source->name;
149  
150 my $parent = {};
151 if ( $self->es_is_child ) {
152  
153 $parent = { parent => $self->es_parent };
154 }
155  
156 }
157  
158 sub schema {
159  
160 return shift->result_source->schema;
161 }
162  
163 sub es {
164  
165 return shift->schema->es;
166 }
167  
168 1;