Use "undef" and avoid uninitialized warnings when the value we're type checking is...
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
1 #!/usr/bin/env perl
2 package Mouse::Attribute;
3 use strict;
4 use warnings;
5
6 use Carp 'confess';
7
8 sub new {
9     my $class = shift;
10     my %args  = @_;
11
12     $args{init_arg} = $args{name}
13         unless exists $args{init_arg};
14     $args{is} ||= '';
15
16     bless \%args, $class;
17 }
18
19 sub name            { $_[0]->{name}            }
20 sub class           { $_[0]->{class}           }
21 sub default         { $_[0]->{default}         }
22 sub predicate       { $_[0]->{predicate}       }
23 sub clearer         { $_[0]->{clearer}         }
24 sub handles         { $_[0]->{handles}         }
25 sub weak_ref        { $_[0]->{weak_ref}        }
26 sub init_arg        { $_[0]->{init_arg}        }
27 sub type_constraint { $_[0]->{type_constraint} }
28
29 sub has_name            { exists $_[0]->{name}            }
30 sub has_class           { exists $_[0]->{class}           }
31 sub has_default         { exists $_[0]->{default}         }
32 sub has_predicate       { exists $_[0]->{predicate}       }
33 sub has_clearer         { exists $_[0]->{clearer}         }
34 sub has_handles         { exists $_[0]->{handles}         }
35 sub has_weak_ref        { exists $_[0]->{weak_ref}        }
36 sub has_init_arg        { exists $_[0]->{init_arg}        }
37 sub has_type_constraint { exists $_[0]->{type_constraint} }
38
39 sub generate_accessor {
40     my $attribute = shift;
41
42     my $name       = $attribute->{name};
43     my $key        = $attribute->{init_arg};
44     my $default    = $attribute->{default};
45     my $trigger    = $attribute->{trigger};
46     my $type       = $attribute->{type_constraint};
47     my $constraint = $attribute->find_type_constraint;
48
49     my $accessor = 'sub {
50         my $self = shift;';
51
52     if ($attribute->{is} eq 'rw') {
53         $accessor .= 'if (@_) {
54             local $_ = $_[0];';
55
56         if ($constraint) {
57             $accessor .= 'do {
58                 my $display = defined($_) ? $_ : "undef";
59                 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
60             };'
61         }
62
63         $accessor .= '$self->{$key} = $_;';
64
65         if ($attribute->{weak_ref}) {
66             $accessor .= 'Scalar::Util::weaken($self->{$key});';
67         }
68
69         if ($trigger) {
70             $accessor .= '$trigger->($self, $_, $attribute);';
71         }
72
73         $accessor .= '}';
74     }
75     else {
76     }
77
78     if ($attribute->{lazy}) {
79         $accessor .= '$self->{$key} = ';
80         $accessor .= ref($attribute->{default}) eq 'CODE'
81                    ? '$default->($self)'
82                    : '$default';
83         $accessor .= ' if !exists($self->{$key});';
84     }
85
86     $accessor .= 'return $self->{$key}
87     }';
88
89     return eval $accessor;
90 }
91
92 sub generate_predicate {
93     my $attribute = shift;
94     my $key = $attribute->{init_arg};
95
96     my $predicate = 'sub { exists($_[0]->{$key}) }';
97
98     return eval $predicate;
99 }
100
101 sub generate_clearer {
102     my $attribute = shift;
103     my $key = $attribute->{init_arg};
104
105     my $predicate = 'sub { delete($_[0]->{$key}) }';
106
107     return eval $predicate;
108 }
109
110 sub generate_handles {
111     my $attribute = shift;
112     my $reader = $attribute->{name};
113
114     my %method_map;
115
116     for my $local_method (keys %{ $attribute->{handles} }) {
117         my $remote_method = $attribute->{handles}{$local_method};
118
119         my $method = 'sub {
120             my $self = shift;
121             $self->$reader->$remote_method(@_)
122         }';
123
124         $method_map{$local_method} = eval $method;
125     }
126
127     return \%method_map;
128 }
129
130 sub create {
131     my ($self, $class, $name, %args) = @_;
132
133     confess "You must specify a default for lazy attribute '$name'"
134         if $args{lazy} && !exists($args{default});
135
136     confess "Trigger is not allowed on read-only attribute '$name'"
137         if $args{trigger} && $args{is} ne 'rw';
138
139     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
140         if ref($args{default})
141         && ref($args{default}) ne 'CODE';
142
143     $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
144         if $args{handles}
145         && ref($args{handles}) eq 'ARRAY';
146
147     confess "You must pass a HASH or ARRAY to handles"
148         if exists($args{handles})
149         && ref($args{handles}) ne 'HASH';
150
151     $args{type_constraint} = delete $args{isa};
152
153     my $attribute = $self->new(%args, name => $name, class => $class);
154     my $meta = $class->meta;
155
156     $meta->add_attribute($attribute);
157
158     # install an accessor
159     if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
160         my $accessor = $attribute->generate_accessor;
161         no strict 'refs';
162         *{ $class . '::' . $name } = $accessor;
163     }
164
165     for my $method (qw/predicate clearer/) {
166         if (exists $attribute->{$method}) {
167             my $generator = "generate_$method";
168             my $coderef = $attribute->$generator;
169             no strict 'refs';
170             *{ $class . '::' . $attribute->{$method} } = $coderef;
171         }
172     }
173
174     if ($attribute->{handles}) {
175         my $method_map = $attribute->generate_handles;
176         for my $method_name (keys %$method_map) {
177             no strict 'refs';
178             *{ $class . '::' . $method_name } = $method_map->{$method_name};
179         }
180     }
181
182     return $attribute;
183 }
184
185 sub find_type_constraint {
186     my $self = shift;
187     my $type = $self->type_constraint;
188
189     return unless $type;
190
191     my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
192     return $checker if $checker;
193
194     confess "Unable to parse type constraint '$type'";
195 }
196
197 sub verify_type_constraint {
198     my $self = shift;
199     local $_ = shift;
200
201     my $type = $self->type_constraint
202         or return 1;
203     my $constraint = $self->find_type_constraint
204         or return 1;
205
206     return 1 if $constraint->($_);
207
208     my $name = $self->name;
209     local $_ = "undef" unless defined($_);
210     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
211 }
212
213 1;
214
215 __END__
216
217 =head1 NAME
218
219 Mouse::Attribute - attribute metaclass
220
221 =head1 METHODS
222
223 =head2 new %args -> Mouse::Attribute
224
225 Instantiates a new Mouse::Attribute. Does nothing else.
226
227 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
228
229 Creates a new attribute in OwnerClass. Accessors and helper methods are
230 installed. Some error checking is done.
231
232 =head2 name -> AttributeName
233
234 =head2 class -> OwnerClass
235
236 =head2 default -> Value
237
238 =head2 predicate -> MethodName
239
240 =head2 clearer -> MethodName
241
242 =head2 handles -> { LocalName => RemoteName }
243
244 =head2 weak_ref -> Bool
245
246 =head2 init_arg -> Str
247
248 Informational methods.
249
250 =head2 generate_accessor -> CODE
251
252 Creates a new code reference for the attribute's accessor.
253
254 =head2 generate_predicate -> CODE
255
256 Creates a new code reference for the attribute's predicate.
257
258 =head2 generate_clearer -> CODE
259
260 Creates a new code reference for the attribute's clearer.
261
262 =head2 generate_handles -> { MethodName => CODE }
263
264 Creates a new code reference for each of the attribute's handles methods.
265
266 =cut
267