}
sub initialize_instance_slot {
- my ($self, $instance, $params) = @_;
+ my ($self, $meta_instance, $instance, $params) = @_;
my $init_arg = $self->init_arg();
# try to fetch the init arg from the %params ...
+
my $val;
if (exists $params->{$init_arg}) {
$val = $params->{$init_arg};
confess "Attribute (" . $self->name . ") is required"
if $self->is_required && !$self->has_default;
}
+
# if nothing was in the %params, we can use the
# attribute's default value (if it has one)
if (!defined $val && $self->has_default) {
") with '$val'";
}
}
- $instance->{$self->name} = $val;
- if (defined $val && $self->is_weak_ref) {
- weaken($instance->{$self->name});
- }
+
+ $meta_instance->set_slot_value( $instance, $self->name, $val );
+ $meta_instance->weaken_slot_value( $instance, $self->name ) if ( ref $val && $self->is_weak_ref );
}
sub _inline_check_constraint {
my ( $self, $instance, $value ) = @_;
my $mi = $self->associated_class->get_meta_instance;
- my $slot_name = sprintf "'%s'", $self->slot_name;
+ my $slot_name = sprintf "'%s'", $self->name;
return ( $self->is_weak_ref
- ? $mi->inline_set_weak_slot_value( $instance, $slot_name, $value )
+ ? $mi->inline_set_slot_value_weak( $instance, $slot_name, $value )
: $mi->inline_set_slot_value( $instance, $slot_name, $value ) ) . ";";
}
return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
}
+sub _inline_get {
+ my ( $self, $instance ) = @_;
+
+ my $mi = $self->associated_class->get_meta_instance;
+ my $slot_name = sprintf "'%s'", $self->name;
+
+ return $mi->inline_get_slot_value( $instance, $slot_name );
+}
+
sub generate_accessor_method {
my ($attr, $attr_name) = @_;
my $value_name = $attr->should_coerce ? '$val' : '$_[1]';
my $mi = $attr->associated_class->get_meta_instance;
- my $slot_name = $attr->slot_name;
+ my $slot_name = sprintf "'%s'", $attr->name;
my $inv = '$_[0]';
my $code = 'sub { '
. 'if (scalar(@_) == 2) {'
'$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)'
. 'unless exists $_[0]->{$attr_name};'
: '')
- . 'return ' . $mi->inline_get_slot_value( '$_[0]', "'$slot_name'", $value_name ) . ';'
+ . 'return ' . $attr->_inline_get( $inv )
. ' }';
my $sub = eval $code;
warn "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@;
my $pkg = shift;
$class->SUPER::initialize($pkg,
':attribute_metaclass' => 'Moose::Meta::Attribute',
+ ':instance_metaclass' => 'Moose::Meta::Instance',
@_);
}
sub construct_instance {
my ($class, %params) = @_;
- my $instance = $params{'__INSTANCE__'} || $class->get_meta_instance->create_instance();
+ my $meta_instance = $class->get_meta_instance;
+ my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
foreach my $attr ($class->compute_all_applicable_attributes()) {
- $attr->initialize_instance_slot($instance, \%params)
+ $attr->initialize_instance_slot($meta_instance, $instance, \%params)
}
return $instance;
}