X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FLazyClass.pod;h=38482ece5df9c456bb5055fc16e397e5a39e2f4a;hb=2d711cc8f03b6d8cbfe53f9628883ff33582ed03;hp=3ce659b8a15e27e85ed8c9f2e04cc10af0b72750;hpb=2bab2be690fec92f81ec4174ae83e09bde362ca7;p=gitmo%2FClass-MOP.git diff --git a/examples/LazyClass.pod b/examples/LazyClass.pod index 3ce659b..38482ec 100644 --- a/examples/LazyClass.pod +++ b/examples/LazyClass.pod @@ -12,44 +12,55 @@ our $VERSION = '0.04'; use base 'Class::MOP::Attribute'; sub initialize_instance_slot { - my ($self, $class, $meta_instance, $params) = @_; + my ($self, $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; - $val = $params->{$init_arg} if exists $params->{$init_arg}; - # now add this to the instance structure - # only if we have found a value at all - $meta_instance->add_slot($self->name, $val) if defined $val; -} + if ( exists $params->{$init_arg} ) { + my $val = $params->{$init_arg}; + my $meta_instance = $self->associated_class->get_meta_instance; + $meta_instance->set_slot_value_with_init( $instance, $self->slot_name, $val); + } +} sub generate_accessor_method { - my ($self, $attr_name) = @_; + my $attr = shift; + + my $slot_name = $attr->slot_name; + my $meta_instance = $attr->associated_class->get_meta_instance; + sub { if (scalar(@_) == 2) { - $_[0]->{$attr_name} = $_[1]; + $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $_[1] ); } else { - if (!exists $_[0]->{$attr_name}) { - my $attr = $self->associated_class->get_attribute($attr_name); - $_[0]->{$attr_name} = $attr->has_default ? $attr->default($_[0]) : undef; - } - $_[0]->{$attr_name}; + unless ( $meta_instance->slot_initialized( $_[0], $slot_name ) ) { + my $value = $attr->has_default ? $attr->default($_[0]) : undef; + $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $value ); + } + + $meta_instance->get_slot_value( $_[0], $slot_name ); } }; } sub generate_reader_method { - my ($self, $attr_name) = @_; + my $attr = shift; + + my $slot_name = $attr->slot_name; + my $meta_instance = $attr->associated_class->get_meta_instance; + sub { confess "Cannot assign a value to a read-only accessor" if @_ > 1; - if (!exists $_[0]->{$attr_name}) { - my $attr = $self->associated_class->get_attribute($attr_name); - $_[0]->{$attr_name} = $attr->has_default ? $attr->default($_[0]) : undef; - } - $_[0]->{$attr_name}; + + unless ( $meta_instance->slot_initialized( $_[0], $slot_name ) ) { + my $value = $attr->has_default ? $attr->default($_[0]) : undef; + $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $value ); + } + + $meta_instance->get_slot_value( $_[0], $slot_name ); }; } @@ -121,4 +132,4 @@ L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. -=cut \ No newline at end of file +=cut