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
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 }
=item B<set_slot_value ($instance_structure, $slot_name, $value)>
+=item B<get_all_slot_values ($instance_structure)>
+
=item B<initialize_slot ($instance_structure, $slot_name)>
=item B<deinitialize_slot ($instance_structure, $slot_name)>
use Scalar::Util 'blessed', 'reftype';
-use Test::More tests => 35;
+use Test::More tests => 38;
BEGIN {
use_ok('Class::MOP');
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');
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,
+});
+