Fix RT #49902
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
CommitLineData
306290e8 1package Mouse::Meta::Attribute;
c3398f5b 2use strict;
3use warnings;
4
684db121 5use Mouse::Meta::TypeConstraint;
90fe520e 6use Mouse::Meta::Method::Accessor;
c3398f5b 7
8sub new {
2608b115 9 my ($class, $name, %options) = @_;
c3398f5b 10
2608b115 11 $options{name} = $name;
2e7e86c6 12
2608b115 13 $options{init_arg} = $name
14 unless exists $options{init_arg};
45959ffa 15
90fe520e 16 my $is = $options{is} ||= '';
17
18 if($is eq 'rw'){
19 $options{accessor} = $name if !exists $options{accessor};
20 }
21 elsif($is eq 'ro'){
22 $options{reader} = $name if !exists $options{reader};
23 }
c3398f5b 24
2608b115 25 bless \%options, $class;
c3398f5b 26}
27
90fe520e 28# readers
29
f6715552 30sub name { $_[0]->{name} }
31sub associated_class { $_[0]->{associated_class} }
90fe520e 32
33sub accessor { $_[0]->{accessor} }
34sub reader { $_[0]->{reader} }
35sub writer { $_[0]->{writer} }
36sub predicate { $_[0]->{predicate} }
37sub clearer { $_[0]->{clearer} }
38sub handles { $_[0]->{handles} }
39
f6715552 40sub _is_metadata { $_[0]->{is} }
41sub is_required { $_[0]->{required} }
42sub default { $_[0]->{default} }
43sub is_lazy { $_[0]->{lazy} }
44sub is_lazy_build { $_[0]->{lazy_build} }
f6715552 45sub is_weak_ref { $_[0]->{weak_ref} }
46sub init_arg { $_[0]->{init_arg} }
47sub type_constraint { $_[0]->{type_constraint} }
4c03ed87 48sub find_type_constraint {
49 Carp::carp("This method was deprecated");
50 $_[0]->type_constraint();
51}
f6715552 52sub trigger { $_[0]->{trigger} }
53sub builder { $_[0]->{builder} }
54sub should_auto_deref { $_[0]->{auto_deref} }
55sub should_coerce { $_[0]->{should_coerce} }
c3398f5b 56
90fe520e 57# predicates
58
59sub has_accessor { exists $_[0]->{accessor} }
60sub has_reader { exists $_[0]->{reader} }
61sub has_writer { exists $_[0]->{writer} }
f6715552 62sub has_predicate { exists $_[0]->{predicate} }
63sub has_clearer { exists $_[0]->{clearer} }
64sub has_handles { exists $_[0]->{handles} }
90fe520e 65
66sub has_default { exists $_[0]->{default} }
f6715552 67sub has_type_constraint { exists $_[0]->{type_constraint} }
68sub has_trigger { exists $_[0]->{trigger} }
69sub has_builder { exists $_[0]->{builder} }
eec1bb49 70
1bfebf5f 71sub _create_args {
72 $_[0]->{_create_args} = $_[1] if @_ > 1;
73 $_[0]->{_create_args}
74}
75
90fe520e 76sub accessor_metaclass { 'Mouse::Meta::Method::Accessor' }
77
c3398f5b 78sub create {
79 my ($self, $class, $name, %args) = @_;
80
90fe520e 81 $args{name} = $name;
181502b9 82 $args{associated_class} = $class;
1bfebf5f 83
93d190e0 84 %args = $self->canonicalize_args($name, %args);
1bbaa8ed 85 $self->validate_args($name, \%args);
45ea8620 86
32af3489 87 $args{should_coerce} = delete $args{coerce}
4188b837 88 if exists $args{coerce};
89
eec1bb49 90 if (exists $args{isa}) {
91 my $type_constraint = delete $args{isa};
684db121 92 $args{type_constraint}= Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint($type_constraint);
eec1bb49 93 }
186657a9 94
2608b115 95 my $attribute = $self->new($name, %args);
1bfebf5f 96
724c77c0 97 $attribute->_create_args(\%args);
c3398f5b 98
724c77c0 99 $class->add_attribute($attribute);
b2500191 100
74be9f76 101 my $associated_methods = 0;
102
90fe520e 103 my $generator_class = $self->accessor_metaclass;
104 foreach my $type(qw(accessor reader writer predicate clearer handles)){
105 if(exists $attribute->{$type}){
106 my $installer = '_install_' . $type;
107 $generator_class->$installer($attribute, $attribute->{$type}, $class);
74be9f76 108 $associated_methods++;
c3398f5b 109 }
110 }
111
90fe520e 112 if($associated_methods == 0 && ($attribute->_is_metadata || '') ne 'bare'){
c8c1aeaf 113 Carp::cluck(qq{Attribute ($name) of class }.$class->name.qq{ has no associated methods (did you mean to provide an "is" argument?)});
a7d31de0 114
74be9f76 115 }
116
c3398f5b 117 return $attribute;
118}
119
93d190e0 120sub canonicalize_args {
121 my $self = shift;
122 my $name = shift;
123 my %args = @_;
124
125 if ($args{lazy_build}) {
126 $args{lazy} = 1;
127 $args{required} = 1;
128 $args{builder} = "_build_${name}"
129 if !exists($args{builder});
130 if ($name =~ /^_/) {
131 $args{clearer} = "_clear${name}" if !exists($args{clearer});
132 $args{predicate} = "_has${name}" if !exists($args{predicate});
133 }
134 else {
135 $args{clearer} = "clear_${name}" if !exists($args{clearer});
136 $args{predicate} = "has_${name}" if !exists($args{predicate});
137 }
138 }
139
140 return %args;
141}
142
8fd9e611 143sub validate_args {
144 my $self = shift;
145 my $name = shift;
1bbaa8ed 146 my $args = shift;
8fd9e611 147
fce211ae 148 $self->throw_error("You can not use lazy_build and default for the same attribute ($name)")
1bbaa8ed 149 if $args->{lazy_build} && exists $args->{default};
93d190e0 150
fce211ae 151 $self->throw_error("You cannot have lazy attribute ($name) without specifying a default value for it")
1bbaa8ed 152 if $args->{lazy}
153 && !exists($args->{default})
154 && !exists($args->{builder});
8fd9e611 155
fce211ae 156 $self->throw_error("References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])")
1bbaa8ed 157 if ref($args->{default})
158 && ref($args->{default}) ne 'CODE';
8fd9e611 159
fce211ae 160 $self->throw_error("You cannot auto-dereference without specifying a type constraint on attribute ($name)")
1bbaa8ed 161 if $args->{auto_deref} && !exists($args->{isa});
8fd9e611 162
fce211ae 163 $self->throw_error("You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute ($name)")
1bbaa8ed 164 if $args->{auto_deref}
a3f4f68e 165 && $args->{isa} !~ /^(?:ArrayRef|HashRef)(?:\[.*\])?$/;
8fd9e611 166
506db557 167 if ($args->{trigger}) {
a08e715f 168 if (ref($args->{trigger}) eq 'HASH') {
fce211ae 169 $self->throw_error("HASH-based form of trigger has been removed. Only the coderef form of triggers are now supported.");
844fa049 170 }
506db557 171
fce211ae 172 $self->throw_error("Trigger must be a CODE ref on attribute ($name)")
a08e715f 173 if ref($args->{trigger}) ne 'CODE';
506db557 174 }
6c5498d0 175
8fd9e611 176 return 1;
177}
178
20e25eb9 179sub verify_against_type_constraint {
f55f60dd 180 my ($self, $value) = @_;
181 my $tc = $self->type_constraint;
182 return 1 unless $tc;
5aa30ced 183
f55f60dd 184 local $_ = $value;
185 return 1 if $tc->check($value);
5aa30ced 186
f55f60dd 187 $self->verify_type_constraint_error($self->name, $value, $tc);
b3b74cc6 188}
5aa30ced 189
b3b74cc6 190sub verify_type_constraint_error {
191 my($self, $name, $value, $type) = @_;
fce211ae 192 $self->throw_error("Attribute ($name) does not pass the type constraint because: " . $type->get_message($value));
5aa30ced 193}
194
8a7f2a8a 195sub coerce_constraint { ## my($self, $value) = @_;
196 my $type = $_[0]->{type_constraint}
197 or return $_[1];
684db121 198 return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $_[0]->type_constraint, $_[1]);
4188b837 199}
200
af745d5a 201sub _canonicalize_handles {
202 my $self = shift;
203 my $handles = shift;
204
205 if (ref($handles) eq 'HASH') {
206 return %$handles;
207 }
208 elsif (ref($handles) eq 'ARRAY') {
209 return map { $_ => $_ } @$handles;
210 }
211 else {
fce211ae 212 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
af745d5a 213 }
214}
215
1bfebf5f 216sub clone_parent {
217 my $self = shift;
218 my $class = shift;
219 my $name = shift;
220 my %args = ($self->get_parent_args($class, $name), @_);
221
222 $self->create($class, $name, %args);
223}
224
225sub get_parent_args {
226 my $self = shift;
227 my $class = shift;
228 my $name = shift;
229
724c77c0 230 for my $super ($class->linearized_isa) {
bb733405 231 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
1bfebf5f 232 or next;
233 return %{ $super_attr->_create_args };
234 }
235
fce211ae 236 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
237}
238
239sub throw_error{
240 my $self = shift;
241
242 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
243 $metaclass->throw_error(@_, depth => 1);
1bfebf5f 244}
245
c3398f5b 2461;
247
248__END__
249
250=head1 NAME
251
306290e8 252Mouse::Meta::Attribute - attribute metaclass
c3398f5b 253
254=head1 METHODS
255
306290e8 256=head2 new %args -> Mouse::Meta::Attribute
c3398f5b 257
306290e8 258Instantiates a new Mouse::Meta::Attribute. Does nothing else.
c3398f5b 259
306290e8 260=head2 create OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
c3398f5b 261
262Creates a new attribute in OwnerClass. Accessors and helper methods are
263installed. Some error checking is done.
264
265=head2 name -> AttributeName
266
181502b9 267=head2 associated_class -> OwnerClass
c3398f5b 268
ab27a55e 269=head2 is_required -> Bool
c3398f5b 270
ab27a55e 271=head2 default -> Item
c3398f5b 272
ab27a55e 273=head2 has_default -> Bool
274
275=head2 is_lazy -> Bool
276
277=head2 predicate -> MethodName | Undef
278
279=head2 has_predicate -> Bool
280
281=head2 clearer -> MethodName | Undef
282
283=head2 has_clearer -> Bool
c3398f5b 284
285=head2 handles -> { LocalName => RemoteName }
286
ab27a55e 287=head2 has_handles -> Bool
288
3645b316 289=head2 is_weak_ref -> Bool
c3398f5b 290
291=head2 init_arg -> Str
292
ab27a55e 293=head2 type_constraint -> Str
294
295=head2 has_type_constraint -> Bool
296
297=head2 trigger => CODE | Undef
298
299=head2 has_trigger -> Bool
300
301=head2 builder => MethodName | Undef
302
303=head2 has_builder -> Bool
304
93f08899 305=head2 is_lazy_build => Bool
306
0fff36e6 307=head2 should_auto_deref -> Bool
308
c3398f5b 309Informational methods.
310
20e25eb9 311=head2 verify_against_type_constraint Item -> 1 | ERROR
fb706f5c 312
313Checks that the given value passes this attribute's type constraint. Returns 1
314on success, otherwise C<confess>es.
315
93d190e0 316=head2 canonicalize_args Name, %args -> %args
317
318Canonicalizes some arguments to create. In particular, C<lazy_build> is
319canonicalized into C<lazy>, C<builder>, etc.
320
1bbaa8ed 321=head2 validate_args Name, \%args -> 1 | ERROR
93d190e0 322
323Checks that the arguments to create the attribute (ie those specified by
324C<has>) are valid.
325
f7b11a21 326=head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
327
328Creates a new attribute in OwnerClass, inheriting options from parent classes.
329Accessors and helper methods are installed. Some error checking is done.
330
331=head2 get_parent_args OwnerClass, AttributeName -> Hash
332
333Returns the options that the parent class of C<OwnerClass> used for attribute
334C<AttributeName>.
335
c3398f5b 336=cut
337