merging the immutable branch into trunk
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
1
2 package Class::MOP::Method::Accessor;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed', 'weaken';
9
10 our $VERSION   = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Class::MOP::Method';
14
15 sub 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},
33         '$!is_inline'     => ($options{is_inline} || 0),
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
49 sub associated_attribute { (shift)->{'$!attribute'}     }
50 sub accessor_type        { (shift)->{'$!accessor_type'} }
51 sub is_inline            { (shift)->{'$!is_inline'}     }
52
53 ## factory 
54
55 sub intialize_body {
56     my $self = shift;
57     
58     my $method_name = join "_" => (
59         'generate', 
60         $self->accessor_type, 
61         'method',
62         ($self->is_inline ? 'inline' : ())
63     );
64     
65     eval { $self->{'&!body'} = $self->$method_name() };
66     die $@ if $@;
67 }
68
69 ## generators
70
71 sub 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
79 sub 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
87 sub generate_writer_method {
88     my $attr = (shift)->associated_attribute; 
89     return sub {
90         $attr->set_value($_[0], $_[1]);
91     };
92 }
93
94 sub generate_predicate_method {
95     my $attr = (shift)->associated_attribute; 
96     return sub { 
97         $attr->has_value($_[0])
98     };
99 }
100
101 sub generate_clearer_method {
102     my $attr = (shift)->associated_attribute; 
103     return sub { 
104         $attr->clear_value($_[0])
105     };
106 }
107
108 ## Inline methods
109
110
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;
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
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;
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
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;
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
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;
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
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;
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
179 1;
180
181 __END__
182
183 =pod
184
185 =head1 NAME 
186
187 Class::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
205 =item B<is_inline>
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
233 Stevan Little E<lt>stevan@iinteractive.comE<gt>
234
235 =head1 COPYRIGHT AND LICENSE
236
237 Copyright 2006 by Infinity Interactive, Inc.
238
239 L<http://www.iinteractive.com>
240
241 This library is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself. 
243
244 =cut
245