X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FClassEncapsulatedAttributes.pod;h=c1ddae88b83ec6be5c041af673fc4de6d623076b;hb=38d91ddc1c9368f0771aed719aeff0269c0bea61;hp=e14a17a545c189cf343e6f9b642e8e14e762f514;hpb=839ea97307cde7e936bff72d3f76d6213b9883a9;p=gitmo%2FClass-MOP.git diff --git a/examples/ClassEncapsulatedAttributes.pod b/examples/ClassEncapsulatedAttributes.pod index e14a17a..c1ddae8 100644 --- a/examples/ClassEncapsulatedAttributes.pod +++ b/examples/ClassEncapsulatedAttributes.pod @@ -5,30 +5,33 @@ package # hide the package from PAUSE use strict; use warnings; -our $VERSION = '0.05'; +our $VERSION = '0.06'; use base 'Class::MOP::Class'; sub initialize { (shift)->SUPER::initialize(@_, # use the custom attribute metaclass here - ':attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute' + 'attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute', ); } sub construct_instance { my ($class, %params) = @_; - my $meta_instance = Class::MOP::Instance->new($class); + + my $meta_instance = $class->get_meta_instance; + my $instance = $meta_instance->create_instance(); + + # initialize *ALL* attributes, including masked ones (as opposed to applicable) foreach my $current_class ($class->class_precedence_list()) { - $meta_instance->add_slot($current_class => {}) - unless $meta_instance->has_slot($current_class); my $meta = $current_class->meta; foreach my $attr_name ($meta->get_attribute_list()) { my $attr = $meta->get_attribute($attr_name); - $attr->initialize_instance_slot($meta, $meta_instance, \%params); + $attr->initialize_instance_slot($meta_instance, $instance, \%params); } } - return $meta_instance->get_instance; + + return $instance; } package # hide the package from PAUSE @@ -37,65 +40,37 @@ package # hide the package from PAUSE use strict; use warnings; -our $VERSION = '0.03'; +our $VERSION = '0.04'; use base 'Class::MOP::Attribute'; +# alter the way parameters are specified sub initialize_instance_slot { - my ($self, $class, $meta_instance, $params) = @_; + my ($self, $meta_instance, $instance, $params) = @_; # if the attr has an init_arg, use that, otherwise, # use the attributes name itself as the init_arg my $init_arg = $self->init_arg(); # try to fetch the init arg from the %params ... - my $val; + my $class = $self->associated_class; + my $val; $val = $params->{$class->name}->{$init_arg} if exists $params->{$class->name} && exists ${$params->{$class->name}}{$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($meta_instance->get_instance); + $val = $self->default($instance); } - # now add this to the instance structure - $meta_instance->get_slot_value($class->name)->{$self->name} = $val; -} - -sub generate_accessor_method { - my ($self, $attr_name) = @_; - my $class_name = $self->associated_class->name; - eval qq{sub { - \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1] if scalar(\@_) == 2; - \$_[0]->{'$class_name'}->{'$attr_name'}; - }}; -} - -sub generate_reader_method { - my ($self, $attr_name) = @_; - my $class_name = $self->associated_class->name; - eval qq{sub { - Carp::confess "Cannot assign a value to a read-only accessor" if \@_ > 1; - \$_[0]->{'$class_name'}->{'$attr_name'}; - }}; -} -sub generate_writer_method { - my ($self, $attr_name) = @_; - my $class_name = $self->associated_class->name; - eval qq{sub { - \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1]; - }}; + # now add this to the instance structure + $meta_instance->set_slot_value($instance, $self->name, $val); } -sub generate_predicate_method { - my ($self, $attr_name) = @_; - my $class_name = $self->associated_class->name; - eval qq{sub { - defined \$_[0]->{'$class_name'}->{'$attr_name'} ? 1 : 0; - }}; +sub name { + my $self = shift; + return ($self->associated_class->name . '::' . $self->SUPER::name) } -## &remove_attribute is left as an exercise for the reader :) - 1; __END__ @@ -158,13 +133,15 @@ is similar to how C++ handles its data members. Thanks to Yuval "nothingmuch" Kogman for the idea for this example. -=head1 AUTHOR +=head1 AUTHORS Stevan Little Estevan@iinteractive.comE +Yuval Kogman Enothingmuch@woobling.comE + =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L