From: Shawn M Moore Date: Tue, 10 Jun 2008 04:01:36 +0000 (+0000) Subject: Use the correct hash keys with init_arg in the constructor X-Git-Tag: 0.04~54 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=384072a3e031806229e4b11dfb5c055e8829ff08;p=gitmo%2FMouse.git Use the correct hash keys with init_arg in the constructor --- diff --git a/lib/Mouse/Object.pm b/lib/Mouse/Object.pm index e72542c..70ad475 100644 --- a/lib/Mouse/Object.pm +++ b/lib/Mouse/Object.pm @@ -13,10 +13,11 @@ sub new { my $instance = bless {}, $class; for my $attribute (values %{ $class->meta->get_attribute_map }) { - my $key = $attribute->name; + my $from = $attribute->init_arg; + my $key = $attribute->name; my $default; - if (!exists($args{$key})) { + if (!exists($args{$from})) { if ($attribute->has_default || $attribute->has_builder) { unless ($attribute->is_lazy) { my $default = $attribute->default; @@ -43,17 +44,17 @@ sub new { } } - if (exists($args{$key})) { - $attribute->verify_type_constraint($args{$key}) + if (exists($args{$from})) { + $attribute->verify_type_constraint($args{$from}) if $attribute->has_type_constraint; - $instance->{$key} = $args{$key}; + $instance->{$key} = $args{$from}; weaken($instance->{$key}) if ref($instance->{$key}) && $attribute->is_weak_ref; if ($attribute->has_trigger) { - $attribute->trigger->($instance, $args{$key}, $attribute); + $attribute->trigger->($instance, $args{$from}, $attribute); } } } diff --git a/t/022-init-arg.t b/t/022-init-arg.t index 559c3b9..aa1bac1 100644 --- a/t/022-init-arg.t +++ b/t/022-init-arg.t @@ -20,9 +20,9 @@ is($object->{key}, undef, 'nothing in object->{init_arg}!'); is($object->{name}, 'default', 'value is in object->{name}'); my $object2 = Class->new(name => 'name', key => 'key'); -is($object2->name, 'name', 'attribute value is from name'); +is($object2->name, 'key', 'attribute value is from name'); is($object2->{key}, undef, 'no value for the init_arg'); -is($object2->{name}, 'name', 'value is in key from name'); +is($object2->{name}, 'key', 'value is in key from name'); my $attr = $object2->meta->get_attribute('name'); ok($attr, 'got the attribute object by name (not init_arg)');