blib/lib/Tie/MooseObject.pm

filter: Statementstmt 91.1% 134 / 147 Branchbran 71.8% 23 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 97.7% 43 / 44 Podpod n/a totaltotal 89.2% 208 / 233 CC 21 SCAR 9.3
CC
21
Cov
88%
_build__predicate
6.0
_build_rw
6.0
EXISTS
6.0
CRAP 2.5
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Tie::MooseObject;
3 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $Tie::MooseObject::VERSION = '0.0001';
5 } # for Pod::Weaver
6 # ABSTRACT: a tied hash interface to Moose object attributes
7  
8  
9 1 ▶use MooseX::Declare 0.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶class Tie::MooseObject {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 1 ▶ use MooseX::Has::Sugar 0.0405;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use MooseX::Types::Moose 0.12 qw( Bool Str HashRef CodeRef Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶ use Moose::Util::TypeConstraints 1.03 qw( enum );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ use List::Util 1.23 qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 1 has '_reader' => ( isa => HashRef[CodeRef], ro, lazy_build );
20 1 has '_writer' => ( isa => HashRef[CodeRef], ro, lazy_build );
21 1 has '_predicate' => ( isa => HashRef[CodeRef], ro, lazy_build );
22  
23  
24 1 has 'is' => ( isa => enum( [ qw(ro rw) ] ), default => 'ro', rw );
25  
26  
27 1 has 'write_loop' => ( isa => Bool, default => 0, rw );
28  
29  
30 1 has 'object' => ( isa => Object, ro, required );
31  
32  
33 1 ▶ method BUILD {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BUILD: called CC=1 SCAR=0.0
34 2 $self->TIEHASH;
35 1 }
36  
37 1 ▶ method TIEHASH(ClassName|Object $self: @args ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
38 3 ▶ $self = $self->new( @args )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 2 3
39 unless ref $self;
40 3 return $self;
41 1 }
42  
43 1 ▶ method _build__reader() {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 return $self->_build_rw( 'read' );
45 1 }
46  
47 1 ▶ method _build__writer() {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build__writer: called CC=1 SCAR=0.0
48 1 return $self->_build_rw( 'write' );
49 1 }
50  
51 1 ▶ method _build__predicate() {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build__predicate: called CC=6 SCAR=18.0
52 1 my $object = $self->object;
53 1 my $meta = Class::MOP::Class->initialize( ref $object );
54 1 my ( %predicate );
55 1 for ( $meta->get_method_list ) {
56 7 my $method = $meta->get_method( $_ );
57 7 ▶ next unless $method->can( 'associated_attribute' );
Branch: 1 branch
BranchTrueFalseTotal
unless $method->can("associated_attribute") 3 4 7
58 4 my $attr = $method->associated_attribute;
59 0 ▶ my ( $predicate ) = ref( $attr->predicate ) ? %{ $attr->predicate } : $attr->predicate;
Branch: 1 branch
BranchTrueFalseTotal
ref $attr->predicate ? : 0 4 4
60 4 ▶ if ( $predicate and $method->name eq $predicate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicate and $method->name eq $predicate) 1 3 4
Condition100%3/3
$predicate and $method->name eq $predicate
!ll&&!rl&&r
121
MC/DC100%2/2
$predicate and $method->name eq $predicate
$predicate $method->name eq $predicate
Truth table100%3/3
A and B
A= $predicateB= $method->name eq $predicate
ABresult
0X0
100
111
61 1 $predicate{ $predicate } = $method->body;
62 }
63 }
64 1 return \%predicate;
65 1 }
66  
67 1 ▶ method _build_rw( Str $type ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _build_rw: called CC=6 SCAR=17.9
68 2 ▶ my ( $has, $get ) = $type eq 'read'
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'read' ? : 1 1 2
69 ? qw( has_read_method get_read_method )
70 : qw( has_write_method get_write_method );
71 2 my $meta = Class::MOP::Class->initialize( ref $self->object );
72 2 my %return;
73 2 for ( $meta->get_method_list ) {
74 14 my $method = $meta->get_method( $_ );
75 14 ▶ next unless $method->can( 'associated_attribute' );
Branch: 1 branch
BranchTrueFalseTotal
unless $method->can("associated_attribute") 6 8 14
76 8 my $attr = $method->associated_attribute;
77 8 ▶ if ( $attr->$has() && $method->name eq $attr->$get() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->$has and $method->name eq $attr->$get) 4 4 8
Condition66%2/3
$attr->$has and $method->name eq $attr->$get
!ll&&!rl&&r
044
MC/DC50%1/2
$attr->$has and $method->name eq $attr->$get
$attr->$has $method->name eq $attr->$get
Truth table66%2/3
A and B
A= $attr->$hasB= $method->name eq $attr->$get
ABresult
0X0
100
111
78 4 $return{ $method->name } = $method->body;
79 }
80 }
81 2 return \%return;
82 1 }
83  
84  
85 1 ▶ method STORE( Str $key, Any $value ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine STORE: called CC=3 SCAR=11.0
86 5 ▶ croak "Attempt to modify a readonly Moose tied hash"
Branch: 1 branch
BranchTrueFalseTotal
if $self->is eq "ro" 2 3 5
87 if $self->is eq 'ro';
88  
89 3 ▶ if ( exists $self->_writer->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_writer->{$key}) { } 2 1 3
90 2 $self->_writer->{$key}->( $self->object, $value );
91 2 return $value;
92 }
93 else {
94 1 croak "Invalid attempt to call write method $key on $self->object for Moose tied hash";
95 }
96 1 }
97  
98  
99 1 ▶ method FETCH( Str $key ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.4
100 11 ▶ if ( exists $self->_reader->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_reader->{$key}) 11 0 11
101 11 return $self->_reader->{$key}->( $self->object );
102 }
103 0 croak "Invalid attempt to call read method $key on $self->object for Moose tied hash";
104 1 }
105  
106  
107 1 ▶ method FIRSTKEY {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.5
108 6 my $h = $self->_get_loop_hashref;
109 6 my $a = scalar keys %{ $h };
110 6 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 6 6
111 0 my ( $k, $v ) = each %{ $h };
112 0 return ( $k, $v->( $self->object ) );
113 }
114 # else scalar or void context
115 6 return each %{ $h };
116 1 }
117  
118 1 ▶ method NEXTKEY {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine NEXTKEY: called CC=2 SCAR=7.8
119 12 my $h = $self->_get_loop_hashref;
120 12 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 12 12
121 0 my ( $k, $v ) = each %{ $h };
122 0 return ( $k, $v->( $self->object ) );
123 }
124 # else scalar or void context
125 12 return each %{ $h };
126 1 }
127  
128  
129 0 ▶ method SCALAR {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=3.5
130 0 return scalar( keys( %{ $self->_get_loop_hashref } ) );
131 1 }
132  
133  
134 1 ▶ method EXISTS( Str $key ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine EXISTS: called CC=4 SCAR=17.9
135 2 ▶ return $self->_predicate->{$key}->( $self->object ) if exists $self->_predicate->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_predicate->{$key} 0 2 2
136 2 ▶ return exists $self->_reader->{$key} if exists $self->_reader->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_reader->{$key} 2 0 2
137 0 ▶ return exists $self->_writer->{$key} if $self->is eq 'rw';
Branch: 1 branch
BranchTrueFalseTotal
if $self->is eq "rw" 0 0 0
138 0 return;
139 1 }
140  
141  
142 1 ▶ method DELETE( Str $key ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine DELETE: called CC=1 SCAR=0.0
143 1 croak "$self->DELETE not implemented";
144 1 }
145  
146 # override this method if you have some default for clearing the method hash values...
147 1 ▶ method CLEAR {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
148 1 croak "$self->CLEAR not implemented";
149 1 }
150  
151 1 ▶ method _get_loop_hashref {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.1
152 18 ▶ return $self->write_loop
Branch: 1 branch
BranchTrueFalseTotal
$self->write_loop ? : 0 18 18
153 ? $self->_writer
154 : $self->_reader;
155 1 }
156 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 1 1;
159  
160  
161  
162 __END__
↑ Summary