2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method';
19 (exists $options{attribute})
20 || confess "You must supply an attribute to construct with";
22 (exists $options{accessor_type})
23 || confess "You must supply an accessor_type to construct with";
25 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
26 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
31 # specific to this subclass
32 attribute => $options{attribute},
33 is_inline => ($options{is_inline} || 0),
34 accessor_type => $options{accessor_type},
37 # we don't want this creating
38 # a cycle in the code, if not
40 weaken($self->{attribute});
42 $self->intialize_body;
49 sub associated_attribute { (shift)->{attribute} }
50 sub accessor_type { (shift)->{accessor_type} }
51 sub is_inline { (shift)->{is_inline} }
58 my $method_name = join "_" => (
62 ($self->is_inline ? 'inline' : ())
65 eval { $self->{body} = $self->$method_name() };
71 sub generate_accessor_method {
72 my $attr = (shift)->associated_attribute;
74 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
75 $attr->get_value($_[0]);
79 sub generate_reader_method {
80 my $attr = (shift)->associated_attribute;
82 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
83 $attr->get_value($_[0]);
87 sub generate_writer_method {
88 my $attr = (shift)->associated_attribute;
90 $attr->set_value($_[0], $_[1]);
94 sub generate_predicate_method {
95 my $attr = (shift)->associated_attribute;
97 $attr->has_value($_[0])
101 sub generate_clearer_method {
102 my $attr = (shift)->associated_attribute;
104 $attr->clear_value($_[0])
111 sub 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;
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'")
120 confess "Could not generate inline accessor because : $@" if $@;
125 sub 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;
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'")
134 confess "Could not generate inline accessor because : $@" if $@;
139 sub 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;
144 my $code = eval 'sub {'
145 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
147 confess "Could not generate inline accessor because : $@" if $@;
153 sub 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;
158 my $code = eval 'sub {'
159 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
161 confess "Could not generate inline predicate because : $@" if $@;
166 sub 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;
171 my $code = eval 'sub {'
172 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
174 confess "Could not generate inline clearer because : $@" if $@;
187 Class::MOP::Method::Accessor - Method Meta Object for accessors
191 # ... more to come later maybe
201 =item B<intialize_body>
203 =item B<accessor_type>
207 =item B<associated_attribute>
209 =item B<generate_accessor_method>
211 =item B<generate_accessor_method_inline>
213 =item B<generate_clearer_method>
215 =item B<generate_clearer_method_inline>
217 =item B<generate_predicate_method>
219 =item B<generate_predicate_method_inline>
221 =item B<generate_reader_method>
223 =item B<generate_reader_method_inline>
225 =item B<generate_writer_method>
227 =item B<generate_writer_method_inline>
233 Stevan Little E<lt>stevan@iinteractive.comE<gt>
235 =head1 COPYRIGHT AND LICENSE
237 Copyright 2006 by Infinity Interactive, Inc.
239 L<http://www.iinteractive.com>
241 This library is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself.