Commit | Line | Data |
8cfd8177 |
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 | |
8cfd8177 |
16 | ## generators |
17 | |
8cfd8177 |
18 | sub _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 |
27 | sub _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 | |
44 | 1; |
45 | |
46 | # XXX - UPDATE DOCS |
47 | __END__ |
48 | |
49 | =pod |
50 | |
51 | =head1 NAME |
52 | |
53 | Class::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 | |
69 | This is a subclass of <Class::MOP::Method> which is used by |
70 | C<Class::MOP::Attribute> to generate accessor code. It handles |
71 | generation of readers, writers, predicates and clearers. For each type |
72 | of method, it can either create a subroutine reference, or actually |
73 | inline 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 | |
81 | This returns a new C<Class::MOP::Method::Accessor> based on the |
82 | C<%options> provided. |
83 | |
84 | =over 4 |
85 | |
86 | =item * attribute |
87 | |
88 | This is the C<Class::MOP::Attribute> for which accessors are being |
89 | generated. This option is required. |
90 | |
91 | =item * accessor_type |
92 | |
93 | This is a string which should be one of "reader", "writer", |
94 | "accessor", "predicate", or "clearer". This is the type of method |
95 | being generated. This option is required. |
96 | |
97 | =item * is_inline |
98 | |
99 | This indicates whether or not the accessor should be inlined. This |
100 | defaults to false. |
101 | |
102 | =item * name |
103 | |
104 | The method name (without a package name). This is required. |
105 | |
106 | =item * package_name |
107 | |
108 | The package name for the method. This is required. |
109 | |
110 | =back |
111 | |
112 | =item B<< $metamethod->accessor_type >> |
113 | |
114 | Returns the accessor type which was passed to C<new>. |
115 | |
116 | =item B<< $metamethod->is_inline >> |
117 | |
118 | Returns a boolean indicating whether or not the accessor is inlined. |
119 | |
120 | =item B<< $metamethod->associated_attribute >> |
121 | |
122 | This returns the L<Class::MOP::Attribute> object which was passed to |
123 | C<new>. |
124 | |
125 | =item B<< $metamethod->body >> |
126 | |
127 | The method itself is I<generated> when the accessor object is |
128 | constructed. |
129 | |
130 | =back |
131 | |
132 | =head1 AUTHORS |
133 | |
134 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
135 | |
136 | =head1 COPYRIGHT AND LICENSE |
137 | |
138 | Copyright 2006-2009 by Infinity Interactive, Inc. |
139 | |
140 | L<http://www.iinteractive.com> |
141 | |
142 | This library is free software; you can redistribute it and/or modify |
143 | it under the same terms as Perl itself. |
144 | |
145 | =cut |
146 | |