Attributes/Trait/Attribute.pm

filter: Statementstmt 82.3% 98 / 119 Branchbran 65.0% 13 / 20 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 75.6% 28 / 37 Podpod n/a totaltotal 73.8% 141 / 191 CC 14 SCAR 11.0
CC
14
Cov
76%
__ANON__
12.0
__ANON__
8.1
install_trackdirty_accessors
4.0
CRAP 3.0
↑ Summary Attributes/Trait/Method/Accessor.pm »
1 #
2 # This file is part of MooseX-TrackDirty-Attributes
3 #
4 # This software is Copyright (c) 2011 by Chris Weyl.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package MooseX::TrackDirty::Attributes::Trait::Attribute;
11 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 $MooseX::TrackDirty::Attributes::Trait::Attribute::AUTHORITY = 'cpan:RSRCHBOY';
13 }
14 8 $MooseX::TrackDirty::Attributes::Trait::Attribute::VERSION = '2.003';
15 # ABSTRACT: Track dirtied attributes
16  
17 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use MooseX::Types::Perl ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use MooseX::AttributeShortcuts 0.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use Moose::Util::MetaRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use MooseX::TrackDirty::Attributes::Util ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use MooseX::TrackDirty::Attributes::Trait::Attribute::Native::Trait ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # roles to help us track / do-the-right-thing when native traits are also used
27 8 Moose::Util::MetaRole::apply_metaroles(
28 for => __PACKAGE__->meta,
29 role_metaroles => {
30 role => [ trait_for 'Role' ],
31 application_to_class => [ ToClass ],
32 application_to_role => [ ToRole ],
33 application_to_instance => [ ToInstance ],
34 },
35 );
36  
37  
38 # debugging
39 #use Smart::Comments '###', '####';
40  
41 8 has is_dirty => (is => 'ro', isa => Identifier, predicate => 1);
42 8 has original_value => (is => 'ro', isa => Identifier, predicate => 1);
43 8 has cleaner => (is => 'ro', isa => Identifier, predicate => 1);
44  
45 # ensure that our is_dirty option is correct, as we apparently cannot rely on
46 # $self->name anymore. *le sigh*
47 7 ▶after _process_options => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
48 7 my ($class, $name, $options) = @_;
49  
50 $options->{is_dirty} = $name . '_is_dirty'
51 7 ▶ unless defined $options->{is_dirty};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options->{"is_dirty"} 6 1 7
52  
53 7 return;
54 8 };
55  
56 8 has value_slot => (is => 'lazy', isa => 'Str');
57 8 has dirty_slot => (is => 'lazy', isa => 'Str');
58  
59 7 ▶sub _build_value_slot { shift->name }
Subroutine _build_value_slot: called CC=1 SCAR=0.0
60 7 ▶sub _build_dirty_slot { shift->name . '__DIRTY_TRACKING__' }
Subroutine _build_dirty_slot: called CC=1 SCAR=0.0
61  
62 11 ▶around slots => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 11 my ($orig, $self) = @_;
64 11 return ($self->$orig(), $self->dirty_slot);
65 8 };
66  
67 0 ▶before set_value => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 0 my ($self, $instance) = @_;
69  
70 0 my $mi = $self->associated_class->get_meta_instance;
71  
72 0 ▶ my $_get = sub { $mi->get_slot_value($instance, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ my $_set = sub { $mi->set_slot_value($instance, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ my $_exists = sub { $mi->is_slot_initialized($instance, @_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
75  
76 0 ▶ $_set->($self->dirty_slot, $_get->($self->value_slot))
Branch: 1 branch
BranchTrueFalseTotal
if &$_exists($self->value_slot) and not &$_exists($self->dirty_slot) 0 0 0
Condition0%0/3
&$_exists($self->value_slot) and not &$_exists($self->dirty_slot)
!ll&&!rl&&r
000
MC/DC0%0/2
&$_exists($self->value_slot) and not &$_exists($self->dirty_slot)
&$_exists($self->value_slot) not &$_exists($self->dirty_slot)
Truth table0%0/3
A and not(B)
A= &$_exists($self->value_slot)B= not &$_exists($self->dirty_slot)
ABresult
0X0
100
111
77 if $_exists->($self->value_slot) && !$_exists->($self->dirty_slot);
78  
79 0 return;
80 8 };
81  
82 2 ▶sub mark_clean { shift->clear_dirty_slot(@_) }
Subroutine mark_clean: called CC=1 SCAR=0.0
83 0 ▶after clear_value => sub { shift->clear_dirty_slot(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
84  
85 4 ▶around _inline_clear_value => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 4 my ($orig, $self) = (shift, shift);
87 4 my ($instance) = @_;
88  
89 4 my $mi = $self->associated_class->get_meta_instance;
90  
91 4 return $self->$orig(@_)
92 . $self->_inline_mark_clean(@_)
93 ;
94 8 };
95  
96 4 ▶sub _inline_mark_clean {
Subroutine _inline_mark_clean: called CC=1 SCAR=0.0
97 4 my ($self, $instance) = @_;
98  
99 4 my $mi = $self->associated_class->get_meta_instance;
100 4 return $mi->inline_deinitialize_slot($instance, $self->dirty_slot);
101 }
102  
103 25 ▶sub _inline_is_dirty_set {
Subroutine _inline_is_dirty_set: called CC=1 SCAR=0.0
104 25 my $self = shift;
105 25 my ($instance, $value) = @_;
106  
107 # set tracker if dirty_slot is not init and value_slot value_slot is
108  
109 25 my $mi = $self->associated_class->get_meta_instance;
110 25 return $mi->inline_set_slot_value($instance, $self->dirty_slot, $value);
111 }
112  
113 0 ▶sub _inline_is_dirty_get {
Subroutine _inline_is_dirty_get: not called CC=1 SCAR=6.9
114 0 my $self = shift;
115 0 my ($instance, $value) = @_;
116  
117 # set tracker if dirty_slot is not init and value_slot value_slot is
118  
119 0 my $mi = $self->associated_class->get_meta_instance;
120 0 return $mi->inline_get_slot_value($instance, $self->dirty_slot, $value);
121 }
122  
123 25 ▶sub _inline_set_dirty_slot_if_dirty {
Subroutine _inline_set_dirty_slot_if_dirty: called CC=2 SCAR=6.9
124 25 my ($self, $instance, $value) = @_;
125 # set dirty_slot from value_slot if dirty_slot is not init and value_slot value_slot is
126  
127 ### $instance
128 ### $value
129  
130 25 my $mi = $self->associated_class->get_meta_instance;
131 25 ▶ my $_exists = sub { $mi->inline_is_slot_initialized($instance, shift) };
Subroutine __ANON__: called CC=1 SCAR=0.0
132  
133 # use our predicate method if we have one, as it may have been wrapped/etc
134 25 ▶ my $value_slot_exists
Branch: 1 branch
BranchTrueFalseTotal
$self->has_predicate ? : 2 23 25
135 = $self->has_predicate
136 ? "${instance}->" . $self->predicate . '()'
137 : $_exists->($self->value_slot)
138 ;
139  
140 25 my $dirty_slot_exists = $_exists->($self->dirty_slot);
141  
142 25 my $set_dirty_slot = $self
143 ->_inline_is_dirty_set(
144 $instance,
145 'do { ' . $mi->inline_get_slot_value($instance, $self->value_slot) . ' } ',
146 )
147 ;
148  
149 25 my $code =
150 "do { $set_dirty_slot } " .
151 " if $value_slot_exists && !$dirty_slot_exists;"
152 ;
153  
154 25 return $code;
155 }
156  
157 17 ▶around _inline_instance_set => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 17 my ($orig, $self) = (shift, shift);
159 17 my ($instance, $value) = @_;
160  
161 17 my $code = $self->_inline_set_dirty_slot_if_dirty(@_);
162 17 $code = "do { $code; " . $self->$orig(@_) . " }";
163  
164 ### $code
165 17 return $code;
166 8 };
167  
168 # TODO remove_accessors
169  
170 0 ▶sub mark_tracking_dirty { shift->set_dirty_slot(@_) }
Subroutine mark_tracking_dirty: not called CC=1 SCAR=6.9
171  
172 0 ▶sub original_value_get { shift->is_dirty_get(@_) }
Subroutine original_value_get: not called CC=1 SCAR=6.9
173  
174 0 ▶sub is_dirty_set {
Subroutine is_dirty_set: not called CC=1 SCAR=6.9
175 0 my ($self, $instance) = @_;
176  
177 0 return $self
178 ->associated_class
179 ->get_meta_instance
180 ->set_slot_value($instance, $self->dirty_slot)
181 ;
182 }
183  
184 10 ▶sub is_dirty_get {
Subroutine is_dirty_get: called CC=1 SCAR=0.0
185 10 my ($self, $instance) = @_;
186  
187 10 return $self
188 ->associated_class
189 ->get_meta_instance
190 ->get_slot_value($instance, $self->dirty_slot)
191 ;
192 }
193  
194 44 ▶sub is_dirty_instance {
Subroutine is_dirty_instance: called CC=1 SCAR=0.0
195 44 my ($self, $instance) = @_;
196  
197 44 return $self
198 ->associated_class
199 ->get_meta_instance
200 ->is_slot_initialized($instance, $self->dirty_slot)
201 ;
202 }
203  
204 2 ▶sub clear_dirty_slot {
Subroutine clear_dirty_slot: called CC=1 SCAR=0.0
205 2 my ($self, $instance) = @_;
206  
207 2 return $self
208 ->associated_class
209 ->get_meta_instance
210 ->deinitialize_slot($instance, $self->dirty_slot)
211 ;
212 }
213  
214 48 ▶around accessor_metaclass => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 48 my ($orig,$self,@args) = @_;
216  
217 48 my $classname = Moose::Meta::Class->create_anon_class(
218 superclasses => [ $self->$orig(@args) ],
219 roles => [ 'MooseX::TrackDirty::Attributes::Trait::Method::Accessor' ],
220 cache => 1,
221 )->name;
222  
223 48 return $classname;
224 8 };
225  
226 9 ▶after install_accessors => sub { shift->install_trackdirty_accessors(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
227  
228 9 ▶sub install_trackdirty_accessors {
Subroutine install_trackdirty_accessors: called CC=4 SCAR=13.9
229 9 my ($self, $inline) = @_;
230 9 my $class = $self->associated_class;
231  
232 ### in install_accessors...
233 9 ▶ $class->add_method(
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_is_dirty 9 0 9
234 $self->_process_accessors(is_dirty => $self->is_dirty, $inline)
235 ) if $self->has_is_dirty;
236 9 ▶ $class->add_method(
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_original_value 2 7 9
237 $self->_process_accessors(original_value => $self->original_value, $inline)
238 ) if $self->has_original_value;
239 9 ▶ $class->add_method(
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cleaner 1 8 9
240 $self->_process_accessors(cleaner => $self->cleaner, $inline)
241 ) if $self->has_cleaner;
242  
243 9 return;
244 };
245  
246 1 ▶before remove_accessors => sub { shift->remove_trackdirty_accessors(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
247  
248 1 ▶sub remove_trackdirty_accessors {
Subroutine remove_trackdirty_accessors: called CC=3 SCAR=12.7
249 1 my $self = shift @_;
250  
251 # stolen from Class::MOP::Attribute
252 1 ▶ my $_remove_accessor = sub {
Subroutine __ANON__: called CC=5 SCAR=20.9
253 1 my ($accessor, $class) = @_;
254 1 ▶ if (ref($accessor) && ref($accessor) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $accessor and ref $accessor eq "HASH") 0 1 1
Condition33%1/3
ref $accessor and ref $accessor eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $accessor and ref $accessor eq "HASH"
ref $accessor ref $accessor eq "HASH"
Truth table33%1/3
A and B
A= ref $accessorB= ref $accessor eq "HASH"
ABresult
0X0
100
111
255 0 ($accessor) = keys %{$accessor};
256 }
257 1 my $method = $class->get_method($accessor);
258 1 ▶ $class->remove_method($accessor)
Branch: 1 branch
BranchTrueFalseTotal
if ref $method and $method->isa("Class::MOP::Method::Accessor") 1 0 1
Condition33%1/3
ref $method and $method->isa("Class::MOP::Method::Accessor")
!ll&&!rl&&r
001
MC/DC0%0/2
ref $method and $method->isa("Class::MOP::Method::Accessor")
ref $method $method->isa("Class::MOP::Method::Accessor")
Truth table33%1/3
A and B
A= ref $methodB= $method->isa("Class::MOP::Method::Accessor")
ABresult
0X0
100
111
259 if (ref($method) && $method->isa('Class::MOP::Method::Accessor'));
260 1 };
261  
262 1 ▶ $_remove_accessor->($self->is_dirty, $self->associated_class) if $self->is_dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_dirty 1 0 1
263 1 ▶ $_remove_accessor->($self->original_value, $self->associated_class) if $self->original_value;
Branch: 1 branch
BranchTrueFalseTotal
if $self->original_value 0 1 1
264  
265 1 return;
266 };
267  
268  
269 8 !!42;
270  
271 __END__
↑ Summary Attributes/Trait/Method/Accessor.pm »