From: Stevan Little Date: Sun, 12 Feb 2006 01:42:47 +0000 (+0000) Subject: fixing an example which I broke X-Git-Tag: 0_10~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0b8eb32530bfae7933da7ceb3371b5e11a9d7224;p=gitmo%2FClass-MOP.git fixing an example which I broke --- diff --git a/Changes b/Changes index 5508877..4b0a9f3 100644 --- a/Changes +++ b/Changes @@ -13,6 +13,9 @@ Revision history for Perl extension Class-MOP. * Class::MOP::Class - adding in &mixin method to do Scala style mixins + + * examples/ + - fixing the AttributesWithHistory example, it was broken 0.06 Thurs Feb. 9, 2006 * metaclass diff --git a/examples/AttributesWithHistory.pod b/examples/AttributesWithHistory.pod index 50f855b..da75135 100644 --- a/examples/AttributesWithHistory.pod +++ b/examples/AttributesWithHistory.pod @@ -5,7 +5,7 @@ package # hide the package from PAUSE use strict; use warnings; -our $VERSION = '0.02'; +our $VERSION = '0.03'; use base 'Class::MOP::Attribute'; @@ -25,7 +25,7 @@ __PACKAGE__->meta->add_attribute( __PACKAGE__->meta->add_attribute( Class::MOP::Attribute->new('_history' => ( accessor => '_history', - default => sub { [] }, + default => sub { {} }, )) ); @@ -34,7 +34,10 @@ __PACKAGE__->meta->add_attribute( sub generate_history_accessor_method { my ($self, $attr_name) = @_; eval qq{sub { - \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\}; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\}; }}; } @@ -42,7 +45,10 @@ sub generate_accessor_method { my ($self, $attr_name) = @_; eval qq{sub { if (scalar(\@_) == 2) { - push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; } \$_[0]->{'$attr_name'}; @@ -52,7 +58,10 @@ sub generate_accessor_method { sub generate_writer_method { my ($self, $attr_name) = @_; eval qq{sub { - push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; }}; } diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 28f1923..64b8f4e 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -10,7 +10,7 @@ use Sub::Name 'subname'; use B 'svref_2object'; use Clone (); -our $VERSION = '0.03'; +our $VERSION = '0.04'; # Self-introspection @@ -81,7 +81,6 @@ sub meta { Class::MOP::Class->initialize(blessed($_[0]) || $_[0]) } shift @class_list; # shift off $self->name foreach my $class_name (@class_list) { - next unless $METAS{$class_name}; my $meta = $METAS{$class_name}; ($self->isa(blessed($meta))) || confess $self->name . "->meta => (" . (blessed($self)) . ")" . diff --git a/t/104_AttributesWithHistory_test.t b/t/104_AttributesWithHistory_test.t index c83c950..5b74faa 100644 --- a/t/104_AttributesWithHistory_test.t +++ b/t/104_AttributesWithHistory_test.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 19; +use Test::More tests => 28; use File::Spec; BEGIN { @@ -41,11 +41,20 @@ can_ok($foo, 'set_bar'); can_ok($foo, 'get_bar'); can_ok($foo, 'get_bar_history'); +my $foo2 = Foo->new(); +isa_ok($foo2, 'Foo'); + is($foo->foo, undef, '... foo is not yet defined'); is_deeply( [ $foo->get_foo_history() ], [ ], '... got correct empty history for foo'); + +is($foo2->foo, undef, '... foo2 is not yet defined'); +is_deeply( + [ $foo2->get_foo_history() ], + [ ], + '... got correct empty history for foo2'); $foo->foo(42); is($foo->foo, 42, '... foo == 42'); @@ -54,6 +63,25 @@ is_deeply( [ 42 ], '... got correct history for foo'); +is($foo2->foo, undef, '... foo2 is still not yet defined'); +is_deeply( + [ $foo2->get_foo_history() ], + [ ], + '... still got correct empty history for foo2'); + +$foo2->foo(100); +is($foo->foo, 42, '... foo is still == 42'); +is_deeply( + [ $foo->get_foo_history() ], + [ 42 ], + '... still got correct history for foo'); + +is($foo2->foo, 100, '... foo2 == 100'); +is_deeply( + [ $foo2->get_foo_history() ], + [ 100 ], + '... got correct empty history for foo2'); + $foo->foo(43); $foo->foo(44); $foo->foo(45);