Tests and implementation for Undef/Defined types
[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 .= 'Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_") unless $constraint->();'
58         }
59
60         $accessor .= '$self->{$key} = $_;';
61
62         if ($attribute->{weak_ref}) {
63             $accessor .= 'Scalar::Util::weaken($self->{$key});';
64         }
65
66         if ($trigger) {
67             $accessor .= '$trigger->($self, $_, $attribute);';
68         }
69
70         $accessor .= '}';
71     }
72     else {
73     }
74
75     if ($attribute->{lazy}) {
76         $accessor .= '$self->{$key} = ';
77         $accessor .= ref($attribute->{default}) eq 'CODE'
78                    ? '$default->($self)'
79                    : '$default';
80         $accessor .= ' if !exists($self->{$key});';
81     }
82
83     $accessor .= 'return $self->{$key}
84     }';
85
86     return eval $accessor;
87 }
88
89 sub generate_predicate {
90     my $attribute = shift;
91     my $key = $attribute->{init_arg};
92
93     my $predicate = 'sub { exists($_[0]->{$key}) }';
94
95     return eval $predicate;
96 }
97
98 sub generate_clearer {
99     my $attribute = shift;
100     my $key = $attribute->{init_arg};
101
102     my $predicate = 'sub { delete($_[0]->{$key}) }';
103
104     return eval $predicate;
105 }
106
107 sub generate_handles {
108     my $attribute = shift;
109     my $reader = $attribute->{name};
110
111     my %method_map;
112
113     for my $local_method (keys %{ $attribute->{handles} }) {
114         my $remote_method = $attribute->{handles}{$local_method};
115
116         my $method = 'sub {
117             my $self = shift;
118             $self->$reader->$remote_method(@_)
119         }';
120
121         $method_map{$local_method} = eval $method;
122     }
123
124     return \%method_map;
125 }
126
127 sub create {
128     my ($self, $class, $name, %args) = @_;
129
130     confess "You must specify a default for lazy attribute '$name'"
131         if $args{lazy} && !exists($args{default});
132
133     confess "Trigger is not allowed on read-only attribute '$name'"
134         if $args{trigger} && $args{is} ne 'rw';
135
136     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
137         if ref($args{default})
138         && ref($args{default}) ne 'CODE';
139
140     $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
141         if $args{handles}
142         && ref($args{handles}) eq 'ARRAY';
143
144     confess "You must pass a HASH or ARRAY to handles"
145         if exists($args{handles})
146         && ref($args{handles}) ne 'HASH';
147
148     $args{type_constraint} = delete $args{isa};
149
150     my $attribute = $self->new(%args, name => $name, class => $class);
151     my $meta = $class->meta;
152
153     $meta->add_attribute($attribute);
154
155     # install an accessor
156     if ($attribute->{is} eq 'rw' || $attribute->{is} eq 'ro') {
157         my $accessor = $attribute->generate_accessor;
158         no strict 'refs';
159         *{ $class . '::' . $name } = $accessor;
160     }
161
162     for my $method (qw/predicate clearer/) {
163         if (exists $attribute->{$method}) {
164             my $generator = "generate_$method";
165             my $coderef = $attribute->$generator;
166             no strict 'refs';
167             *{ $class . '::' . $attribute->{$method} } = $coderef;
168         }
169     }
170
171     if ($attribute->{handles}) {
172         my $method_map = $attribute->generate_handles;
173         for my $method_name (keys %$method_map) {
174             no strict 'refs';
175             *{ $class . '::' . $method_name } = $method_map->{$method_name};
176         }
177     }
178
179     return $attribute;
180 }
181
182 sub find_type_constraint {
183     my $self = shift;
184     my $type = $self->type_constraint;
185
186     return unless $type;
187
188     my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
189     return $checker if $checker;
190
191     confess "Unable to parse type constraint '$type'";
192 }
193
194 sub verify_type_constraint {
195     my $self = shift;
196     local $_ = shift;
197
198     my $type = $self->type_constraint
199         or return 1;
200     my $constraint = $self->find_type_constraint
201         or return 1;
202
203     return 1 if $constraint->($_);
204
205     my $name = $self->name;
206     Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
207 }
208
209 1;
210
211 __END__
212
213 =head1 NAME
214
215 Mouse::Attribute - attribute metaclass
216
217 =head1 METHODS
218
219 =head2 new %args -> Mouse::Attribute
220
221 Instantiates a new Mouse::Attribute. Does nothing else.
222
223 =head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
224
225 Creates a new attribute in OwnerClass. Accessors and helper methods are
226 installed. Some error checking is done.
227
228 =head2 name -> AttributeName
229
230 =head2 class -> OwnerClass
231
232 =head2 default -> Value
233
234 =head2 predicate -> MethodName
235
236 =head2 clearer -> MethodName
237
238 =head2 handles -> { LocalName => RemoteName }
239
240 =head2 weak_ref -> Bool
241
242 =head2 init_arg -> Str
243
244 Informational methods.
245
246 =head2 generate_accessor -> CODE
247
248 Creates a new code reference for the attribute's accessor.
249
250 =head2 generate_predicate -> CODE
251
252 Creates a new code reference for the attribute's predicate.
253
254 =head2 generate_clearer -> CODE
255
256 Creates a new code reference for the attribute's clearer.
257
258 =head2 generate_handles -> { MethodName => CODE }
259
260 Creates a new code reference for each of the attribute's handles methods.
261
262 =cut
263