| 1 | package Elastic::Model::TypeMap::Objects; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 23 | $Elastic::Model::TypeMap::Objects::VERSION = '0.52'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use Elastic::Model::TypeMap::Base qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use Scalar::Util qw(reftype weaken); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use Moose::Util qw(does_role); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 11 | has_type 'Moose::Meta::TypeConstraint::Class', | ||||||||||||||||||||||||||||||||||||||||||
| 12 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | deflate_via { _deflate_class(@_) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | inflate_via { _inflate_class(@_) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | ▶ | map_via { _map_class(@_) }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | # TODO: Moose role | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 20 | has_type 'Moose::Meta::TypeConstraint::Role', | ||||||||||||||||||||||||||||||||||||||||||
| 21 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | deflate_via {undef}, inflate_via {undef}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 25 | has_type 'Object', | ||||||||||||||||||||||||||||||||||||||||||
| 26 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | deflate_via { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $obj = shift; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | my $ref = ref $obj; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | die "$ref does not provide a deflate() method" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | unless $obj->can('deflate'); | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | return { $ref => $obj->deflate }; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | inflate_via { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my ( $class, $data ) = %{ shift() }; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $inflated = $class->inflate($data); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return bless $inflated, $class; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | }, | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | map_via { type => 'object', enabled => 0 }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _deflate_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deflate_class: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ( $tc, $attr, $map ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $class = $tc->name; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | my $attrs = _class_attrs( $map, $class, $attr ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | return $map->class_deflator( $class, $attrs ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _inflate_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inflate_class: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my ( $tc, $attr, $map ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $class = $tc->name; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ( $map->model->knows_class($class) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $model = $map->model; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | weaken $model; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | die "Missing UID\n" unless $hash->{uid}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $uid = Elastic::Model::UID->new( %{ $hash->{uid} }, | |||||||||||||||||||||||||||||||||||||||||
| 73 | from_store => 1 ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | return $model->get_doc( uid => $uid ); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $attrs = _class_attrs( $map, $class, $attr ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $attr_inflator = $map->class_inflator( $class, $attrs ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | my $hash = shift; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | my $obj = Class::MOP::class_of($class) | |||||||||||||||||||||||||||||||||||||||||
| 86 | ->get_meta_instance->create_instance; | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $attr_inflator->( $obj, $hash ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub _map_class { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_map_class: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my ( $tc, $attr, $map ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return ( type => 'object', enabled => 0 ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$attr->can("has_enabled") and $attr->has_enabled and not $attr->enabled
$attr->can("has_enabled") and $attr->has_enabled
MC/DC0%0/3
$attr->can("has_enabled") and $attr->has_enabled and not $attr->enabled
$attr->can("has_enabled")
$attr->has_enabled
not $attr->enabled
Truth table0%0/4
A and B and not(C)
A= $attr->can("has_enabled")B= $attr->has_enabledC= not $attr->enabled
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | if $attr->can('has_enabled') | ||||||||||||||||||||||||||||||||||||||||||
| 98 | && $attr->has_enabled | ||||||||||||||||||||||||||||||||||||||||||
| 99 | && !$attr->enabled; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $class = $tc->name; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my $attrs = _class_attrs( $map, $class, $attr ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return $map->class_mapping( $class, $attrs ); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub _class_attrs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_class_attrs: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | #=================================== | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my ( $map, $class, $attr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $class = $map->model->class_for($class) || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$map->model->class_for($class) || $class
MC/DC0%0/2
$map->model->class_for($class) || $class
$map->model->class_for($class)
$class
Truth table0%0/3
A || B
A= $map->model->class_for($class)B= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $meta = Class::MOP::class_of($class); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | return unless $meta && $meta->isa('Moose::Meta::Class'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$meta and $meta->isa("Moose::Meta::Class")
MC/DC0%0/2
$meta and $meta->isa("Moose::Meta::Class")
$meta
$meta->isa("Moose::Meta::Class")
Truth table0%0/3
A and B
A= $metaB= $meta->isa("Moose::Meta::Class")
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my %attrs; | |||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $inc = $attr->can('include_attrs') && $attr->include_attrs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$attr->can('include_attrs') && $attr->include_attrs
MC/DC0%0/2
$attr->can('include_attrs') && $attr->include_attrs
$attr->can('include_attrs')
$attr->include_attrs
Truth table0%0/3
A && B
A= $attr->can('include_attrs')B= $attr->include_attrs
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | my $exc = $attr->can('exclude_attrs') && $attr->exclude_attrs; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$attr->can('exclude_attrs') && $attr->exclude_attrs
MC/DC0%0/2
$attr->can('exclude_attrs') && $attr->exclude_attrs
$attr->can('exclude_attrs')
$attr->exclude_attrs
Truth table0%0/3
A && B
A= $attr->can('exclude_attrs')B= $attr->exclude_attrs
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | my @inc_attr = $inc | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ? map { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $meta->find_attribute_by_name($_) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$meta->find_attribute_by_name($_) || die("Unknown attribute ($_) in class $class")
MC/DC0%0/1
$meta->find_attribute_by_name($_) || die("Unknown attribute ($_) in class $class")
$meta->find_attribute_by_name($_)
Truth table0%0/2
A
A= $meta->find_attribute_by_name($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | or die "Unknown attribute ($_) in class $class" | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } @$inc | ||||||||||||||||||||||||||||||||||||||||||
| 128 | : $meta->get_all_attributes; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | %attrs = map { $_->name => $_ } | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | grep { !( $_->can('exclude') && $_->exclude ) } @inc_attr; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_->can('exclude') && $_->exclude
MC/DC0%0/2
$_->can('exclude') && $_->exclude
$_->can('exclude')
$_->exclude
Truth table0%0/3
A && B
A= $_->can('exclude')B= $_->exclude
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | # TODO: does it ever make sense to remove the UID field? | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if ( my $uid = $meta->find_attribute_by_name('uid') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | $attrs{uid} = $uid; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | delete @attrs{@$exc} if $exc; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | return \%attrs; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | # ABSTRACT: Type maps for objects and Moose classes | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||||||||||||||||