| 1 | package DBIx::NoSQL::Store::Manager::StoreModel; | ||||||||||||||||||||||||||||||||
| 2 | 3 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: trait for attributes linking to store objects. | ||||||||||||||||||||||||||||||||
| 4 | 3 | $DBIx::NoSQL::Store::Manager::StoreModel::VERSION = '1.0.0'; | |||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Log::Any qw/ $log /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 3 | Moose::Util::meta_attribute_alias('StoreModel'); | |||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | 3 | has store_model => ( | |||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 17 | isa => 'Str', | ||||||||||||||||||||||||||||||||
| 18 | required => 1, | ||||||||||||||||||||||||||||||||
| 19 | predicate => 'has_store_model', | ||||||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | has cascade_model => ( | ||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 24 | isa => 'Bool', | ||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 26 | 3 | ); | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | has cascade_save => ( | ||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 30 | isa => 'Bool', | ||||||||||||||||||||||||||||||||
| 31 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { $_[0]->cascade_model }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 33 | 3 | ); | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | has cascade_delete => ( | ||||||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 37 | isa => 'Bool', | ||||||||||||||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | default => sub { $_[0]->cascade_model }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 40 | 3 | ); | |||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use Types::Standard qw/ InstanceOf Str HashRef ArrayRef ConsumerOf /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | before _process_options => sub ( $meta, $name, $options ) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 45 | 0 | my $type = InstanceOf[ $options->{store_model } ] | Str | HashRef; | |||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( grep { $_ eq 'Moose::Meta::Attribute::Native::Trait::Array' } @{ $options->{traits} || [] } ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$options->{'traits'} || []
MC/DC0%0/1
$options->{'traits'} || []
$options->{'traits'}
Truth table0%0/2
A
A= $options->{'traits'}
| |||||||||||||||||||||||||||||||||
| 47 | 0 | $type = 'ArrayRef'; | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $options->{isa} ||= $type; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$options->{"isa"} ||= $type
MC/DC0%0/2
$options->{"isa"} ||= $type
$options->{"isa"}
$type
Truth table0%0/3
A ||= B
A= $options->{"isa"}B= $type
| |||||||||||||||||||||||||||||||||
| 50 | 0 | push @{ $options->{traits} }, 'DoNotSerialize'; | |||||||||||||||||||||||||||||||
| 51 | 3 | }; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | use experimental 'postderef'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _expand_to_object($self,$value,$main_object) { | ||||||||||||||||||||||||||||||
|
Subroutine
_expand_to_object: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return $value if blessed $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | return $self->store_model->new($value) if ref $value; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 0 | my $class = $self->store_model; | |||||||||||||||||||||||||||||||
| 61 | 0 | $class =~ s/^.*::Model:://; | |||||||||||||||||||||||||||||||
| 62 | 0 | $class =~ s/::/_/g; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return $main_object->store_db->get( $class => $value ) | ||||||||||||||||||||||||||||||
|
Condition0%0/2
$main_object->store_db->get($class, $value) || die("'${class}' object with key '${value}' not found\n")
MC/DC0%0/1
$main_object->store_db->get($class, $value) || die("'${class}' object with key '${value}' not found\n")
$main_object->store_db->get($class, $value)
Truth table0%0/2
A
A= $main_object->store_db->get($class, $value)
| |||||||||||||||||||||||||||||||||
| 65 | || die "'$class' object with key '$value' not found\n"; | ||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | after install_accessors => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 69 | 0 | my $attr = shift; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 0 | my $array_context = grep { $_ eq 'Moose::Meta::Attribute::Native::Trait::Array' } @{ $attr->applied_traits }; | |||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 0 | my $reader = $attr->get_read_method; | |||||||||||||||||||||||||||||||
| 74 | # class that has the attribute | ||||||||||||||||||||||||||||||||
| 75 | 0 | my $main_class = $attr->associated_class; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $main_class->add_before_method_modifier( delete => sub ( $self, @) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $obj = $self->$reader or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $_->delete for $array_context ? @$obj : $obj; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | }) if $attr->cascade_delete; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $main_class->add_before_method_modifier( $attr->get_read_method => sub ( $self, @rest ) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | return if @rest; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 0 | my $value = $attr->get_value( $self ); | |||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return unless grep { defined $_ and not blessed $_ } $array_context ? @$value : $value; | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $_ && !builtin::blessed($_)
MC/DC0%0/2
defined $_ && !builtin::blessed($_)
defined $_
!builtin::blessed($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !builtin::blessed($_)
| |||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if( $array_context ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 90 | $attr->set_raw_value( $self, [ map { | ||||||||||||||||||||||||||||||||
| 91 | 0 | $attr->_expand_to_object( $_, $self ) } @$value ] ); | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||
| 94 | 0 | $attr->set_raw_value( $self, $attr->_expand_to_object( $value, $self ) ); | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 0 | }); | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $main_class->add_around_method_modifier( pack => sub($orig,$self) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 100 | 0 | my $packed = $orig->($self); | |||||||||||||||||||||||||||||||
| 101 | 0 | my $val = $self->$reader; | |||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ( $val ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 103 | $packed->{ $attr->name } = $array_context ? [ | ||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | map { $_->store_key } @$val | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | ] : $val->store_key; | ||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | 0 | return $packed; | |||||||||||||||||||||||||||||||
| 108 | 0 | } ); | |||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if( $attr->cascade_save ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | $main_class->add_before_method_modifier( 'save' => sub ( $self, $store=undef ) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||
| 112 | # TODO bug if we remove the value altogether | ||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $value = $self->$reader or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ( $attr->cascade_delete ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 0 | my $priors = eval { $self->store_db->get( $self->store_model, $self->store_key )->$reader }; | |||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ( $array_context ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 0 | my %priors = map { $_->store_key => $_ } @$priors; | |||||||||||||||||||||||||||||||
| 120 | 0 | for ( @$value ) { | |||||||||||||||||||||||||||||||
| 121 | 0 | delete $priors{ $_->store_key }; | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 0 | $_->delete for values %priors; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | else { | ||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( $priors ) { $priors->delete; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $store ||= $self->store_db; | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$store ||= $self->store_db
MC/DC0%0/2
$store ||= $self->store_db
$store
$self->store_db
Truth table0%0/3
A ||= B
A= $storeB= $self->store_db
| |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | for ( $array_context ? @$value : $value ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 133 | 0 | $_->store_db( $store ); | |||||||||||||||||||||||||||||||
| 134 | 0 | $_->save; | |||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | 0 | }); | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 3 | }; | |||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 3 | 1; | |||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||
| 143 | __END__ | ||||||||||||||||||||||||||||||||