package # hide the package from PAUSE
- LazyClass;
+ LazyClass::Attribute;
use strict;
use warnings;
-our $VERSION = '0.02';
-
-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;
-}
+use Carp 'confess';
-package # hide the package from PAUSE
- LazyClass::Attribute;
+our $VERSION = '0.04';
-use strict;
-use warnings;
+use base 'Class::MOP::Attribute';
-our $VERSION = '0.02';
+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;
+}
-use base 'Class::MOP::Attribute';
sub generate_accessor_method {
my ($self, $attr_name) = @_;
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;
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',
default => sub { BinaryTree->new() }
));
- sub new {
+ sub new {
my $class = shift;
- bless $class->meta->construct_instance(@_) => $class;
+ $class->meta->new_object(@_);
}
# ... later in code