Add meta() method to method metaclasses
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Accessor.pm
CommitLineData
a41c0667 1package Mouse::Meta::Method::Accessor;
3821b191 2use Mouse::Util qw(:meta); # enables strict and warnings
a41c0667 3
f8cbb121 4sub _inline_slot{
5 my(undef, $self_var, $attr_name) = @_;
6 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
7}
8
2a464664 9sub _generate_accessor{
f8cbb121 10 my ($method_class, $attribute, $class, $type) = @_;
a41c0667 11
12 my $name = $attribute->name;
13 my $default = $attribute->default;
14 my $constraint = $attribute->type_constraint;
15 my $builder = $attribute->builder;
16 my $trigger = $attribute->trigger;
17 my $is_weak = $attribute->is_weak_ref;
18 my $should_deref = $attribute->should_auto_deref;
cd00d876 19 my $should_coerce = (defined($constraint) && $constraint->has_coercion && $attribute->should_coerce);
a41c0667 20
cd00d876 21 my $compiled_type_constraint = defined($constraint) ? $constraint->_compiled_type_constraint : undef;
a41c0667 22
23 my $self = '$_[0]';
f8cbb121 24 my $slot = $method_class->_inline_slot($self, $name);;
a41c0667 25
26 $type ||= 'accessor';
27
8aba926d 28 my $accessor = sprintf(qq{package %s;\n#line 1 "%s for %s (%s)"\n}, $class->name, $type, $name, __FILE__)
cd00d876 29 . "sub {\n";
2a464664 30
a41c0667 31 if ($type eq 'accessor' || $type eq 'writer') {
32 if($type eq 'accessor'){
33 $accessor .=
a41c0667 34 'if (scalar(@_) >= 2) {' . "\n";
35 }
36 else{ # writer
37 $accessor .=
2a464664 38 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$name.'") }'.
a41c0667 39 '{' . "\n";
40 }
41
42 my $value = '$_[1]';
43
14d7595a 44 if (defined $constraint) {
a41c0667 45 if ($should_coerce) {
46 $accessor .=
47 "\n".
ffbbf459 48 'my $val = $constraint->coerce('.$value.');';
a41c0667 49 $value = '$val';
50 }
14d7595a 51 $accessor .=
52 "\n".
cd00d876 53 '$compiled_type_constraint->('.$value.') or
54 $attribute->verify_type_constraint_error($name, '.$value.', $constraint);' . "\n";
a41c0667 55 }
56
57 # if there's nothing left to do for the attribute we can return during
58 # this setter
59 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
60
cd00d876 61 $accessor .= "$slot = $value;\n";
a41c0667 62
63 if ($is_weak) {
cd00d876 64 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
a41c0667 65 }
66
67 if ($trigger) {
68 $accessor .= '$trigger->('.$self.', '.$value.');' . "\n";
69 }
70
71 $accessor .= "}\n";
72 }
73 elsif($type eq 'reader') {
74 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
75 }
76 else{
77 $class->throw_error("Unknown accessor type '$type'");
78 }
79
f774b7de 80 if ($attribute->is_lazy) {
cd00d876 81 my $value;
a41c0667 82
cd00d876 83 if (defined $builder){
84 $value = "$self->\$builder()";
85 }
86 elsif (ref($default) eq 'CODE'){
87 $value = "$self->\$default()";
88 }
89 else{
90 $value = '$default';
14d7595a 91 }
14d7595a 92
95ecd6f1 93 $accessor .= "if(!exists $slot){\n";
cd00d876 94 if($should_coerce){
95ecd6f1 95 $accessor .= "$slot = \$constraint->coerce($value)";
14d7595a 96 }
95ecd6f1 97 elsif(defined $constraint){
98 $accessor .= "my \$tmp = $value;\n";
b6331c0c 99
95ecd6f1 100 $accessor .= "\$compiled_type_constraint->(\$tmp)";
b6331c0c 101 $accessor .= " || \$attribute->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n";
95ecd6f1 102 $accessor .= "$slot = \$tmp;\n";
103 }
104 else{
105 $accessor .= "$slot = $value;\n";
106 }
3f2c3dc7 107 if ($is_weak) {
108 $accessor .= "Scalar::Util::weaken($slot) if ref $slot;\n";
109 }
95ecd6f1 110 $accessor .= "}\n";
a41c0667 111 }
112
113 if ($should_deref) {
114 if ($constraint->is_a_type_of('ArrayRef')) {
cd00d876 115 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
a41c0667 116 }
117 elsif($constraint->is_a_type_of('HashRef')){
cd00d876 118 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
a41c0667 119 }
120 else{
121 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
122 }
123 }
124
cd00d876 125 $accessor .= "return $slot;\n}\n";
a41c0667 126
8aba926d 127 #print $accessor, "\n";
2a464664 128 my $code;
129 my $e = do{
130 local $@;
131 $code = eval $accessor;
132 $@;
133 };
134 die $e if $e;
135
ad087d11 136 return $code;
a41c0667 137}
138
2a464664 139sub _generate_reader{
a41c0667 140 my $class = shift;
2a464664 141 return $class->_generate_accessor(@_, 'reader');
a41c0667 142}
143
2a464664 144sub _generate_writer{
a41c0667 145 my $class = shift;
2a464664 146 return $class->_generate_accessor(@_, 'writer');
a41c0667 147}
148
149
2a464664 150sub _generate_predicate {
4ab51fb0 151 my (undef, $attribute, $class) = @_;
a41c0667 152
7b133c92 153 my $slot = $attribute->name;
4ab51fb0 154 return sub{
7b133c92 155 return exists $_[0]->{$slot};
4ab51fb0 156 };
a41c0667 157}
158
2a464664 159sub _generate_clearer {
4ab51fb0 160 my (undef, $attribute, $class) = @_;
a41c0667 161
7b133c92 162 my $slot = $attribute->name;
a41c0667 163
4ab51fb0 164 return sub{
7b133c92 165 delete $_[0]->{$slot};
4ab51fb0 166 };
a41c0667 167}
168
a41c0667 1691;
0126c27c 170__END__
a25ca8d6 171
172=head1 NAME
173
174Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
175
176=head1 VERSION
177
f7e41eda 178This document describes Mouse version 0.40_08
a25ca8d6 179
180=head1 SEE ALSO
181
182L<Moose::Meta::Method::Accessor>
183
184=cut