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