_new for Instance
[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
2e5c1a3f 10our $VERSION = '0.65';
ba38bf08 11our $AUTHORITY = 'cpan:STEVAN';
12
565f0cbb 13use base 'Class::MOP::Method::Generated';
ba38bf08 14
ba38bf08 15sub new {
16 my $class = shift;
17 my %options = @_;
8d2d4c67 18
ba38bf08 19 (exists $options{attribute})
20 || confess "You must supply an attribute to construct with";
8d2d4c67 21
ba38bf08 22 (exists $options{accessor_type})
8d2d4c67 23 || confess "You must supply an accessor_type to construct with";
24
ba38bf08 25 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
8d2d4c67 26 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
27
b38f3848 28 ($options{package_name} && $options{name})
32202ce2 29 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 30
ba38bf08 31 my $self = bless {
32 # from our superclass
8683db0e 33 'body' => undef,
34 'package_name' => $options{package_name},
35 'name' => $options{name},
ba38bf08 36 # specific to this subclass
8683db0e 37 'attribute' => $options{attribute},
38 'is_inline' => ($options{is_inline} || 0),
39 'accessor_type' => $options{accessor_type},
ba38bf08 40 } => $class;
8d2d4c67 41
42 # we don't want this creating
43 # a cycle in the code, if not
ba38bf08 44 # needed
8683db0e 45 weaken($self->{'attribute'});
8d2d4c67 46
565f0cbb 47 $self->initialize_body;
8d2d4c67 48
ba38bf08 49 return $self;
50}
51
52## accessors
53
8683db0e 54sub associated_attribute { (shift)->{'attribute'} }
55sub accessor_type { (shift)->{'accessor_type'} }
ba38bf08 56
8d2d4c67 57## factory
ba38bf08 58
565f0cbb 59sub initialize_body {
ba38bf08 60 my $self = shift;
8d2d4c67 61
ba38bf08 62 my $method_name = join "_" => (
8d2d4c67 63 'generate',
64 $self->accessor_type,
ba38bf08 65 'method',
d90b42a6 66 ($self->is_inline ? 'inline' : ())
ba38bf08 67 );
8d2d4c67 68
8683db0e 69 eval { $self->{'body'} = $self->$method_name() };
ba38bf08 70 die $@ if $@;
71}
72
73## generators
74
75sub generate_accessor_method {
8d2d4c67 76 my $attr = (shift)->associated_attribute;
ba38bf08 77 return sub {
78 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
79 $attr->get_value($_[0]);
80 };
81}
82
83sub generate_reader_method {
8d2d4c67 84 my $attr = (shift)->associated_attribute;
85 return sub {
ba38bf08 86 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
87 $attr->get_value($_[0]);
8d2d4c67 88 };
ba38bf08 89}
90
91sub generate_writer_method {
8d2d4c67 92 my $attr = (shift)->associated_attribute;
ba38bf08 93 return sub {
94 $attr->set_value($_[0], $_[1]);
95 };
96}
97
98sub generate_predicate_method {
8d2d4c67 99 my $attr = (shift)->associated_attribute;
100 return sub {
3545c727 101 $attr->has_value($_[0])
ba38bf08 102 };
103}
104
105sub generate_clearer_method {
8d2d4c67 106 my $attr = (shift)->associated_attribute;
107 return sub {
3545c727 108 $attr->clear_value($_[0])
ba38bf08 109 };
110}
111
112## Inline methods
113
114
115sub generate_accessor_method_inline {
8d2d4c67 116 my $attr = (shift)->associated_attribute;
ba38bf08 117 my $attr_name = $attr->name;
118 my $meta_instance = $attr->associated_class->instance_metaclass;
119
120 my $code = eval 'sub {'
121 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
122 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
123 . '}';
124 confess "Could not generate inline accessor because : $@" if $@;
125
126 return $code;
127}
128
129sub generate_reader_method_inline {
8d2d4c67 130 my $attr = (shift)->associated_attribute;
ba38bf08 131 my $attr_name = $attr->name;
132 my $meta_instance = $attr->associated_class->instance_metaclass;
133
134 my $code = eval 'sub {'
135 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
136 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
137 . '}';
138 confess "Could not generate inline accessor because : $@" if $@;
139
140 return $code;
141}
142
143sub generate_writer_method_inline {
8d2d4c67 144 my $attr = (shift)->associated_attribute;
ba38bf08 145 my $attr_name = $attr->name;
146 my $meta_instance = $attr->associated_class->instance_metaclass;
147
148 my $code = eval 'sub {'
149 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
150 . '}';
151 confess "Could not generate inline accessor because : $@" if $@;
152
153 return $code;
154}
155
156
157sub generate_predicate_method_inline {
8d2d4c67 158 my $attr = (shift)->associated_attribute;
ba38bf08 159 my $attr_name = $attr->name;
160 my $meta_instance = $attr->associated_class->instance_metaclass;
161
8d2d4c67 162 my $code = eval 'sub {' .
163 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
ba38bf08 164 . '}';
165 confess "Could not generate inline predicate because : $@" if $@;
166
167 return $code;
168}
169
170sub generate_clearer_method_inline {
8d2d4c67 171 my $attr = (shift)->associated_attribute;
ba38bf08 172 my $attr_name = $attr->name;
173 my $meta_instance = $attr->associated_class->instance_metaclass;
174
175 my $code = eval 'sub {'
176 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
177 . '}';
178 confess "Could not generate inline clearer because : $@" if $@;
179
180 return $code;
181}
182
1831;
184
185__END__
186
187=pod
188
8d2d4c67 189=head1 NAME
ba38bf08 190
191Class::MOP::Method::Accessor - Method Meta Object for accessors
192
193=head1 SYNOPSIS
194
96e38ba6 195 use Class::MOP::Method::Accessor;
196
197 my $reader = Class::MOP::Method::Accessor->new(
198 attribute => $attribute,
199 is_inline => 1,
200 accessor_type => 'reader',
201 );
8d2d4c67 202
96e38ba6 203 $reader->body->($instance); # call the reader method
ba38bf08 204
205=head1 DESCRIPTION
206
8d2d4c67 207This is a C<Class::MOP::Method> subclass which is used interally
208by C<Class::MOP::Attribute> to generate accessor code. It can
209handle generation of readers, writers, predicate and clearer
96e38ba6 210methods, both as closures and as more optimized inline methods.
211
ba38bf08 212=head1 METHODS
213
214=over 4
215
96e38ba6 216=item B<new (%options)>
ba38bf08 217
8d2d4c67 218This creates the method based on the criteria in C<%options>,
96e38ba6 219these options are:
220
221=over 4
222
223=item I<attribute>
224
8d2d4c67 225This must be an instance of C<Class::MOP::Attribute> which this
96e38ba6 226accessor is being generated for. This paramter is B<required>.
227
228=item I<accessor_type>
229
8d2d4c67 230This is a string from the following set; reader, writer, accessor,
231predicate or clearer. This is used to determine which type of
96e38ba6 232method is to be generated.
233
234=item I<is_inline>
235
236This is a boolean to indicate if the method should be generated
237as a closure, or as a more optimized inline version.
238
239=back
ba38bf08 240
241=item B<accessor_type>
242
96e38ba6 243This returns the accessor type which was passed into C<new>.
244
d90b42a6 245=item B<is_inline>
ba38bf08 246
96e38ba6 247This returns the boolean which was passed into C<new>.
248
ba38bf08 249=item B<associated_attribute>
250
96e38ba6 251This returns the attribute instance which was passed into C<new>.
252
565f0cbb 253=item B<initialize_body>
96e38ba6 254
8d2d4c67 255This will actually generate the method based on the specified
96e38ba6 256criteria passed to the constructor.
257
258=back
259
260=head2 Method Generators
261
8d2d4c67 262These methods will generate appropriate code references for
263the various types of accessors which are supported by
96e38ba6 264C<Class::MOP::Attribute>. The names pretty much explain it all.
265
266=over 4
267
ba38bf08 268=item B<generate_accessor_method>
269
270=item B<generate_accessor_method_inline>
271
272=item B<generate_clearer_method>
273
274=item B<generate_clearer_method_inline>
275
276=item B<generate_predicate_method>
277
278=item B<generate_predicate_method_inline>
279
280=item B<generate_reader_method>
281
282=item B<generate_reader_method_inline>
283
284=item B<generate_writer_method>
285
286=item B<generate_writer_method_inline>
287
288=back
289
290=head1 AUTHORS
291
292Stevan Little E<lt>stevan@iinteractive.comE<gt>
293
ba38bf08 294=head1 COPYRIGHT AND LICENSE
295
69e3ab0a 296Copyright 2006-2008 by Infinity Interactive, Inc.
ba38bf08 297
298L<http://www.iinteractive.com>
299
300This library is free software; you can redistribute it and/or modify
8d2d4c67 301it under the same terms as Perl itself.
ba38bf08 302
303=cut
304