Add split out Writer method
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Writer.pm
1
2 package Class::MOP::Method::Writer;
3
4 use strict;
5 use warnings;
6
7 use Carp         'confess';
8 use Scalar::Util 'blessed', 'weaken';
9
10 our $VERSION   = '0.88';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use base 'Class::MOP::Method::Attribute';
15
16 sub _initialize_body {
17     my $self = shift;
18
19     my $method_name = join "_" => (
20         '_generate',
21         'method',
22         ($self->is_inline ? 'inline' : ())
23     );
24
25     $self->{'body'} = $self->$method_name();
26 }
27
28 ## generators
29
30 sub generate_method {
31     Carp::cluck('The generate_writer_method method has been made private.'
32         . " The public version is deprecated and will be removed in a future release.\n");
33     shift->_generate_method;
34 }
35
36 sub _generate_method {
37     my $attr = (shift)->associated_attribute;
38     return sub {
39         $attr->set_value($_[0], $_[1]);
40     };
41 }
42
43 ## Inline methods
44
45 sub generate_method_inline {
46     Carp::cluck('The generate_writer_method_inline method has been made private.'
47         . " The public version is deprecated and will be removed in a future release.\n");
48     shift->_generate_method_inline;
49 }
50
51 sub _generate_method_inline {
52     my $self          = shift;
53     my $attr          = $self->associated_attribute;
54     my $attr_name     = $attr->name;
55     my $meta_instance = $attr->associated_class->instance_metaclass;
56
57     my ( $code, $e ) = $self->_eval_closure(
58         {},
59         'sub {'
60         . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
61         . '}'
62     );
63     confess "Could not generate inline writer because : $e" if $e;
64
65     return $code;
66 }
67
68 1;
69
70 # XXX - UPDATE DOCS
71 __END__
72
73 =pod
74
75 =head1 NAME
76
77 Class::MOP::Method::Writer - Method Meta Object for accessors
78
79 =head1 SYNOPSIS
80
81     use Class::MOP::Method::Accessor;
82
83     my $reader = Class::MOP::Method::Accessor->new(
84         attribute     => $attribute,
85         is_inline     => 1,
86         accessor_type => 'reader',
87     );
88
89     $reader->body->execute($instance); # call the reader method
90
91 =head1 DESCRIPTION
92
93 This is a subclass of <Class::MOP::Method> which is used by
94 C<Class::MOP::Attribute> to generate accessor code. It handles
95 generation of readers, writers, predicates and clearers. For each type
96 of method, it can either create a subroutine reference, or actually
97 inline code by generating a string and C<eval>'ing it.
98
99 =head1 METHODS
100
101 =over 4
102
103 =item B<< Class::MOP::Method::Accessor->new(%options) >>
104
105 This returns a new C<Class::MOP::Method::Accessor> based on the
106 C<%options> provided.
107
108 =over 4
109
110 =item * attribute
111
112 This is the C<Class::MOP::Attribute> for which accessors are being
113 generated. This option is required.
114
115 =item * accessor_type
116
117 This is a string which should be one of "reader", "writer",
118 "accessor", "predicate", or "clearer". This is the type of method
119 being generated. This option is required.
120
121 =item * is_inline
122
123 This indicates whether or not the accessor should be inlined. This
124 defaults to false.
125
126 =item * name
127
128 The method name (without a package name). This is required.
129
130 =item * package_name
131
132 The package name for the method. This is required.
133
134 =back
135
136 =item B<< $metamethod->accessor_type >>
137
138 Returns the accessor type which was passed to C<new>.
139
140 =item B<< $metamethod->is_inline >>
141
142 Returns a boolean indicating whether or not the accessor is inlined.
143
144 =item B<< $metamethod->associated_attribute >>
145
146 This returns the L<Class::MOP::Attribute> object which was passed to
147 C<new>.
148
149 =item B<< $metamethod->body >>
150
151 The method itself is I<generated> when the accessor object is
152 constructed.
153
154 =back
155
156 =head1 AUTHORS
157
158 Stevan Little E<lt>stevan@iinteractive.comE<gt>
159
160 =head1 COPYRIGHT AND LICENSE
161
162 Copyright 2006-2009 by Infinity Interactive, Inc.
163
164 L<http://www.iinteractive.com>
165
166 This library is free software; you can redistribute it and/or modify
167 it under the same terms as Perl itself.
168
169 =cut
170