Moose compat: required error message tweak
[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';
3301fa54 7use Scalar::Util 'blessed';
c3398f5b 8
9sub new {
10 my $class = shift;
11 my %args = @_;
12
7ee01d77 13 $args{init_arg} = $args{name}
14 unless exists $args{init_arg};
c3398f5b 15 $args{is} ||= '';
16
17 bless \%args, $class;
18}
19
186657a9 20sub name { $_[0]->{name} }
21sub class { $_[0]->{class} }
2434d21b 22sub _is_metadata { $_[0]->{is} }
23sub is_required { $_[0]->{required} }
186657a9 24sub default { $_[0]->{default} }
2434d21b 25sub is_lazy { $_[0]->{lazy} }
186657a9 26sub predicate { $_[0]->{predicate} }
27sub clearer { $_[0]->{clearer} }
28sub handles { $_[0]->{handles} }
29sub weak_ref { $_[0]->{weak_ref} }
30sub init_arg { $_[0]->{init_arg} }
31sub type_constraint { $_[0]->{type_constraint} }
de9a434a 32sub trigger { $_[0]->{trigger} }
33sub builder { $_[0]->{builder} }
c3398f5b 34
ccea8101 35sub has_default { exists $_[0]->{default} }
36sub has_predicate { exists $_[0]->{predicate} }
37sub has_clearer { exists $_[0]->{clearer} }
38sub has_handles { exists $_[0]->{handles} }
ccea8101 39sub has_type_constraint { exists $_[0]->{type_constraint} }
de9a434a 40sub has_trigger { exists $_[0]->{trigger} }
41sub has_builder { exists $_[0]->{builder} }
ccea8101 42
c3398f5b 43sub generate_accessor {
44 my $attribute = shift;
45
2434d21b 46 my $name = $attribute->name;
47 my $key = $attribute->init_arg;
48 my $default = $attribute->default;
49 my $trigger = $attribute->trigger;
50 my $type = $attribute->type_constraint;
5aa30ced 51 my $constraint = $attribute->find_type_constraint;
9367e029 52 my $builder = $attribute->builder;
c3398f5b 53
54 my $accessor = 'sub {
55 my $self = shift;';
56
2434d21b 57 if ($attribute->_is_metadata eq 'rw') {
c3398f5b 58 $accessor .= 'if (@_) {
a707f587 59 local $_ = $_[0];';
60
61 if ($constraint) {
1f2b4780 62 $accessor .= 'do {
63 my $display = defined($_) ? $_ : "undef";
64 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
65 };'
a707f587 66 }
67
68 $accessor .= '$self->{$key} = $_;';
c3398f5b 69
2434d21b 70 if ($attribute->weak_ref) {
c3398f5b 71 $accessor .= 'Scalar::Util::weaken($self->{$key});';
72 }
73
74 if ($trigger) {
a707f587 75 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 76 }
77
78 $accessor .= '}';
79 }
80 else {
636c002e 81 $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;';
c3398f5b 82 }
83
2434d21b 84 if ($attribute->is_lazy) {
c3398f5b 85 $accessor .= '$self->{$key} = ';
9367e029 86
87 $accessor .= $attribute->has_builder
88 ? '$self->$builder'
89 : ref($default) eq 'CODE'
90 ? '$default->($self)'
91 : '$default';
92
c3398f5b 93 $accessor .= ' if !exists($self->{$key});';
94 }
95
96 $accessor .= 'return $self->{$key}
97 }';
98
99 return eval $accessor;
100}
101
102sub generate_predicate {
103 my $attribute = shift;
2434d21b 104 my $key = $attribute->init_arg;
c3398f5b 105
106 my $predicate = 'sub { exists($_[0]->{$key}) }';
107
108 return eval $predicate;
109}
110
111sub generate_clearer {
112 my $attribute = shift;
2434d21b 113 my $key = $attribute->init_arg;
c3398f5b 114
115 my $predicate = 'sub { delete($_[0]->{$key}) }';
116
117 return eval $predicate;
118}
119
120sub generate_handles {
121 my $attribute = shift;
2434d21b 122 my $reader = $attribute->name;
c3398f5b 123
124 my %method_map;
125
2434d21b 126 for my $local_method (keys %{ $attribute->handles }) {
127 my $remote_method = $attribute->handles->{$local_method};
c3398f5b 128
129 my $method = 'sub {
130 my $self = shift;
131 $self->$reader->$remote_method(@_)
132 }';
133
134 $method_map{$local_method} = eval $method;
135 }
136
137 return \%method_map;
138}
139
140sub create {
141 my ($self, $class, $name, %args) = @_;
142
143 confess "You must specify a default for lazy attribute '$name'"
9367e029 144 if $args{lazy} && !exists($args{default}) && !exists($args{builder});
c3398f5b 145
146 confess "Trigger is not allowed on read-only attribute '$name'"
147 if $args{trigger} && $args{is} ne 'rw';
148
149 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
150 if ref($args{default})
151 && ref($args{default}) ne 'CODE';
152
af745d5a 153 $args{handles} = { $self->_canonicalize_handles($args{handles}) }
154 if $args{handles};
c3398f5b 155
c021207d 156 $args{type_constraint} = delete $args{isa}
157 if exists $args{isa};
186657a9 158
c3398f5b 159 my $attribute = $self->new(%args, name => $name, class => $class);
160 my $meta = $class->meta;
161
b2500191 162 $meta->add_attribute($attribute);
163
c3398f5b 164 # install an accessor
2434d21b 165 if ($attribute->_is_metadata eq 'rw' || $attribute->_is_metadata eq 'ro') {
c3398f5b 166 my $accessor = $attribute->generate_accessor;
167 no strict 'refs';
168 *{ $class . '::' . $name } = $accessor;
169 }
170
c3398f5b 171 for my $method (qw/predicate clearer/) {
2434d21b 172 my $predicate = "has_$method";
173 if ($attribute->$predicate) {
c3398f5b 174 my $generator = "generate_$method";
175 my $coderef = $attribute->$generator;
176 no strict 'refs';
2434d21b 177 *{ $class . '::' . $attribute->$method } = $coderef;
c3398f5b 178 }
179 }
180
2434d21b 181 if ($attribute->has_handles) {
c3398f5b 182 my $method_map = $attribute->generate_handles;
183 for my $method_name (keys %$method_map) {
184 no strict 'refs';
185 *{ $class . '::' . $method_name } = $method_map->{$method_name};
186 }
187 }
188
189 return $attribute;
190}
191
5aa30ced 192sub find_type_constraint {
193 my $self = shift;
194 my $type = $self->type_constraint;
195
196 return unless $type;
197
198 my $checker = Mouse::TypeRegistry->optimized_constraints->{$type};
199 return $checker if $checker;
200
3301fa54 201 return sub { blessed($_) && blessed($_) eq $type };
5aa30ced 202}
203
204sub verify_type_constraint {
205 my $self = shift;
206 local $_ = shift;
207
208 my $type = $self->type_constraint
209 or return 1;
af745d5a 210 my $constraint = $self->find_type_constraint;
5aa30ced 211
212 return 1 if $constraint->($_);
213
214 my $name = $self->name;
1f2b4780 215 local $_ = "undef" unless defined($_);
5aa30ced 216 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
217}
218
af745d5a 219sub _canonicalize_handles {
220 my $self = shift;
221 my $handles = shift;
222
223 if (ref($handles) eq 'HASH') {
224 return %$handles;
225 }
226 elsif (ref($handles) eq 'ARRAY') {
227 return map { $_ => $_ } @$handles;
228 }
229 else {
230 confess "Unable to canonicalize the 'handles' option with $handles";
231 }
232}
233
c3398f5b 2341;
235
236__END__
237
238=head1 NAME
239
240Mouse::Attribute - attribute metaclass
241
242=head1 METHODS
243
244=head2 new %args -> Mouse::Attribute
245
246Instantiates a new Mouse::Attribute. Does nothing else.
247
248=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
249
250Creates a new attribute in OwnerClass. Accessors and helper methods are
251installed. Some error checking is done.
252
253=head2 name -> AttributeName
254
255=head2 class -> OwnerClass
256
ab27a55e 257=head2 is_required -> Bool
c3398f5b 258
ab27a55e 259=head2 default -> Item
c3398f5b 260
ab27a55e 261=head2 has_default -> Bool
262
263=head2 is_lazy -> Bool
264
265=head2 predicate -> MethodName | Undef
266
267=head2 has_predicate -> Bool
268
269=head2 clearer -> MethodName | Undef
270
271=head2 has_clearer -> Bool
c3398f5b 272
273=head2 handles -> { LocalName => RemoteName }
274
ab27a55e 275=head2 has_handles -> Bool
276
c3398f5b 277=head2 weak_ref -> Bool
278
279=head2 init_arg -> Str
280
ab27a55e 281=head2 type_constraint -> Str
282
283=head2 has_type_constraint -> Bool
284
285=head2 trigger => CODE | Undef
286
287=head2 has_trigger -> Bool
288
289=head2 builder => MethodName | Undef
290
291=head2 has_builder -> Bool
292
c3398f5b 293Informational methods.
294
295=head2 generate_accessor -> CODE
296
297Creates a new code reference for the attribute's accessor.
298
299=head2 generate_predicate -> CODE
300
301Creates a new code reference for the attribute's predicate.
302
303=head2 generate_clearer -> CODE
304
305Creates a new code reference for the attribute's clearer.
306
307=head2 generate_handles -> { MethodName => CODE }
308
309Creates a new code reference for each of the attribute's handles methods.
310
fb706f5c 311=head2 find_type_constraint -> CODE
312
313Returns a code reference which can be used to check that a given value passes
314this attribute's type constraint;
315
316=head2 verify_type_constraint Item -> 1 | ERROR
317
318Checks that the given value passes this attribute's type constraint. Returns 1
319on success, otherwise C<confess>es.
320
c3398f5b 321=cut
322