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