foo
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
CommitLineData
ba38bf08 1
2package Class::MOP::Method::Accessor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9
0870928c 10our $VERSION = '0.01';
ba38bf08 11our $AUTHORITY = 'cpan:STEVAN';
12
13use base 'Class::MOP::Method';
14
ba38bf08 15sub new {
16 my $class = shift;
17 my %options = @_;
18
19 (exists $options{attribute})
20 || confess "You must supply an attribute to construct with";
21
22 (exists $options{accessor_type})
23 || confess "You must supply an accessor_type to construct with";
24
25 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
26 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
27
28 my $self = bless {
29 # from our superclass
30 body => undef,
31 # specific to this subclass
32 attribute => $options{attribute},
d90b42a6 33 is_inline => ($options{is_inline} || 0),
ba38bf08 34 accessor_type => $options{accessor_type},
35 } => $class;
36
37 # we don't want this creating
38 # a cycle in the code, if not
39 # needed
40 weaken($self->{attribute});
41
42 $self->intialize_body;
43
44 return $self;
45}
46
47## accessors
48
49sub associated_attribute { (shift)->{attribute} }
50sub accessor_type { (shift)->{accessor_type} }
d90b42a6 51sub is_inline { (shift)->{is_inline} }
ba38bf08 52
53## factory
54
55sub intialize_body {
56 my $self = shift;
57
58 my $method_name = join "_" => (
59 'generate',
60 $self->accessor_type,
61 'method',
d90b42a6 62 ($self->is_inline ? 'inline' : ())
ba38bf08 63 );
64
3545c727 65 eval { $self->{body} = $self->$method_name() };
ba38bf08 66 die $@ if $@;
67}
68
69## generators
70
71sub generate_accessor_method {
72 my $attr = (shift)->associated_attribute;
73 return sub {
74 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
75 $attr->get_value($_[0]);
76 };
77}
78
79sub generate_reader_method {
80 my $attr = (shift)->associated_attribute;
81 return sub {
82 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
83 $attr->get_value($_[0]);
84 };
85}
86
87sub generate_writer_method {
88 my $attr = (shift)->associated_attribute;
89 return sub {
90 $attr->set_value($_[0], $_[1]);
91 };
92}
93
94sub generate_predicate_method {
3545c727 95 my $attr = (shift)->associated_attribute;
ba38bf08 96 return sub {
3545c727 97 $attr->has_value($_[0])
ba38bf08 98 };
99}
100
101sub generate_clearer_method {
3545c727 102 my $attr = (shift)->associated_attribute;
ba38bf08 103 return sub {
3545c727 104 $attr->clear_value($_[0])
ba38bf08 105 };
106}
107
108## Inline methods
109
110
111sub generate_accessor_method_inline {
112 my $attr = (shift)->associated_attribute;
113 my $attr_name = $attr->name;
114 my $meta_instance = $attr->associated_class->instance_metaclass;
115
116 my $code = eval 'sub {'
117 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
118 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
119 . '}';
120 confess "Could not generate inline accessor because : $@" if $@;
121
122 return $code;
123}
124
125sub generate_reader_method_inline {
126 my $attr = (shift)->associated_attribute;
127 my $attr_name = $attr->name;
128 my $meta_instance = $attr->associated_class->instance_metaclass;
129
130 my $code = eval 'sub {'
131 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
132 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
133 . '}';
134 confess "Could not generate inline accessor because : $@" if $@;
135
136 return $code;
137}
138
139sub generate_writer_method_inline {
140 my $attr = (shift)->associated_attribute;
141 my $attr_name = $attr->name;
142 my $meta_instance = $attr->associated_class->instance_metaclass;
143
144 my $code = eval 'sub {'
145 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
146 . '}';
147 confess "Could not generate inline accessor because : $@" if $@;
148
149 return $code;
150}
151
152
153sub generate_predicate_method_inline {
154 my $attr = (shift)->associated_attribute;
155 my $attr_name = $attr->name;
156 my $meta_instance = $attr->associated_class->instance_metaclass;
157
158 my $code = eval 'sub {'
159 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
160 . '}';
161 confess "Could not generate inline predicate because : $@" if $@;
162
163 return $code;
164}
165
166sub generate_clearer_method_inline {
167 my $attr = (shift)->associated_attribute;
168 my $attr_name = $attr->name;
169 my $meta_instance = $attr->associated_class->instance_metaclass;
170
171 my $code = eval 'sub {'
172 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
173 . '}';
174 confess "Could not generate inline clearer because : $@" if $@;
175
176 return $code;
177}
178
1791;
180
181__END__
182
183=pod
184
185=head1 NAME
186
187Class::MOP::Method::Accessor - Method Meta Object for accessors
188
189=head1 SYNOPSIS
190
191 # ... more to come later maybe
192
193=head1 DESCRIPTION
194
195=head1 METHODS
196
197=over 4
198
199=item B<new>
200
201=item B<intialize_body>
202
203=item B<accessor_type>
204
d90b42a6 205=item B<is_inline>
ba38bf08 206
207=item B<associated_attribute>
208
209=item B<generate_accessor_method>
210
211=item B<generate_accessor_method_inline>
212
213=item B<generate_clearer_method>
214
215=item B<generate_clearer_method_inline>
216
217=item B<generate_predicate_method>
218
219=item B<generate_predicate_method_inline>
220
221=item B<generate_reader_method>
222
223=item B<generate_reader_method_inline>
224
225=item B<generate_writer_method>
226
227=item B<generate_writer_method_inline>
228
229=back
230
231=head1 AUTHORS
232
233Stevan Little E<lt>stevan@iinteractive.comE<gt>
234
ba38bf08 235=head1 COPYRIGHT AND LICENSE
236
237Copyright 2006 by Infinity Interactive, Inc.
238
239L<http://www.iinteractive.com>
240
241This library is free software; you can redistribute it and/or modify
242it under the same terms as Perl itself.
243
244=cut
245