sub construct_instance {
my ($class, %params) = @_;
- my $instance = {};
+ my $meta_instance = Class::MOP::Instance->new($class);
foreach my $current_class ($class->class_precedence_list()) {
- $instance->{$current_class} = {}
- unless exists $instance->{$current_class};
+ $meta_instance->add_slot($current_class => {})
+ unless $meta_instance->has_slot($current_class);
my $meta = $current_class->meta;
foreach my $attr_name ($meta->get_attribute_list()) {
my $attr = $meta->get_attribute($attr_name);
- $attr->initialize_instance_slot($meta, $instance, \%params);
+ $attr->initialize_instance_slot($meta, $meta_instance, \%params);
}
}
- return $instance;
+ return $meta_instance->get_instance;
}
package # hide the package from PAUSE
use base 'Class::MOP::Attribute';
sub initialize_instance_slot {
- my ($self, $class, $instance, $params) = @_;
+ my ($self, $class, $meta_instance, $params) = @_;
# if the attr has an init_arg, use that, otherwise,
# use the attributes name itself as the init_arg
my $init_arg = $self->init_arg();
# if nothing was in the %params, we can use the
# attribute's default value (if it has one)
if (!defined $val && $self->has_default) {
- $val = $self->default($instance);
+ $val = $self->default($meta_instance->get_instance);
}
# now add this to the instance structure
- $instance->{$class->name}->{$self->name} = $val;
+ $meta_instance->get_slot_value($class->name)->{$self->name} = $val;
}
sub generate_accessor_method {
use base 'Class::MOP::Attribute';
sub initialize_instance_slot {
- my ($self, $class, $instance, $params) = @_;
+ my ($self, $class, $meta_instance, $params) = @_;
# if the attr has an init_arg, use that, otherwise,
# use the attributes name itself as the init_arg
my $init_arg = $self->init_arg();
# if nothing was in the %params, we can use the
# attribute's default value (if it has one)
if (!defined $val && $self->has_default) {
- $val = $self->default($instance);
+ $val = $self->default($meta_instance->get_instance);
}
# now add this to the instance structure
- $class->get_package_variable('%' . $self->name)->{ refaddr($instance) } = $val;
+ $class->get_package_variable('%' . $self->name)->{ refaddr($meta_instance->get_instance) } = $val;
}
sub generate_accessor_method {
use base 'Class::MOP::Attribute';
sub initialize_instance_slot {
- my ($self, $class, $instance, $params) = @_;
+ my ($self, $class, $meta_instance, $params) = @_;
# if the attr has an init_arg, use that, otherwise,
# use the attributes name itself as the init_arg
my $init_arg = $self->init_arg();
$val = $params->{$init_arg} if exists $params->{$init_arg};
# now add this to the instance structure
# only if we have found a value at all
- $instance->{$self->name} = $val if defined $val;
+ $meta_instance->add_slot($self->name, $val) if defined $val;
}
our $VERSION = '0.13';
+use Class::MOP::Instance;
+
# Self-introspection
sub meta { Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) }
sub construct_instance {
my ($class, %params) = @_;
- require Class::MOP::Instance;
my $meta_instance = Class::MOP::Instance->new($class);
foreach my $attr ($class->compute_all_applicable_attributes()) {
$attr->initialize_instance_slot($class, $meta_instance, \%params);
my $class = shift;
my $meta = shift;
bless {
- instance => bless {} => $meta->name
+ instance => (bless {} => $meta->name)
} => $class;
}
return $self->{instance}->{$slot_name} = $value;
}
+sub has_slot {
+ my ($self, $slot_name) = @_;
+ exists $self->{instance}->{$slot_name} ? 1 : 0;
+}
+
+sub get_slot_value {
+ my ($self, $slot_name) = @_;
+ return $self->{instance}->{$slot_name};
+}
+
+*set_slot_value = \&add_slot;
+
sub get_instance { (shift)->{instance} }
1;
=item B<add_slot>
+=item B<has_slot>
+
+=item B<get_slot_value>
+
+=item B<set_slot_value>
+
=item B<get_instance>
=back