Checking in changes prior to tagging of version 0.50_06. Changelog diff is:
[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
e950b355 3use warnings FATAL => 'recursion';
4
3db78d2a 5use Mouse::Meta::Method::Constructor; # for slot access
f8cbb121 6
bb4204b1 7sub _generate_accessor_any{
8 my($method_class, $type, $attribute, $class) = @_;
a41c0667 9
3db78d2a 10 my $c = 'Mouse::Meta::Method::Constructor';
11
12 my $key = $attribute->name;
a41c0667 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
3db78d2a 23 my $instance = '$_[0]';
24 my $slot = $c->_inline_get_slot($instance, $key);;
a41c0667 25
3db78d2a 26 my $accessor = sprintf(<<'END_SUB_START', $class->name, __LINE__, $type, $key, __FILE__);
27package %s;
28#line %d "%s-accessor for %s (%s)
29sub {
30END_SUB_START
2a464664 31
bb4204b1 32 if ($type eq 'rw' || $type eq 'wo') {
33 if($type eq 'rw'){
a41c0667 34 $accessor .=
a41c0667 35 'if (scalar(@_) >= 2) {' . "\n";
36 }
37 else{ # writer
38 $accessor .=
3db78d2a 39 'if(@_ < 2){ Carp::confess("Not enough arguments for the writer of '.$key.'") }'.
a41c0667 40 '{' . "\n";
41 }
42
43 my $value = '$_[1]';
44
14d7595a 45 if (defined $constraint) {
a41c0667 46 if ($should_coerce) {
47 $accessor .=
48 "\n".
ffbbf459 49 'my $val = $constraint->coerce('.$value.');';
a41c0667 50 $value = '$val';
51 }
14d7595a 52 $accessor .=
53 "\n".
cd00d876 54 '$compiled_type_constraint->('.$value.') or
da23cd4a 55 $attribute->_throw_type_constraint_error('.$value.', $constraint);' . "\n";
a41c0667 56 }
57
58 # if there's nothing left to do for the attribute we can return during
59 # this setter
60 $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref;
61
3db78d2a 62 $accessor .= $c->_inline_set_slot($instance, $key, $value) . ";\n";
a41c0667 63
64 if ($is_weak) {
3db78d2a 65 $accessor .= $c->_inline_weaken_slot($instance, $key) ." if ref $slot;\n";
a41c0667 66 }
67
68 if ($trigger) {
3db78d2a 69 $accessor .= '$trigger->('.$instance.', '.$value.');' . "\n";
a41c0667 70 }
71
72 $accessor .= "}\n";
73 }
bb4204b1 74 elsif($type eq 'ro') {
a41c0667 75 $accessor .= 'Carp::confess("Cannot assign a value to a read-only accessor") if scalar(@_) >= 2;' . "\n";
76 }
77 else{
78 $class->throw_error("Unknown accessor type '$type'");
79 }
80
f774b7de 81 if ($attribute->is_lazy) {
cd00d876 82 my $value;
a41c0667 83
cd00d876 84 if (defined $builder){
3db78d2a 85 $value = "$instance->\$builder()";
cd00d876 86 }
87 elsif (ref($default) eq 'CODE'){
3db78d2a 88 $value = "$instance->\$default()";
cd00d876 89 }
90 else{
91 $value = '$default';
14d7595a 92 }
14d7595a 93
3db78d2a 94 $accessor .= sprintf "if(!%s){\n", $c->_inline_has_slot($instance, $key);
cd00d876 95 if($should_coerce){
3db78d2a 96 $value = "\$constraint->coerce($value)";
14d7595a 97 }
95ecd6f1 98 elsif(defined $constraint){
99 $accessor .= "my \$tmp = $value;\n";
b6331c0c 100
95ecd6f1 101 $accessor .= "\$compiled_type_constraint->(\$tmp)";
da23cd4a 102 $accessor .= " || \$attribute->_throw_type_constraint_error(\$tmp, \$constraint);\n";
3db78d2a 103 $value = '$tmp';
95ecd6f1 104 }
3db78d2a 105
106 $accessor .= $c->_inline_set_slot($instance, $key, $value) .";\n";
107
3f2c3dc7 108 if ($is_weak) {
3db78d2a 109 $accessor .= $c->_inline_weaken_slot($instance, $key) . " if ref $slot;\n";
3f2c3dc7 110 }
95ecd6f1 111 $accessor .= "}\n";
a41c0667 112 }
113
114 if ($should_deref) {
115 if ($constraint->is_a_type_of('ArrayRef')) {
cd00d876 116 $accessor .= "return \@{ $slot || [] } if wantarray;\n";
a41c0667 117 }
118 elsif($constraint->is_a_type_of('HashRef')){
cd00d876 119 $accessor .= "return \%{ $slot || {} } if wantarray;\n";
a41c0667 120 }
121 else{
122 $class->throw_error("Can not auto de-reference the type constraint " . $constraint->name);
123 }
124 }
125
cd00d876 126 $accessor .= "return $slot;\n}\n";
a41c0667 127
8aba926d 128 #print $accessor, "\n";
2a464664 129 my $code;
130 my $e = do{
131 local $@;
132 $code = eval $accessor;
133 $@;
134 };
135 die $e if $e;
136
ad087d11 137 return $code;
a41c0667 138}
139
bb4204b1 140sub _generate_accessor{
a41c0667 141 my $class = shift;
bb4204b1 142 return $class->_generate_accessor_any(rw => @_);
a41c0667 143}
144
bb4204b1 145sub _generate_reader {
a41c0667 146 my $class = shift;
bb4204b1 147 return $class->_generate_accessor_any(ro => @_);
a41c0667 148}
149
bb4204b1 150sub _generate_writer {
151 my $class = shift;
152 return $class->_generate_accessor_any(wo => @_);
153}
a41c0667 154
2a464664 155sub _generate_predicate {
4ab51fb0 156 my (undef, $attribute, $class) = @_;
a41c0667 157
7b133c92 158 my $slot = $attribute->name;
4ab51fb0 159 return sub{
7b133c92 160 return exists $_[0]->{$slot};
4ab51fb0 161 };
a41c0667 162}
163
2a464664 164sub _generate_clearer {
4ab51fb0 165 my (undef, $attribute, $class) = @_;
a41c0667 166
7b133c92 167 my $slot = $attribute->name;
bb4204b1 168 return sub{
7b133c92 169 delete $_[0]->{$slot};
4ab51fb0 170 };
a41c0667 171}
172
a41c0667 1731;
0126c27c 174__END__
a25ca8d6 175
176=head1 NAME
177
178Mouse::Meta::Method::Accessor - A Mouse method generator for accessors
179
180=head1 VERSION
181
6e1e5b31 182This document describes Mouse version 0.50_06
a25ca8d6 183
184=head1 SEE ALSO
185
186L<Moose::Meta::Method::Accessor>
187
188=cut