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