X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FLazyClass.pod;h=184793986b4c1551524b7c33b893ec8fc824bf6b;hb=839ea97307cde7e936bff72d3f76d6213b9883a9;hp=df9e4b59d5335d332a9b5be83a76be3e367c2fa1;hpb=046688ed71ac338d09fad7abdd17833409826113;p=gitmo%2FClass-MOP.git diff --git a/examples/LazyClass.pod b/examples/LazyClass.pod index df9e4b5..1847939 100644 --- a/examples/LazyClass.pod +++ b/examples/LazyClass.pod @@ -1,45 +1,30 @@ package # hide the package from PAUSE - LazyClass; - -use strict; -use warnings; - -use Class::MOP 'meta'; - -our $VERSION = '0.01'; - -use base 'Class::MOP::Class'; - -sub construct_instance { - my ($class, %params) = @_; - my $instance = {}; - foreach my $attr (map { $_->{attribute} } $class->compute_all_applicable_attributes()) { - # if the attr has an init_arg, use that, otherwise, - # use the attributes name itself as the init_arg - my $init_arg = $attr->has_init_arg() ? $attr->init_arg() : $attr->name; - # 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 - $instance->{$attr->name} = $val if defined $val; - } - return $instance; -} - -package # hide the package from PAUSE LazyClass::Attribute; use strict; use warnings; -use Class::MOP 'meta'; +use Carp 'confess'; -our $VERSION = '0.01'; +our $VERSION = '0.03'; use base 'Class::MOP::Attribute'; +sub initialize_instance_slot { + my ($self, $class, $meta_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; +} + + sub generate_accessor_method { my ($self, $attr_name) = @_; sub { @@ -59,6 +44,7 @@ sub generate_accessor_method { sub generate_reader_method { my ($self, $attr_name) = @_; 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; @@ -81,11 +67,9 @@ LazyClass - An example metaclass with lazy initialization package BinaryTree; - sub meta { - LazyClass->initialize($_[0] => ( - ':attribute_metaclass' => 'LazyClass::Attribute' - )); - } + use metaclass 'Class::MOP::Class' => ( + ':attribute_metaclass' => 'LazyClass::Attribute' + ); BinaryTree->meta->add_attribute('$:node' => ( accessor => 'node', @@ -102,9 +86,9 @@ LazyClass - An example metaclass with lazy initialization default => sub { BinaryTree->new() } )); - sub new { + sub new { my $class = shift; - bless $class->meta->construct_instance(@_) => $class; + $class->meta->new_object(@_); } # ... later in code