From: Shawn M Moore Date: Sun, 27 Jan 2008 15:05:22 +0000 (+0000) Subject: Add Instance->get_all_slot_values and tests X-Git-Tag: 0_53~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad074154e47a36dde19708d11a97e44970114fdd;p=gitmo%2FClass-MOP.git Add Instance->get_all_slot_values and tests --- diff --git a/Changes b/Changes index e215ef0..ead9f30 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension Class-MOP. +0.53 + * Class::MOP::Instance + - added get_all_slot_values method (Sartak) + - added tests for this (Sartak) + 0.52 Tues. Jan. 22, 2008 * Class::MOP::Class - fixed bug in rebless_instance diff --git a/lib/Class/MOP/Instance.pm b/lib/Class/MOP/Instance.pm index baac84b..458b084 100644 --- a/lib/Class/MOP/Instance.pm +++ b/lib/Class/MOP/Instance.pm @@ -122,6 +122,20 @@ sub rebless_instance_structure { bless $instance, $metaclass->name; } +sub get_all_slot_values { + my ($self, $instance) = @_; + my $class = $self->associated_metaclass; + my %map; + + for my $attr ($class->compute_all_applicable_attributes) { + my $name = $attr->name; + $map{$name} = $self->get_slot_value($instance, $name) + if $self->is_slot_initialized($instance, $name); + } + + return \%map; +} + # inlinable operation snippets sub is_inlinable { 1 } @@ -274,6 +288,8 @@ require that the C<$instance_structure> is passed into them. =item B +=item B + =item B =item B diff --git a/t/023_attribute_get_read_write.t b/t/023_attribute_get_read_write.t index f5faa90..0b490bb 100644 --- a/t/023_attribute_get_read_write.t +++ b/t/023_attribute_get_read_write.t @@ -5,7 +5,7 @@ use warnings; use Scalar::Util 'blessed', 'reftype'; -use Test::More tests => 35; +use Test::More tests => 38; BEGIN { use_ok('Class::MOP'); @@ -34,6 +34,14 @@ and get_read/write_method_ref methods Foo->meta->add_attribute('gorch' => reader => { 'get_gorch', => sub { (shift)->{gorch} } } ); + + package Bar; + use metaclass; + Bar->meta->superclasses('Foo'); + + Bar->meta->add_attribute('quux' => + accessor => 'quux', + ); } can_ok('Foo', 'get_bar'); @@ -104,3 +112,27 @@ ok(!$gorch_attr->get_write_method, '... $attr does not have an write method'); is($reader->fully_qualified_name, 'Foo::get_gorch', '... it is the sub we are looking for'); } + +my $foo = bless {}, 'Foo'; +$foo->set_bar(1); +$foo->baz(10); + +is_deeply($foo->meta->get_meta_instance->get_all_slot_values($foo), { + bar => 1, + baz => 10, +}); + +my $bar = bless {}, 'Bar'; +$bar->set_bar(99); + +is_deeply($bar->meta->get_meta_instance->get_all_slot_values($bar), { + bar => 99, +}); + +$bar->quux(1337); + +is_deeply($bar->meta->get_meta_instance->get_all_slot_values($bar), { + bar => 99, + quux => 1337, +}); +