Add illigal inheritance process (Moose 1.09 feature)
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
CommitLineData
306290e8 1package Mouse::Meta::Attribute;
bc69ee88 2use Mouse::Util qw(:meta); # enables strict and warnings
c3398f5b 3
2efc0af1 4use Carp ();
2efc0af1 5
684db121 6use Mouse::Meta::TypeConstraint;
d7d8d49b 7
7d2a0f10 8my %valid_options = map { $_ => undef } (
9 'accessor',
10 'auto_deref',
11 'builder',
12 'clearer',
13 'coerce',
14 'default',
15 'documentation',
16 'does',
17 'handles',
18 'init_arg',
beb51b30 19 'insertion_order',
7d2a0f10 20 'is',
21 'isa',
22 'lazy',
23 'lazy_build',
24 'name',
25 'predicate',
26 'reader',
27 'required',
28 'traits',
29 'trigger',
30 'type_constraint',
31 'weak_ref',
32 'writer',
33
34 # internally used
35 'associated_class',
36 'associated_methods',
37
38 # Moose defines, but Mouse doesn't
39 #'definition_context',
40 #'initializer',
7d2a0f10 41
42 # special case for AttributeHelpers
43 'provides',
44 'curries',
45);
87ca293b 46
431e4817 47our @CARP_NOT = qw(Mouse::Meta::Class);
48
87ca293b 49sub new {
50 my $class = shift;
51 my $name = shift;
52
2053291d 53 my $args = $class->Mouse::Object::BUILDARGS(@_);
ba1f50a2 54
55 # XXX: for backward compatibility (with method modifiers)
56 if($class->can('canonicalize_args') != \&canonicalize_args){
2053291d 57 %{$args} = $class->canonicalize_args($name, %{$args});
ba1f50a2 58 }
59
2053291d 60 $class->_process_options($name, $args);
87ca293b 61
2053291d 62 $args->{name} = $name;
87ca293b 63
7d2a0f10 64 # check options
65 # (1) known by core
66 my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
67
68 # (2) known by subclasses
69 if(@bad && $class ne __PACKAGE__){
70 my %valid_attrs = (
71 map { $_ => undef }
72 grep { defined }
73 map { $_->init_arg() }
74 $class->meta->get_all_attributes()
75 );
76 @bad = grep{ !exists $valid_attrs{$_} } @bad;
77 }
78
79 # (3) bad options found
80 if(@bad){
431e4817 81 Carp::carp(
82 "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
83 . Mouse::Util::english_list(@bad));
7d2a0f10 84 }
85
2053291d 86 my $self = bless $args, $class;
2efc0af1 87
1b9e472d 88 # extra attributes
89 if($class ne __PACKAGE__){
2053291d 90 $class->meta->_initialize_object($self, $args);
90fe520e 91 }
c3398f5b 92
926290ac 93 return $self;
c3398f5b 94}
95
43165725 96sub has_read_method { $_[0]->has_reader || $_[0]->has_accessor }
97sub has_write_method { $_[0]->has_writer || $_[0]->has_accessor }
1b9e472d 98
df77fd72 99sub _create_args { # DEPRECATED
1bfebf5f 100 $_[0]->{_create_args} = $_[1] if @_ > 1;
101 $_[0]->{_create_args}
102}
103
87ca293b 104sub interpolate_class{
8cf51b82 105 my($class, $args) = @_;
c3398f5b 106
1b9e472d 107 if(my $metaclass = delete $args->{metaclass}){
108 $class = Mouse::Util::resolve_metaclass_alias( Attribute => $metaclass );
109 }
1bfebf5f 110
87ca293b 111 my @traits;
1b9e472d 112 if(my $traits_ref = delete $args->{traits}){
87ca293b 113
f3f04eed 114 for (my $i = 0; $i < @{$traits_ref}; $i++) {
115 my $trait = Mouse::Util::resolve_metaclass_alias(Attribute => $traits_ref->[$i], trait => 1);
b2500191 116
f3f04eed 117 next if $class->does($trait);
74be9f76 118
f3f04eed 119 push @traits, $trait;
1b9e472d 120
f3f04eed 121 # are there options?
122 push @traits, $traits_ref->[++$i]
123 if ref($traits_ref->[$i+1]);
124 }
c3398f5b 125
1b9e472d 126 if (@traits) {
127 $class = Mouse::Meta::Class->create_anon_class(
128 superclasses => [ $class ],
129 roles => \@traits,
130 cache => 1,
131 )->name;
1b9e472d 132 }
74be9f76 133 }
134
87ca293b 135 return( $class, @traits );
1b9e472d 136}
137
df77fd72 138sub canonicalize_args{ # DEPRECATED
230dd14a 139 #my($self, $name, %args) = @_;
140 my($self, undef, %args) = @_;
1b9e472d 141
142 Carp::cluck("$self->canonicalize_args has been deprecated."
da23cd4a 143 . "Use \$self->_process_options instead.");
1b9e472d 144
145 return %args;
146}
147
ce5920b7 148sub create { # DEPRECATED
230dd14a 149 #my($self, $class, $name, %args) = @_;
150 my($self) = @_;
1b9e472d 151
152 Carp::cluck("$self->create has been deprecated."
da23cd4a 153 . "Use \$meta->add_attribute and \$attr->install_accessors instead.");
1b9e472d 154
155 # noop
156 return $self;
c3398f5b 157}
158
ffbbf459 159sub _coerce_and_verify {
230dd14a 160 #my($self, $value, $instance) = @_;
161 my($self, $value) = @_;
ffbbf459 162
163 my $type_constraint = $self->{type_constraint};
4331a3f9 164 return $value if !defined $type_constraint;
ffbbf459 165
166 if ($self->should_coerce && $type_constraint->has_coercion) {
167 $value = $type_constraint->coerce($value);
168 }
169
ffbbf459 170 $self->verify_against_type_constraint($value);
171
172 return $value;
173}
174
20e25eb9 175sub verify_against_type_constraint {
f55f60dd 176 my ($self, $value) = @_;
5aa30ced 177
ffbbf459 178 my $type_constraint = $self->{type_constraint};
4331a3f9 179 return 1 if !$type_constraint;
ffbbf459 180 return 1 if $type_constraint->check($value);
5aa30ced 181
da23cd4a 182 $self->_throw_type_constraint_error($value, $type_constraint);
b3b74cc6 183}
5aa30ced 184
da23cd4a 185sub _throw_type_constraint_error {
2a96ea85 186 my($self, $value, $type) = @_;
187
188 $self->throw_error(
189 sprintf q{Attribute (%s) does not pass the type constraint because: %s},
190 $self->name,
191 $type->get_message($value),
192 );
5aa30ced 193}
194
df7e4729 195sub illegal_options_for_inheritance {
196 return qw(is reader writer accessor clearer predicate);
197}
198
1b9e472d 199sub clone_and_inherit_options{
2053291d 200 my $self = shift;
201 my $args = $self->Mouse::Object::BUILDARGS(@_);
8cf51b82 202
df7e4729 203 foreach my $illegal($self->illegal_options_for_inheritance) {
204 if(exists $args->{$illegal}) {
205 $self->throw_error("Illegal inherited option: $illegal");
206 }
207 }
1b9e472d 208
4f41d79b 209 foreach my $name(keys %{$self}){
df7e4729 210 if(!exists $args->{$name}){
211 $args->{$name} = $self->{$name}; # inherit from self
4f41d79b 212 }
213 }
7d2a0f10 214
df7e4729 215 my($attribute_class, @traits) = ref($self)->interpolate_class($args);
216 $args->{traits} = \@traits if @traits;
217
7d2a0f10 218 # remove temporary caches
219 foreach my $attr(keys %{$args}){
220 if($attr =~ /\A _/xms){
221 delete $args->{$attr};
222 }
223 }
224
df7e4729 225 # remove default if lazy_build => 1
226 if($args->{lazy_build}) {
227 delete $args->{default};
228 }
229
2053291d 230 return $attribute_class->new($self->name, $args);
1b9e472d 231}
232
df77fd72 233sub clone_parent { # DEPRECATED
1bfebf5f 234 my $self = shift;
235 my $class = shift;
236 my $name = shift;
237 my %args = ($self->get_parent_args($class, $name), @_);
238
1b9e472d 239 Carp::cluck("$self->clone_parent has been deprecated."
da23cd4a 240 . "Use \$meta->add_attribute and \$attr->install_accessors instead.");
1b9e472d 241
7ca5c5fb 242 $self->clone_and_inherited_args($class, $name, %args);
1bfebf5f 243}
244
df77fd72 245sub get_parent_args { # DEPRECATED
1bfebf5f 246 my $self = shift;
247 my $class = shift;
248 my $name = shift;
249
724c77c0 250 for my $super ($class->linearized_isa) {
bb733405 251 my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
1bfebf5f 252 or next;
253 return %{ $super_attr->_create_args };
254 }
255
fce211ae 256 $self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
257}
258
2a464664 259
0126c27c 260sub get_read_method {
751de1a1 261 return $_[0]->reader || $_[0]->accessor
df77fd72 262}
0126c27c 263sub get_write_method {
751de1a1 264 return $_[0]->writer || $_[0]->accessor
265}
266
267sub _get_accessor_method_ref {
268 my($self, $type, $generator) = @_;
269
270 my $metaclass = $self->associated_class
271 || $self->throw_error('No asocciated class for ' . $self->name);
272
273 my $accessor = $self->$type();
274 if($accessor){
275 return $metaclass->get_method_body($accessor);
276 }
277 else{
278 return $self->accessor_metaclass->$generator($self, $metaclass);
279 }
df77fd72 280}
2a464664 281
282sub get_read_method_ref{
283 my($self) = @_;
751de1a1 284 return $self->{_read_method_ref} ||= $self->_get_accessor_method_ref('get_read_method', '_generate_reader');
2a464664 285}
286
287sub get_write_method_ref{
288 my($self) = @_;
751de1a1 289 return $self->{_write_method_ref} ||= $self->_get_accessor_method_ref('get_write_method', '_generate_writer');
290}
2a464664 291
751de1a1 292sub set_value {
293 my($self, $object, $value) = @_;
294 return $self->get_write_method_ref()->($object, $value);
295}
296
297sub get_value {
298 my($self, $object) = @_;
299 return $self->get_read_method_ref()->($object);
2a464664 300}
301
751de1a1 302sub has_value {
303 my($self, $object) = @_;
060f9228 304 my $accessor_ref = $self->{_predicate_ref}
751de1a1 305 ||= $self->_get_accessor_method_ref('predicate', '_generate_predicate');
306
060f9228 307 return $accessor_ref->($object);
751de1a1 308}
309
310sub clear_value {
311 my($self, $object) = @_;
060f9228 312 my $accessor_ref = $self->{_crealer_ref}
751de1a1 313 ||= $self->_get_accessor_method_ref('clearer', '_generate_clearer');
314
060f9228 315 return $accessor_ref->($object);
751de1a1 316}
317
318
04493075 319sub associate_method{
230dd14a 320 #my($attribute, $method_name) = @_;
321 my($attribute) = @_;
04493075 322 $attribute->{associated_methods}++;
323 return;
324}
325
1b9e472d 326sub install_accessors{
327 my($attribute) = @_;
328
93540011 329 my $metaclass = $attribute->associated_class;
4ab51fb0 330 my $accessor_class = $attribute->accessor_metaclass;
1b9e472d 331
4ab51fb0 332 foreach my $type(qw(accessor reader writer predicate clearer)){
1b9e472d 333 if(exists $attribute->{$type}){
4ab51fb0 334 my $generator = '_generate_' . $type;
335 my $code = $accessor_class->$generator($attribute, $metaclass);
336 $metaclass->add_method($attribute->{$type} => $code);
e5e22afd 337 $attribute->associate_method($attribute->{$type});
4ab51fb0 338 }
339 }
7ca5c5fb 340
4ab51fb0 341 # install delegation
342 if(exists $attribute->{handles}){
343 my %handles = $attribute->_canonicalize_handles($attribute->{handles});
feaa7084 344
cbb81058 345 while(my($handle, $method_to_call) = each %handles){
df7e4729 346 if($metaclass->has_method($handle)) {
347 $attribute->throw_error("You cannot overwrite a locally defined method ($handle) with a delegation");
348 }
349
cbb81058 350 $metaclass->add_method($handle =>
351 $attribute->_make_delegation_method(
352 $handle, $method_to_call));
4ab51fb0 353
cbb81058 354 $attribute->associate_method($handle);
1b9e472d 355 }
356 }
357
358 if($attribute->can('create') != \&create){
7ca5c5fb 359 # backword compatibility
1b9e472d 360 $attribute->create($metaclass, $attribute->name, %{$attribute});
361 }
362
363 return;
364}
365
a4b15169 366sub delegation_metaclass() { ## no critic
367 'Mouse::Meta::Method::Delegation'
368}
cbb81058 369
370sub _canonicalize_handles {
371 my($self, $handles) = @_;
372
373 if (ref($handles) eq 'HASH') {
374 return %$handles;
375 }
376 elsif (ref($handles) eq 'ARRAY') {
377 return map { $_ => $_ } @$handles;
378 }
7bc01428 379 elsif ( ref($handles) eq 'CODE' ) {
380 my $class_or_role = ( $self->{isa} || $self->{does} )
381 || $self->throw_error( "Cannot find delegate metaclass for attribute " . $self->name );
382 return $handles->( $self, Mouse::Meta::Class->initialize("$class_or_role"));
383 }
cbb81058 384 elsif (ref($handles) eq 'Regexp') {
385 my $class_or_role = ($self->{isa} || $self->{does})
386 || $self->throw_error("Cannot delegate methods based on a Regexp without a type constraint (isa)");
387
388 my $meta = Mouse::Meta::Class->initialize("$class_or_role"); # "" for stringify
389 return map { $_ => $_ }
390 grep { !Mouse::Object->can($_) && $_ =~ $handles }
391 Mouse::Util::is_a_metarole($meta)
392 ? $meta->get_method_list
393 : $meta->get_all_method_names;
394 }
395 else {
396 $self->throw_error("Unable to canonicalize the 'handles' option with $handles");
397 }
398}
399
400sub _make_delegation_method {
401 my($self, $handle, $method_to_call) = @_;
637d4f17 402 return Mouse::Util::load_class($self->delegation_metaclass)
403 ->_generate_delegation($self, $handle, $method_to_call);
cbb81058 404}
405
fce211ae 406sub throw_error{
407 my $self = shift;
408
409 my $metaclass = (ref $self && $self->associated_class) || 'Mouse::Meta::Class';
410 $metaclass->throw_error(@_, depth => 1);
1bfebf5f 411}
412
c3398f5b 4131;
c3398f5b 414__END__
415
416=head1 NAME
417
bedd575c 418Mouse::Meta::Attribute - The Mouse attribute metaclass
c3398f5b 419
a25ca8d6 420=head1 VERSION
421
7e0e6837 422This document describes Mouse version 0.63
a25ca8d6 423
c3398f5b 424=head1 METHODS
425
1820fffe 426=head2 C<< new(%options) -> Mouse::Meta::Attribute >>
c3398f5b 427
306290e8 428Instantiates a new Mouse::Meta::Attribute. Does nothing else.
c3398f5b 429
1820fffe 430It adds the following options to the constructor:
c3398f5b 431
1820fffe 432=over 4
c3398f5b 433
612d3e1a 434=item C<< is => 'ro', 'rw', 'bare' >>
c3398f5b 435
1820fffe 436This provides a shorthand for specifying the C<reader>, C<writer>, or
437C<accessor> names. If the attribute is read-only ('ro') then it will
438have a C<reader> method with the same attribute as the name.
c3398f5b 439
1820fffe 440If it is read-write ('rw') then it will have an C<accessor> method
441with the same name. If you provide an explicit C<writer> for a
442read-write attribute, then you will have a C<reader> with the same
443name as the attribute, and a C<writer> with the name you provided.
c3398f5b 444
1820fffe 445Use 'bare' when you are deliberately not installing any methods
446(accessor, reader, etc.) associated with this attribute; otherwise,
447Moose will issue a deprecation warning when this attribute is added to a
448metaclass.
c3398f5b 449
612d3e1a 450=item C<< isa => Type >>
ab27a55e 451
1820fffe 452This option accepts a type. The type can be a string, which should be
453a type name. If the type name is unknown, it is assumed to be a class
454name.
ab27a55e 455
1820fffe 456This option can also accept a L<Moose::Meta::TypeConstraint> object.
ab27a55e 457
1820fffe 458If you I<also> provide a C<does> option, then your C<isa> option must
459be a class name, and that class must do the role specified with
460C<does>.
ab27a55e 461
612d3e1a 462=item C<< does => Role >>
ab27a55e 463
1820fffe 464This is short-hand for saying that the attribute's type must be an
465object which does the named role.
c3398f5b 466
1820fffe 467B<This option is not yet supported.>
c3398f5b 468
612d3e1a 469=item C<< coerce => Bool >>
ab27a55e 470
1820fffe 471This option is only valid for objects with a type constraint
472(C<isa>). If this is true, then coercions will be applied whenever
473this attribute is set.
c3398f5b 474
1820fffe 475You can make both this and the C<weak_ref> option true.
c3398f5b 476
612d3e1a 477=item C<< trigger => CodeRef >>
ab27a55e 478
1820fffe 479This option accepts a subroutine reference, which will be called after
480the attribute is set.
ab27a55e 481
612d3e1a 482=item C<< required => Bool >>
ab27a55e 483
1820fffe 484An attribute which is required must be provided to the constructor. An
485attribute which is required can also have a C<default> or C<builder>,
486which will satisfy its required-ness.
ab27a55e 487
1820fffe 488A required attribute must have a C<default>, C<builder> or a
489non-C<undef> C<init_arg>
ab27a55e 490
612d3e1a 491=item C<< lazy => Bool >>
ab27a55e 492
1820fffe 493A lazy attribute must have a C<default> or C<builder>. When an
494attribute is lazy, the default value will not be calculated until the
495attribute is read.
93f08899 496
612d3e1a 497=item C<< weak_ref => Bool >>
0fff36e6 498
1820fffe 499If this is true, the attribute's value will be stored as a weak
500reference.
c3398f5b 501
612d3e1a 502=item C<< auto_deref => Bool >>
fb706f5c 503
1820fffe 504If this is true, then the reader will dereference the value when it is
505called. The attribute must have a type constraint which defines the
506attribute as an array or hash reference.
507
612d3e1a 508=item C<< lazy_build => Bool >>
1820fffe 509
510Setting this to true makes the attribute lazy and provides a number of
511default methods.
fb706f5c 512
1820fffe 513 has 'size' => (
514 is => 'ro',
515 lazy_build => 1,
516 );
93d190e0 517
1820fffe 518is equivalent to this:
93d190e0 519
1820fffe 520 has 'size' => (
521 is => 'ro',
522 lazy => 1,
523 builder => '_build_size',
524 clearer => 'clear_size',
525 predicate => 'has_size',
526 );
93d190e0 527
1820fffe 528=back
529
e5e22afd 530=head2 C<< associate_method(MethodName) >>
31c5194b 531
532Associates a method with the attribute. Typically, this is called internally
533when an attribute generates its accessors.
534
e5e22afd 535Currently the argument I<MethodName> is ignored in Mouse.
31c5194b 536
1820fffe 537=head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >>
538
539Checks that the given value passes this attribute's type constraint. Returns C<true>
540on success, otherwise C<confess>es.
93d190e0 541
1820fffe 542=head2 C<< clone_and_inherit_options(options) -> Mouse::Meta::Attribute >>
f7b11a21 543
612d3e1a 544Creates a new attribute in the owner class, inheriting options from parent classes.
f7b11a21 545Accessors and helper methods are installed. Some error checking is done.
546
9ae9702e 547=head2 C<< get_read_method_ref >>
548
549=head2 C<< get_write_method_ref >>
550
551Returns the subroutine reference of a method suitable for reading or
552writing the attribute's value in the associated class. These methods
553always return a subroutine reference, regardless of whether or not the
df77fd72 554attribute is read- or write-only.
555
1820fffe 556=head1 SEE ALSO
f7b11a21 557
1820fffe 558L<Moose::Meta::Attribute>
f7b11a21 559
31c5194b 560L<Class::MOP::Attribute>
561
c3398f5b 562=cut
563