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