Tests and implementation for Undef/Defined types
[gitmo/Mouse.git] / lib / Mouse / Attribute.pm
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2package Mouse::Attribute;
3use strict;
4use warnings;
5
6use Carp 'confess';
7
8sub new {
9 my $class = shift;
10 my %args = @_;
11
7ee01d77 12 $args{init_arg} = $args{name}
13 unless exists $args{init_arg};
c3398f5b 14 $args{is} ||= '';
15
16 bless \%args, $class;
17}
18
186657a9 19sub name { $_[0]->{name} }
20sub class { $_[0]->{class} }
21sub default { $_[0]->{default} }
22sub predicate { $_[0]->{predicate} }
23sub clearer { $_[0]->{clearer} }
24sub handles { $_[0]->{handles} }
25sub weak_ref { $_[0]->{weak_ref} }
26sub init_arg { $_[0]->{init_arg} }
27sub type_constraint { $_[0]->{type_constraint} }
c3398f5b 28
ccea8101 29sub has_name { exists $_[0]->{name} }
30sub has_class { exists $_[0]->{class} }
31sub has_default { exists $_[0]->{default} }
32sub has_predicate { exists $_[0]->{predicate} }
33sub has_clearer { exists $_[0]->{clearer} }
34sub has_handles { exists $_[0]->{handles} }
35sub has_weak_ref { exists $_[0]->{weak_ref} }
36sub has_init_arg { exists $_[0]->{init_arg} }
37sub has_type_constraint { exists $_[0]->{type_constraint} }
38
c3398f5b 39sub generate_accessor {
40 my $attribute = shift;
41
a707f587 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};
5aa30ced 47 my $constraint = $attribute->find_type_constraint;
c3398f5b 48
49 my $accessor = 'sub {
50 my $self = shift;';
51
52 if ($attribute->{is} eq 'rw') {
53 $accessor .= 'if (@_) {
a707f587 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} = $_;';
c3398f5b 61
62 if ($attribute->{weak_ref}) {
63 $accessor .= 'Scalar::Util::weaken($self->{$key});';
64 }
65
66 if ($trigger) {
a707f587 67 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 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
89sub 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
98sub 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
107sub 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
127sub 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
186657a9 148 $args{type_constraint} = delete $args{isa};
149
c3398f5b 150 my $attribute = $self->new(%args, name => $name, class => $class);
151 my $meta = $class->meta;
152
b2500191 153 $meta->add_attribute($attribute);
154
c3398f5b 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
c3398f5b 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
5aa30ced 182sub 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
194sub 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
c3398f5b 2091;
210
211__END__
212
213=head1 NAME
214
215Mouse::Attribute - attribute metaclass
216
217=head1 METHODS
218
219=head2 new %args -> Mouse::Attribute
220
221Instantiates a new Mouse::Attribute. Does nothing else.
222
223=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
224
225Creates a new attribute in OwnerClass. Accessors and helper methods are
226installed. 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
244Informational methods.
245
246=head2 generate_accessor -> CODE
247
248Creates a new code reference for the attribute's accessor.
249
250=head2 generate_predicate -> CODE
251
252Creates a new code reference for the attribute's predicate.
253
254=head2 generate_clearer -> CODE
255
256Creates a new code reference for the attribute's clearer.
257
258=head2 generate_handles -> { MethodName => CODE }
259
260Creates a new code reference for each of the attribute's handles methods.
261
262=cut
263