-
Notifications
You must be signed in to change notification settings - Fork 36
/
attribute.pm
279 lines (205 loc) · 7.97 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
271
272
273
274
275
276
277
278
279
package mop::attribute;
use v5.16;
use warnings;
use Scalar::Util qw[ weaken isweak ];
use mop::internals::util;
our $VERSION = '0.02';
our $AUTHORITY = 'cpan:STEVAN';
use parent 'mop::object', 'mop::internals::observable';
mop::internals::util::init_attribute_storage(my %name);
mop::internals::util::init_attribute_storage(my %default);
mop::internals::util::init_attribute_storage(my %associated_meta);
mop::internals::util::init_attribute_storage(my %original_id);
mop::internals::util::init_attribute_storage(my %storage);
sub name { ${ $name{ $_[0] } // \undef } }
sub associated_meta { ${ $associated_meta{ $_[0] } // \undef } }
sub set_associated_meta {
my ($self, $meta) = @_;
$associated_meta{ $self } = \$meta;
weaken(${ $associated_meta{ $self } });
}
# temporary, for bootstrapping
sub new {
my $class = shift;
my %args = @_;
my $self = $class->SUPER::new;
$name{ $self } = \($args{'name'});
$default{ $self } = \($args{'default'}) if exists $args{'default'};
$storage{ $self } = \($args{'storage'}) if exists $args{'storage'};
# NOTE:
# keep track of the original ID here
# so that we can still detect attribute
# conflicts in roles even after something
# has been cloned
# - SL
$original_id{ $self } = \(mop::id($self));
$self
}
sub BUILD {
my $self = shift;
return unless $default{ $self };
my $value = ${ $default{ $self } };
if ( ref $value && ref $value ne 'CODE' ) {
die "References of type (" . ref($value) . ") are not supported as attribute defaults (in attribute " . $self->name . ($self->associated_meta ? " in class " . $self->associated_meta->name : "") . ")";
}
}
# temporary, for bootstrapping
sub clone {
my $self = shift;
return ref($self)->new(
name => $self->name,
default => ${ $default{ $self } },
storage => ${ $storage{ $self } },
);
}
sub key_name {
my $self = shift;
substr( $self->name, 2, length $self->name )
}
sub has_default { defined( ${ $default{ $_[0] } } ) }
sub set_default {
my $self = shift;
my ($value) = @_;
if ( ref $value && ref $value ne 'CODE' ) {
die "References of type (" . ref($value) . ") are not supported as attribute defaults (in attribute " . $self->name . ($self->associated_meta ? " in class " . $self->associated_meta->name : "") . ")";
}
$default{ $self } = \$value
}
sub clear_default { ${ delete $default{ $_[0] } } }
sub get_default {
my $self = shift;
my $value = ${ $default{ $self } };
if ( ref $value && ref $value eq 'CODE' ) {
$value = $value->();
}
$value
}
sub conflicts_with { ${ $original_id{ $_[0] } } ne ${ $original_id{ $_[1] } } }
sub locally_defined { ${ $original_id{ $_[0] } } eq mop::id( $_[0] ) }
sub has_data_in_slot_for {
my ($self, $instance) = @_;
defined ${ ${ $storage{ $self } }->{ $instance } };
}
sub fetch_data_in_slot_for {
my ($self, $instance) = @_;
$self->fire('before:FETCH_DATA', $instance);
my $val = ${ ${ $storage{ $self } }->{ $instance } || \undef };
$self->fire('after:FETCH_DATA', $instance);
return $val;
}
sub store_data_in_slot_for {
my ($self, $instance, $data) = @_;
$self->fire('before:STORE_DATA', $instance, \$data);
${ $storage{ $self } }->{ $instance } = \$data;
$self->fire('after:STORE_DATA', $instance, \$data);
return;
}
sub store_default_in_slot_for {
my ($self, $instance) = @_;
$self->store_data_in_slot_for($instance, do {
local $_ = $instance;
$self->get_default;
}) if $self->has_default;
}
sub weaken_data_in_slot_for {
my ($self, $instance) = @_;
weaken(${ ${ $storage{ $self } }->{ $instance } });
}
sub is_data_in_slot_weak_for {
my ($self, $instance) = @_;
isweak(${ ${ $storage{ $self } }->{ $instance } });
}
sub __INIT_METACLASS__ {
my $METACLASS = mop::class->new(
name => 'mop::attribute',
version => $VERSION,
authority => $AUTHORITY,
superclass => 'mop::object',
);
$METACLASS->add_attribute(mop::attribute->new(
name => '$!name',
storage => \%name,
));
$METACLASS->add_attribute(mop::attribute->new(
name => '$!default',
storage => \%default,
));
$METACLASS->add_attribute(mop::attribute->new(
name => '$!associated_meta',
storage => \%associated_meta,
));
$METACLASS->add_attribute(mop::attribute->new(
name => '$!original_id',
storage => \%original_id,
default => sub { mop::id($_) },
));
$METACLASS->add_attribute(mop::attribute->new(
name => '$!storage',
storage => \%storage,
default => sub { mop::internals::util::init_attribute_storage(my %x) },
));
$METACLASS->add_method( mop::method->new( name => 'BUILD', body => \&BUILD ) );
$METACLASS->add_method( mop::method->new( name => 'name', body => \&name ) );
$METACLASS->add_method( mop::method->new( name => 'key_name', body => \&key_name ) );
$METACLASS->add_method( mop::method->new( name => 'has_default', body => \&has_default ) );
$METACLASS->add_method( mop::method->new( name => 'get_default', body => \&get_default ) );
$METACLASS->add_method( mop::method->new( name => 'set_default', body => \&set_default ) );
$METACLASS->add_method( mop::method->new( name => 'clear_default', body => \&clear_default ) );
$METACLASS->add_method( mop::method->new( name => 'associated_meta', body => \&associated_meta ) );
$METACLASS->add_method( mop::method->new( name => 'set_associated_meta', body => \&set_associated_meta ) );
$METACLASS->add_method( mop::method->new( name => 'conflicts_with', body => \&conflicts_with ) );
$METACLASS->add_method( mop::method->new( name => 'locally_defined', body => \&locally_defined ) );
$METACLASS->add_method( mop::method->new( name => 'has_data_in_slot_for', body => \&has_data_in_slot_for ) );
$METACLASS->add_method( mop::method->new( name => 'fetch_data_in_slot_for', body => \&fetch_data_in_slot_for ) );
$METACLASS->add_method( mop::method->new( name => 'store_data_in_slot_for', body => \&store_data_in_slot_for ) );
$METACLASS->add_method( mop::method->new( name => 'store_default_in_slot_for', body => \&store_default_in_slot_for ) );
$METACLASS->add_method( mop::method->new( name => 'weaken_data_in_slot_for', body => \&weaken_data_in_slot_for ) );
$METACLASS->add_method( mop::method->new( name => 'is_data_in_slot_weak_for', body => \&is_data_in_slot_weak_for ) );
$METACLASS;
}
1;
__END__
=pod
=head1 NAME
mop::attribute - A meta-object to represent attributes
=head1 DESCRIPTION
TODO
=head1 METHODS
=over 4
=item C<BUILD>
=item C<name>
=item C<key_name>
=item C<has_default>
=item C<get_default>
=item C<set_default($default)>
=item C<clear_default>
=item C<associated_meta>
=item C<set_associated_meta($meta)>
=item C<conflicts_with($obj)>
=item C<locally_defined>
=item C<has_data_in_slot_for($instance)>
=item C<fetch_data_in_slot_for($instance)>
=item C<store_data_in_slot_for($instance, $data)>
=item C<store_default_in_slot_for($instance)>
=item C<weaken_data_in_slot_for($instance)>
=item C<is_data_in_slot_weak_for($instance)>
=back
=head1 SEE ALSO
=head2 L<Attribute Details|mop::manual::details::attributes>
=head1 BUGS
Since this module is still under development we would prefer to not
use the RT bug queue and instead use the built in issue tracker on
L<Github|http://www.github.com>.
=head2 L<Git Repository|https://github.com/stevan/p5-mop-redux>
=head2 L<Issue Tracker|https://github.com/stevan/p5-mop-redux/issues>
=head1 AUTHOR
Stevan Little <stevan.little@iinteractive.com>
Jesse Luehrs <doy@tozt.net>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Infinity Interactive.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=for Pod::Coverage
new
clone
=cut