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