use Carp 'confess';
use Scalar::Util 'blessed', 'reftype', 'weaken';
-our $VERSION = '0.12';
+our $VERSION = '0.14';
our $AUTHORITY = 'cpan:STEVAN';
use base 'Class::MOP::Object';
if exists $options{default} && ref $options{default};
bless {
- name => $name,
- accessor => $options{accessor},
- reader => $options{reader},
- writer => $options{writer},
- predicate => $options{predicate},
- clearer => $options{clearer},
- init_arg => $options{init_arg},
- default => $options{default},
+ '$!name' => $name,
+ '$!accessor' => $options{accessor},
+ '$!reader' => $options{reader},
+ '$!writer' => $options{writer},
+ '$!predicate' => $options{predicate},
+ '$!clearer' => $options{clearer},
+ '$!init_arg' => $options{init_arg},
+ '$!default' => $options{default},
# keep a weakened link to the
# class we are associated with
- associated_class => undef,
+ '$!associated_class' => undef,
+ # and a list of the methods
+ # associated with this attr
+ '@!associated_methods' => [],
} => $class;
}
sub initialize_instance_slot {
my ($self, $meta_instance, $instance, $params) = @_;
- my $init_arg = $self->{init_arg};
+ my $init_arg = $self->{'$!init_arg'};
# try to fetch the init arg from the %params ...
my $val;
$val = $params->{$init_arg} if exists $params->{$init_arg};
# if nothing was in the %params, we can use the
# attribute's default value (if it has one)
- if (!defined $val && defined $self->{default}) {
+ if (!defined $val && defined $self->{'$!default'}) {
$val = $self->default($instance);
}
$meta_instance->set_slot_value($instance, $self->name, $val);
# the next bunch of methods will get bootstrapped
# away in the Class::MOP bootstrapping section
-sub name { $_[0]->{name} }
+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 }
-sub has_writer { defined($_[0]->{writer}) ? 1 : 0 }
-sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
-sub has_clearer { defined($_[0]->{clearer}) ? 1 : 0 }
-sub has_init_arg { defined($_[0]->{init_arg}) ? 1 : 0 }
-sub has_default { defined($_[0]->{default}) ? 1 : 0 }
+sub has_accessor { defined($_[0]->{'$!accessor'}) ? 1 : 0 }
+sub has_reader { defined($_[0]->{'$!reader'}) ? 1 : 0 }
+sub has_writer { defined($_[0]->{'$!writer'}) ? 1 : 0 }
+sub has_predicate { defined($_[0]->{'$!predicate'}) ? 1 : 0 }
+sub has_clearer { defined($_[0]->{'$!clearer'}) ? 1 : 0 }
+sub has_init_arg { defined($_[0]->{'$!init_arg'}) ? 1 : 0 }
+sub has_default { defined($_[0]->{'$!default'}) ? 1 : 0 }
-sub accessor { $_[0]->{accessor} }
-sub reader { $_[0]->{reader} }
-sub writer { $_[0]->{writer} }
-sub predicate { $_[0]->{predicate} }
-sub clearer { $_[0]->{clearer} }
-sub init_arg { $_[0]->{init_arg} }
+sub accessor { $_[0]->{'$!accessor'} }
+sub reader { $_[0]->{'$!reader'} }
+sub writer { $_[0]->{'$!writer'} }
+sub predicate { $_[0]->{'$!predicate'} }
+sub clearer { $_[0]->{'$!clearer'} }
+sub init_arg { $_[0]->{'$!init_arg'} }
# end bootstrapped away method section.
# (all methods below here are kept intact)
sub is_default_a_coderef {
- ('CODE' eq (reftype($_[0]->{default}) || ''))
+ ('CODE' eq (reftype($_[0]->{'$!default'} || $_[0]->{default}) || ''))
}
sub default {
# we pass in the instance and default
# can return a value based on that
# instance. Somewhat crude, but works.
- return $self->{default}->($instance);
+ return $self->{'$!default'}->($instance);
}
- $self->{default};
+ $self->{'$!default'};
}
# slots
my ($self, $class) = @_;
(blessed($class) && $class->isa('Class::MOP::Class'))
|| confess "You must pass a Class::MOP::Class instance (or a subclass)";
- weaken($self->{associated_class} = $class);
+ weaken($self->{'$!associated_class'} = $class);
}
sub detach_from_class {
my $self = shift;
- $self->{associated_class} = undef;
+ $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);
eval {
$method = $self->accessor_metaclass->new(
attribute => $self,
- as_inline => $inline_me,
+ is_inline => $inline_me,
accessor_type => $type,
);
};
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