$args{name} = $name;
- my $instance = bless \%args, $class;
+ my $self = bless \%args, $class;
# extra attributes
if($class ne __PACKAGE__){
- $class->meta->_initialize_instance($instance,\%args);
+ $class->meta->_initialize_object($self, \%args);
}
# XXX: there is no fast way to check attribute validity
# Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad");
# }
- return $instance
+ return $self;
}
# readers
sub should_auto_deref { $_[0]->{auto_deref} }
sub should_coerce { $_[0]->{coerce} }
-sub get_read_method { $_[0]->{reader} || $_[0]->{accessor} }
-sub get_write_method { $_[0]->{writer} || $_[0]->{accessor} }
-
# predicates
sub has_accessor { exists $_[0]->{accessor} }
return $self;
}
+sub _coerce_and_verify {
+ my($self, $value, $instance) = @_;
+
+ my $type_constraint = $self->{type_constraint};
+
+ return $value if !$type_constraint;
+
+ if ($self->should_coerce && $type_constraint->has_coercion) {
+ $value = $type_constraint->coerce($value);
+ }
+
+ return $value if $type_constraint->check($value);
+
+ $self->verify_against_type_constraint($value);
+
+ return $value;
+}
+
sub verify_against_type_constraint {
my ($self, $value) = @_;
- my $tc = $self->type_constraint;
- return 1 unless $tc;
- local $_ = $value;
- return 1 if $tc->check($value);
+ my $type_constraint = $self->{type_constraint};
+ return 1 if !$type_constraint;;
+ return 1 if $type_constraint->check($value);
- $self->verify_type_constraint_error($self->name, $value, $tc);
+ $self->verify_type_constraint_error($self->name, $value, $type_constraint);
}
sub verify_type_constraint_error {
sub coerce_constraint { ## my($self, $value) = @_;
my $type = $_[0]->{type_constraint}
or return $_[1];
+
+ Carp::cluck("coerce_constraint() has been deprecated, which was an internal utility anyway");
+
return Mouse::Util::TypeConstraints->typecast_constraints($_[0]->associated_class->name, $type, $_[1]);
}
$self->throw_error("Could not find an attribute by the name of '$name' to inherit from");
}
+
+#sub get_read_method { $_[0]->{reader} || $_[0]->{accessor} }
+#sub get_write_method { $_[0]->{writer} || $_[0]->{accessor} }
+
+sub get_read_method_ref{
+ my($self) = @_;
+
+ $self->{_read_method_ref} ||= do{
+ my $metaclass = $self->associated_class
+ or $self->throw_error('No asocciated class for ' . $self->name);
+
+ my $reader = $self->{reader} || $self->{accessor};
+ if($reader){
+ $metaclass->name->can($reader);
+ }
+ else{
+ Mouse::Meta::Method::Accessor->_generate_reader($self, undef, $metaclass);
+ }
+ };
+}
+
+sub get_write_method_ref{
+ my($self) = @_;
+
+ $self->{_write_method_ref} ||= do{
+ my $metaclass = $self->associated_class
+ or $self->throw_error('No asocciated class for ' . $self->name);
+
+ my $reader = $self->{writer} || $self->{accessor};
+ if($reader){
+ $metaclass->name->can($reader);
+ }
+ else{
+ Mouse::Meta::Method::Accessor->_generate_writer($self, undef, $metaclass);
+ }
+ };
+}
+
sub associate_method{
my ($attribute, $method) = @_;
$attribute->{associated_methods}++;
foreach my $type(qw(accessor reader writer predicate clearer handles)){
if(exists $attribute->{$type}){
- my $installer = '_install_' . $type;
+ my $installer = '_generate_' . $type;
Mouse::Meta::Method::Accessor->$installer($attribute, $attribute->{$type}, $metaclass);
=head1 NAME
-Mouse::Meta::Attribute - attribute metaclass
+Mouse::Meta::Attribute - The Mouse attribute metaclass
=head1 METHODS
=back
+=head2 C<< associate_method(Method) >>
+
+Associates a method with the attribute. Typically, this is called internally
+when an attribute generates its accessors.
+
+Currently the argument I<Method> is ignored in Mouse.
+
=head2 C<< verify_against_type_constraint(Item) -> TRUE | ERROR >>
Checks that the given value passes this attribute's type constraint. Returns C<true>
L<Moose::Meta::Attribute>
+L<Class::MOP::Attribute>
+
=cut