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