# keep a weakened link to the
# class we are associated with
associated_class => undef,
+ # and a list of the methods
+ # associated with this attr
+ associated_methods => [],
} => $class;
}
sub name { $_[0]->{name} }
-sub associated_class { $_[0]->{associated_class} }
+sub associated_class { $_[0]->{associated_class} }
+sub associated_methods { $_[0]->{associated_methods} }
sub has_accessor { defined($_[0]->{accessor}) ? 1 : 0 }
sub has_reader { defined($_[0]->{reader}) ? 1 : 0 }
$self->{associated_class} = undef;
}
+# method association
+
+sub associate_method {
+ my ($self, $method) = @_;
+ push @{$self->{associated_methods}} => $method;
+}
+
## Slot management
sub set_value {
my ($self, $instance, $value) = @_;
- Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
+ Class::MOP::Class->initialize(blessed($instance))
->get_meta_instance
- ->set_slot_value( $instance, $self->name, $value );
+ ->set_slot_value($instance, $self->name, $value);
}
sub get_value {
my ($self, $instance) = @_;
- Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
+ Class::MOP::Class->initialize(blessed($instance))
->get_meta_instance
->get_slot_value($instance, $self->name);
}
+sub has_value {
+ my ($self, $instance) = @_;
+
+ defined Class::MOP::Class->initialize(blessed($instance))
+ ->get_meta_instance
+ ->get_slot_value($instance, $self->name) ? 1 : 0;
+}
+
+sub clear_value {
+ my ($self, $instance) = @_;
+
+ Class::MOP::Class->initialize(blessed($instance))
+ ->get_meta_instance
+ ->deinitialize_slot($instance, $self->name);
+}
+
## load em up ...
sub accessor_metaclass { 'Class::MOP::Method::Accessor' }
(reftype($accessor) eq 'HASH')
|| confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
my ($name, $method) = %{$accessor};
- return ($name, $self->accessor_metaclass->wrap($method));
+ $method = $self->accessor_metaclass->wrap($method);
+ $self->associate_method($method);
+ return ($name, $method);
}
else {
my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);
);
};
confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;
+ $self->associate_method($method);
return ($accessor, $method);
}
}
=over 4
-=item set_value $instance, $value
+=item B<set_value ($instance, $value)>
Set the value without going through the accessor. Note that this may be done to
even attributes with just read only accessors.
-=item get_value $instance
+=item B<get_value ($instance)>
Return the value without going through the accessor. Note that this may be done
even to attributes with just write only accessors.
+=item B<has_value ($instance)>
+
+=item B<clear_value ($instance)>
+
=back
=head2 Informational
=item B<detach_from_class>
-=item B<slot_name>
-
-=item B<allocate_slots>
-
-=item B<deallocate_slots>
-
=back
=head2 Attribute Accessor generation
=item B<accessor_metaclass>
+=item B<associate_method>
+
+=item B<associated_methods>
+
=item B<install_accessors>
This allows the attribute to generate and install code for it's own
use base 'Class::MOP::Method';
-=pod
-
-So, the idea here is that we have an accessor class
-which takes a weak-link to the attribute and can
-generate the actual code ref needed. This might allow
-for more varied approaches.
-
-And if the attribute type can also declare what
-kind of accessor method metaclass it uses, then
-this relationship can be handled by delegation.
-
-=cut
-
sub new {
my $class = shift;
my %options = @_;
($self->as_inline ? 'inline' : ())
);
- eval {
- $self->{body} = $self->$method_name();
- };
+ eval { $self->{body} = $self->$method_name() };
die $@ if $@;
}
}
sub generate_predicate_method {
- my $attr = (shift)->associated_attribute;
- my $attr_name = $attr->name;
+ my $attr = (shift)->associated_attribute;
return sub {
- defined Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
- ->get_meta_instance
- ->get_slot_value($_[0], $attr_name) ? 1 : 0;
+ $attr->has_value($_[0])
};
}
sub generate_clearer_method {
- my $attr = (shift)->associated_attribute;
- my $attr_name = $attr->name;
+ my $attr = (shift)->associated_attribute;
return sub {
- Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
- ->get_meta_instance
- ->deinitialize_slot($_[0], $attr_name);
+ $attr->clear_value($_[0])
};
}