| 1 | package DBIx::NoSQL::Model; | ||||||||||
| 2 | |||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 8 | ▶ | use Any::Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 8 | ▶ | use Clone qw/ clone /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 8 | ▶ | use Digest::SHA qw/ sha1_hex /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 8 | has store => qw/ is ro required 1 weak_ref 1 /, handles => [qw/ storage /]; | |||||||||
| 11 | 8 | has name => qw/ reader name writer _name required 1 /; | |||||||||
| 12 | |||||||||||
| 13 | 8 | has inflate => qw/ accessor _inflate isa Maybe[CodeRef] /; | |||||||||
| 14 | 8 | has deflate => qw/ accessor _deflate isa Maybe[CodeRef] /; | |||||||||
| 15 | 0 | ▶ | sub inflator { return shift->_inflate( @_ ) } | ||||||||
|
Subroutine
inflator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 16 | 0 | ▶ | sub deflator { return shift->_deflate( @_ ) } | ||||||||
|
Subroutine
deflator: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 17 | |||||||||||
| 18 | 8 | has wrap => qw/ accessor _wrap /; | |||||||||
| 19 | 14 | ▶ | sub wrapper { return shift->_wrap( @_ ) } | ||||||||
|
Subroutine
wrapper: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 20 | |||||||||||
| 21 | 8 | has field_map => qw/ is ro lazy_build 1 isa HashRef /; | |||||||||
| 22 | 9 | ▶ | sub _build_field_map { {} } | ||||||||
|
Subroutine
_build_field_map: called CC=1 SCAR=0.0
| |||||||||||
| 23 | sub field { | ||||||||||
| 24 | 12 | ▶ | require DBIx::NoSQL::Model::Field; | ||||||||
|
Subroutine
field: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 25 | 12 | my $self = shift; | |||||||||
| 26 | 12 | my $name = shift; | |||||||||
| 27 | |||||||||||
| 28 | 12 | ▶ | return $self->field_map->{ $name } unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 12 | ▶ | die "Already have field ($name)" if $self->field_map->{ $name }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 12 | my $field = $self->field_map->{ $name } = DBIx::NoSQL::Model::Field->new( name => $name ); | |||||||||
| 32 | 12 | $field->setup( $self, @_ ); | |||||||||
| 33 | 12 | return $field; | |||||||||
| 34 | } | ||||||||||
| 35 | 18 | ▶ | has _field2column_map => qw/ is ro /, default => sub { {} }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 18 | ▶ | has _field2inflate_map => qw/ is ro /, default => sub { {} }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 18 | ▶ | has _field2deflate_map => qw/ is ro /, default => sub { {} }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | sub _store_set { | ||||||||||
| 40 | 51 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_store_set: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 51 | return $self->store->schema->resultset( '__Store__' ); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub _find { | ||||||||||
| 45 | 9 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_find: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 9 | my $key = shift; | |||||||||
| 47 | |||||||||||
| 48 | 9 | my $result = $self->_store_set->find( | |||||||||
| 49 | { __model__ => $self->name, __key__ => $key }, | ||||||||||
| 50 | { key => 'primary' }, | ||||||||||
| 51 | ); | ||||||||||
| 52 | |||||||||||
| 53 | 9 | return $result; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | sub create { | ||||||||||
| 57 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 58 | 0 | return $self->create_object( @_ ); | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | sub create_object { | ||||||||||
| 62 | 14 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create_object: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 14 | my $target = shift; | |||||||||
| 64 | |||||||||||
| 65 | 14 | my $data = $self->deserialize( $target ); | |||||||||
| 66 | 14 | my $entity = $self->inflate( $data ); | |||||||||
| 67 | 14 | my $object = $self->wrap( $entity ); | |||||||||
| 68 | |||||||||||
| 69 | 14 | return $object; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub create_entity { | ||||||||||
| 73 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create_entity: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 74 | 0 | my $target = shift; | |||||||||
| 75 | |||||||||||
| 76 | 0 | my $data = $self->deserialize( $target ); | |||||||||
| 77 | 0 | my $entity = $self->inflate( $data ); | |||||||||
| 78 | |||||||||||
| 79 | 0 | return $entity; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | sub create_data { | ||||||||||
| 83 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create_data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 5 | my $target = shift; | |||||||||
| 85 | |||||||||||
| 86 | 5 | my $data = $self->deserialize( $target ); | |||||||||
| 87 | |||||||||||
| 88 | 5 | return $data; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | sub set { | ||||||||||
| 92 | 28 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||
| 93 | 28 | my $key = shift; | |||||||||
| 94 | 28 | my $target = shift; | |||||||||
| 95 | |||||||||||
| 96 | 28 | my ( $entity, $data, $value ); | |||||||||
| 97 | |||||||||||
| 98 | 28 | ▶ | if ( blessed $target ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | $entity = $self->unwrap( $target ); | |||||||||
| 100 | 0 | $target = $entity; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 28 | ▶ | if ( ref $target ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | 28 | $data = $self->deflate( $target ); | |||||||||
| 105 | 28 | $target = $data; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | 28 | $value = $self->serialize( $target ); | |||||||||
| 109 | |||||||||||
| 110 | 28 | $self->_store_set->update_or_create( | |||||||||
| 111 | { __model__ => $self->name, __key__ => $key, __value__ => $value }, | ||||||||||
| 112 | { key => 'primary' }, | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | 28 | ▶ | if ( $self->searchable ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 15 | $self->index->update( $key => $data ); | |||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | sub exists { | ||||||||||
| 121 | 10 | ▶ | my $self = shift; | ||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 122 | 10 | my $key = shift; | |||||||||
| 123 | |||||||||||
| 124 | 10 | return $self->_store_set->search({ __key__ => $key })->count; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | sub get { | ||||||||||
| 128 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 129 | 8 | my $key = shift; | |||||||||
| 130 | |||||||||||
| 131 | 8 | my $result = $self->_find( $key ); | |||||||||
| 132 | |||||||||||
| 133 | 8 | ▶ | return unless $result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | |||||||||||
| 135 | 7 | return $self->create_object( $result->get_column( '__value__' ) ); | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | sub delete { | ||||||||||
| 139 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
delete: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||
| 140 | 1 | my $key = shift; | |||||||||
| 141 | |||||||||||
| 142 | 1 | my $result = $self->_find( $key ); | |||||||||
| 143 | 1 | ▶ | if ( $result ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 1 | $result->delete; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 1 | ▶ | if ( $self->searchable ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 1 | $self->index->delete( $key ); | |||||||||
| 149 | } | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | sub wrap { | ||||||||||
| 153 | 14 | ▶ | my $self = shift; | ||||||||
|
Subroutine
wrap: called CC=3 SCAR=15.0
Pod
undocumented
| |||||||||||
| 154 | 14 | my $entity = shift; | |||||||||
| 155 | |||||||||||
| 156 | 14 | ▶ | if ( my $wrapper = $self->wrapper ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 0 | ▶ | if ( ref $wrapper eq 'CODE' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 0 | return $wrapper->( $entity ); | |||||||||
| 159 | } | ||||||||||
| 160 | else { | ||||||||||
| 161 | 0 | return $wrapper->new( _entity => $entity ); | |||||||||
| 162 | } | ||||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 14 | return $entity; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | sub unwrap { | ||||||||||
| 169 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
unwrap: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 170 | 0 | my $target = shift; | |||||||||
| 171 | |||||||||||
| 172 | 0 | ▶ | return $target->_entity if blessed $target; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | return $target; | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | sub inflate { | ||||||||||
| 177 | 14 | ▶ | my $self = shift; | ||||||||
|
Subroutine
inflate: called CC=3 SCAR=12.3
Pod
undocumented
| |||||||||||
| 178 | 14 | my $data = shift; | |||||||||
| 179 | |||||||||||
| 180 | 14 | my $entity = clone $data; | |||||||||
| 181 | |||||||||||
| 182 | 14 | ▶ | while( my ( $field, $inflator ) = each %{ $self->_field2inflate_map } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 0 | ▶ | $entity->{ $field } = $inflator->( $entity->{ $field } ) if defined $entity->{ $field }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 184 | } | ||||||||||
| 185 | |||||||||||
| 186 | 14 | return $entity; | |||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | sub deflate { | ||||||||||
| 190 | 28 | ▶ | my $self = shift; | ||||||||
|
Subroutine
deflate: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||
| 191 | 28 | my $target = shift; | |||||||||
| 192 | |||||||||||
| 193 | 28 | my $data = {}; | |||||||||
| 194 | |||||||||||
| 195 | 28 | ▶ | while( my ( $field, $deflator ) = each %{ $self->_field2deflate_map } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 196 | 0 | ▶ | $data->{ $field } = $deflator->( $target->{ $field } ) if defined $target->{ $field }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | 28 | ▶ | while( my ( $key, $value ) = each %$target ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | 38 | ▶ | next if exists $data->{ $key }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 201 | 38 | ▶ | $data->{ $key } = ref $value ? clone $value : $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | } | ||||||||||
| 203 | |||||||||||
| 204 | 28 | return $data; | |||||||||
| 205 | } | ||||||||||
| 206 | |||||||||||
| 207 | sub deserialize { | ||||||||||
| 208 | 27 | ▶ | my $self = shift; | ||||||||
|
Subroutine
deserialize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 209 | 27 | my $value = shift; | |||||||||
| 210 | |||||||||||
| 211 | 27 | ▶ | return $value if ref $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | |||||||||||
| 213 | 27 | my $data = $self->store->json->decode( $value ); | |||||||||
| 214 | 27 | return $data; | |||||||||
| 215 | } | ||||||||||
| 216 | |||||||||||
| 217 | sub serialize { | ||||||||||
| 218 | 28 | ▶ | my $self = shift; | ||||||||
|
Subroutine
serialize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 219 | 28 | my $data = shift; | |||||||||
| 220 | |||||||||||
| 221 | 28 | ▶ | return $data if ! ref $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 222 | |||||||||||
| 223 | 28 | my $value = $self->store->json->encode( $data ); | |||||||||
| 224 | 28 | return $value; | |||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | 8 | has searchable => qw/ is rw isa Bool default 1 /; | |||||||||
| 228 | |||||||||||
| 229 | 8 | has index => qw/ reader _index lazy_build 1 /; | |||||||||
| 230 | sub _build_index { | ||||||||||
| 231 | 9 | ▶ | require DBIx::NoSQL::Model::Index; | ||||||||
|
Subroutine
_build_index: called CC=2 SCAR=7.0
| |||||||||||
| 232 | 9 | my $self = shift; | |||||||||
| 233 | 9 | ▶ | return unless $self->searchable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 234 | 9 | return DBIx::NoSQL::Model::Index->new( model => $self ); | |||||||||
| 235 | } | ||||||||||
| 236 | |||||||||||
| 237 | sub index { | ||||||||||
| 238 | 45 | ▶ | my $self = shift; | ||||||||
|
Subroutine
index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 239 | 45 | ▶ | return $self->_index unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 240 | 8 | my $field_name = shift; | |||||||||
| 241 | 8 | return $self->field( $field_name => ( index => 1, @_ ) ); | |||||||||
| 242 | } | ||||||||||
| 243 | |||||||||||
| 244 | sub reindex { | ||||||||||
| 245 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
reindex: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||
| 246 | |||||||||||
| 247 | 2 | ▶ | return unless $self->searchable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 248 | |||||||||||
| 249 | 2 | ▶ | if ( ! $self->index ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 250 | 0 | $self->clear_index; | |||||||||
| 251 | } | ||||||||||
| 252 | |||||||||||
| 253 | 2 | return $self->index->reindex; | |||||||||
| 254 | } | ||||||||||
| 255 | |||||||||||
| 256 | sub search { | ||||||||||
| 257 | 16 | ▶ | my $self = shift; | ||||||||
|
Subroutine
search: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 258 | |||||||||||
| 259 | 16 | ▶ | die "Trying to search on an unsearchable (unindexed) model" unless $self->searchable; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 260 | |||||||||||
| 261 | 16 | return $self->index->search( @_ ); | |||||||||
| 262 | } | ||||||||||
| 263 | |||||||||||
| 264 | 8 | 1; | |||||||||