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