Don't immutalize the meta accessor class to prevent breakage.
[gitmo/MooseX-Emulate-Class-Accessor-Fast.git] / lib / MooseX / Emulate / Class / Accessor / Fast / Meta / Accessor.pm
CommitLineData
b41ad5fb 1package MooseX::Emulate::Class::Accessor::Fast::Meta::Accessor;
2
3use Moose;
4
5extends 'Moose::Meta::Method::Accessor';
6
7sub generate_accessor_method {
8 my $attr = (shift)->associated_attribute;
9 return sub {
10 my $self = shift;
11 $attr->set_value($self, $_[0]) if scalar(@_) == 1;
12 $attr->set_value($self, [@_]) if scalar(@_) > 1;
13 $attr->get_value($self);
14 };
15}
16
17sub generate_writer_method {
18 my $attr = (shift)->associated_attribute;
19 return sub {
20 my $self = shift;
21 $attr->set_value($self, $_[0]) if scalar(@_) == 1;
22 $attr->set_value($self, [@_]) if scalar(@_) > 1;
23 };
24}
25
26# FIXME - this is shite, but it does work...
27sub generate_accessor_method_inline {
28 my $attr = (shift)->associated_attribute;
29 my $attr_name = $attr->name;
30 my $meta_instance = $attr->associated_class->instance_metaclass;
31
32 my $code = eval "sub {
33 my \$self = shift;
34 \$self->{'$attr_name'} = \$_[0] if scalar(\@_) == 1;
35 \$self->{'$attr_name'} = [\@_] if scalar(\@_) > 1;
36 \$self->{'$attr_name'};
37 }";
38 confess "Could not generate inline accessor because : $@" if $@;
39
40 return $code;
41}
42
43{
44 my $meta = __PACKAGE__->meta;
45 $meta->add_method(generate_writer_method_inline => $meta->get_method('generate_accessor_method_inline'));
46}
47
48no Moose;
b41ad5fb 49
501;