Unknown type constraints are now interpreted as blessed($value) eq $type
[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} }
39sub has_weak_ref { exists $_[0]->{weak_ref} }
40sub has_init_arg { exists $_[0]->{init_arg} }
41sub has_type_constraint { exists $_[0]->{type_constraint} }
de9a434a 42sub has_trigger { exists $_[0]->{trigger} }
43sub has_builder { exists $_[0]->{builder} }
ccea8101 44
c3398f5b 45sub generate_accessor {
46 my $attribute = shift;
47
2434d21b 48 my $name = $attribute->name;
49 my $key = $attribute->init_arg;
50 my $default = $attribute->default;
51 my $trigger = $attribute->trigger;
52 my $type = $attribute->type_constraint;
5aa30ced 53 my $constraint = $attribute->find_type_constraint;
c3398f5b 54
55 my $accessor = 'sub {
56 my $self = shift;';
57
2434d21b 58 if ($attribute->_is_metadata eq 'rw') {
c3398f5b 59 $accessor .= 'if (@_) {
a707f587 60 local $_ = $_[0];';
61
62 if ($constraint) {
1f2b4780 63 $accessor .= 'do {
64 my $display = defined($_) ? $_ : "undef";
65 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display") unless $constraint->();
66 };'
a707f587 67 }
68
69 $accessor .= '$self->{$key} = $_;';
c3398f5b 70
2434d21b 71 if ($attribute->weak_ref) {
c3398f5b 72 $accessor .= 'Scalar::Util::weaken($self->{$key});';
73 }
74
75 if ($trigger) {
a707f587 76 $accessor .= '$trigger->($self, $_, $attribute);';
c3398f5b 77 }
78
79 $accessor .= '}';
80 }
81 else {
82 }
83
2434d21b 84 if ($attribute->is_lazy) {
c3398f5b 85 $accessor .= '$self->{$key} = ';
2434d21b 86 $accessor .= ref($default) eq 'CODE'
c3398f5b 87 ? '$default->($self)'
88 : '$default';
89 $accessor .= ' if !exists($self->{$key});';
90 }
91
92 $accessor .= 'return $self->{$key}
93 }';
94
95 return eval $accessor;
96}
97
98sub generate_predicate {
99 my $attribute = shift;
2434d21b 100 my $key = $attribute->init_arg;
c3398f5b 101
102 my $predicate = 'sub { exists($_[0]->{$key}) }';
103
104 return eval $predicate;
105}
106
107sub generate_clearer {
108 my $attribute = shift;
2434d21b 109 my $key = $attribute->init_arg;
c3398f5b 110
111 my $predicate = 'sub { delete($_[0]->{$key}) }';
112
113 return eval $predicate;
114}
115
116sub generate_handles {
117 my $attribute = shift;
2434d21b 118 my $reader = $attribute->name;
c3398f5b 119
120 my %method_map;
121
2434d21b 122 for my $local_method (keys %{ $attribute->handles }) {
123 my $remote_method = $attribute->handles->{$local_method};
c3398f5b 124
125 my $method = 'sub {
126 my $self = shift;
127 $self->$reader->$remote_method(@_)
128 }';
129
130 $method_map{$local_method} = eval $method;
131 }
132
133 return \%method_map;
134}
135
136sub create {
137 my ($self, $class, $name, %args) = @_;
138
139 confess "You must specify a default for lazy attribute '$name'"
140 if $args{lazy} && !exists($args{default});
141
142 confess "Trigger is not allowed on read-only attribute '$name'"
143 if $args{trigger} && $args{is} ne 'rw';
144
145 confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
146 if ref($args{default})
147 && ref($args{default}) ne 'CODE';
148
149 $args{handles} = { map { $_ => $_ } @{ $args{handles} } }
150 if $args{handles}
151 && ref($args{handles}) eq 'ARRAY';
152
153 confess "You must pass a HASH or ARRAY to handles"
154 if exists($args{handles})
155 && ref($args{handles}) ne 'HASH';
156
186657a9 157 $args{type_constraint} = delete $args{isa};
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;
210 my $constraint = $self->find_type_constraint
211 or return 1;
212
213 return 1 if $constraint->($_);
214
215 my $name = $self->name;
1f2b4780 216 local $_ = "undef" unless defined($_);
5aa30ced 217 Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $_");
218}
219
c3398f5b 2201;
221
222__END__
223
224=head1 NAME
225
226Mouse::Attribute - attribute metaclass
227
228=head1 METHODS
229
230=head2 new %args -> Mouse::Attribute
231
232Instantiates a new Mouse::Attribute. Does nothing else.
233
234=head2 create OwnerClass, AttributeName, %args -> Mouse::Attribute
235
236Creates a new attribute in OwnerClass. Accessors and helper methods are
237installed. Some error checking is done.
238
239=head2 name -> AttributeName
240
241=head2 class -> OwnerClass
242
243=head2 default -> Value
244
245=head2 predicate -> MethodName
246
247=head2 clearer -> MethodName
248
249=head2 handles -> { LocalName => RemoteName }
250
251=head2 weak_ref -> Bool
252
253=head2 init_arg -> Str
254
255Informational methods.
256
257=head2 generate_accessor -> CODE
258
259Creates a new code reference for the attribute's accessor.
260
261=head2 generate_predicate -> CODE
262
263Creates a new code reference for the attribute's predicate.
264
265=head2 generate_clearer -> CODE
266
267Creates a new code reference for the attribute's clearer.
268
269=head2 generate_handles -> { MethodName => CODE }
270
271Creates a new code reference for each of the attribute's handles methods.
272
273=cut
274