From: Stevan Little Date: Thu, 20 Apr 2006 15:36:57 +0000 (+0000) Subject: more X-Git-Tag: 0_26~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bd4e03f969edf4e9f30835d591baf521605a684b;p=gitmo%2FClass-MOP.git more --- diff --git a/Changes b/Changes index 240ec62..c5c4c5f 100644 --- a/Changes +++ b/Changes @@ -7,6 +7,14 @@ Revision history for Perl extension Class-MOP. - added get_all_metaclasses, get_all_metaclass_names and get_all_metaclass_instances method to allow access to all the cached metaclass objects. + - attribute slot initialization is now the responsibility + of the attribute itself, and construct_instance now + delegates appropriately + + * Class::MOP::Attribute + - attribute slot initialization is now the responsibility + of the attribute itself, so we added a method for it + called initialize_instance_slot 0.24 Tues. April 11, 2006 * Class::MOP::Class diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index 748bafa..305d70e 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -7,7 +7,7 @@ use warnings; use Carp 'confess'; use Scalar::Util 'blessed', 'reftype', 'weaken'; -our $VERSION = '0.05'; +our $VERSION = '0.06'; sub meta { require Class::MOP::Class; @@ -60,6 +60,20 @@ sub clone { return bless { %{$self}, %options } => blessed($self); } +sub initialize_instance_slot { + my ($self, $instance, $params) = @_; + my $init_arg = $self->init_arg(); + # try to fetch the init arg from the %params ... + my $val; + $val = $params->{$init_arg} if exists $params->{$init_arg}; + # if nothing was in the %params, we can use the + # attribute's default value (if it has one) + if (!defined $val && $self->has_default) { + $val = $self->default($instance); + } + $instance->{$self->name} = $val; +} + # NOTE: # the next bunch of methods will get bootstrapped # away in the Class::MOP bootstrapping section @@ -263,8 +277,6 @@ An attribute must (at the very least), have a C<$name>. All other C<%options> are contained added as key-value pairs. Acceptable keys are as follows: -=item B - =over 4 =item I @@ -375,6 +387,10 @@ defined, and false (C<0>) otherwise. =back +=item B + +=item B + =back =head2 Informational diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index cdece7e..26f5e12 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -9,7 +9,7 @@ use Scalar::Util 'blessed', 'reftype'; use Sub::Name 'subname'; use B 'svref_2object'; -our $VERSION = '0.11'; +our $VERSION = '0.12'; # Self-introspection @@ -177,16 +177,7 @@ sub construct_instance { my ($class, %params) = @_; my $instance = {}; foreach my $attr ($class->compute_all_applicable_attributes()) { - my $init_arg = $attr->init_arg(); - # try to fetch the init arg from the %params ... - my $val; - $val = $params{$init_arg} if exists $params{$init_arg}; - # if nothing was in the %params, we can use the - # attribute's default value (if it has one) - if (!defined $val && $attr->has_default) { - $val = $attr->default($instance); - } - $instance->{$attr->name} = $val; + $attr->initialize_instance_slot($instance, \%params); } return $instance; } diff --git a/t/014_attribute_introspection.t b/t/014_attribute_introspection.t index 12f8c94..c51c953 100644 --- a/t/014_attribute_introspection.t +++ b/t/014_attribute_introspection.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 39; +use Test::More tests => 40; use Test::Exception; BEGIN { @@ -22,6 +22,9 @@ BEGIN { my @methods = qw( meta new clone + + initialize_instance_slot + name has_accessor accessor has_writer writer