instance-protocol
[gitmo/Class-MOP.git] / examples / ClassEncapsulatedAttributes.pod
index 7c19e7a..8e84dbd 100644 (file)
@@ -5,37 +5,30 @@ package # hide the package from PAUSE
 use strict;
 use warnings;
 
-use Class::MOP 'meta';
-
-our $VERSION = '0.01';
+our $VERSION = '0.06';
 
 use base 'Class::MOP::Class';
 
+sub initialize { 
+    (shift)->SUPER::initialize(@_, 
+        # use the custom attribute metaclass here 
+        ':attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute' 
+    );
+}
+
 sub construct_instance {
     my ($class, %params) = @_;
-    my $instance = {};
+    my $meta_instance = Class::MOP::Instance->new($class);
     foreach my $current_class ($class->class_precedence_list()) {
-        $instance->{$current_class} = {} 
-            unless exists $instance->{$current_class};
-        my $meta = $class->initialize($current_class);
+        $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);
-            # 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{$current_class}->{$init_arg} 
-                if exists $params{$current_class} && 
-                   exists ${$params{$current_class}}{$init_arg};
-            # if nothing was in the %params, we can use the 
-            # attribute's default value (if it has one)
-            $val ||= $attr->default($instance) if $attr->has_default();
-            # now add this to the instance structure
-            $instance->{$current_class}->{$attr_name} = $val;
+            $attr->initialize_instance_slot($meta, $meta_instance, \%params);
         }
     }  
-    return $instance;
+    return $meta_instance->get_instance;
 }
 
 package # hide the package from PAUSE
@@ -44,12 +37,32 @@ package # hide the package from PAUSE
 use strict;
 use warnings;
 
-use Class::MOP 'meta';
-
-our $VERSION = '0.01';
+our $VERSION = '0.04';
 
 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->{$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); 
+    }
+    # now add this to the instance structure
+    $meta_instance->get_slot_value(
+        $meta_instance->get_instance, 
+        $class->name
+    )->{$self->name} = $val;   
+}
+
 sub generate_accessor_method {
     my ($self, $attr_name) = @_;
     my $class_name = $self->associated_class->name;
@@ -63,6 +76,7 @@ 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'};
     }};   
 }
@@ -99,30 +113,26 @@ ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulat
 
   package Foo;
   
-  sub meta { ClassEncapsulatedAttributes->initialize($_[0]) }
+  use metaclass 'ClassEncapsulatedAttributes';
   
-  Foo->meta->add_attribute(
-      ClassEncapsulatedAttributes::Attribute->new('foo' => (
-          accessor  => 'Foo_foo',
-          default   => 'init in FOO'
-      ))
-  );   
+  Foo->meta->add_attribute('foo' => (
+      accessor  => 'Foo_foo',
+      default   => 'init in FOO'
+  ));
   
   sub new  {
       my $class = shift;
-      bless $class->meta->construct_instance(@_) => $class;
+      $class->meta->new_object(@_);
   }
   
   package Bar;
   our @ISA = ('Foo');
   
   # duplicate the attribute name here
-  Bar->meta->add_attribute(
-      ClassEncapsulatedAttributes::Attribute->new('foo' => (
-          accessor  => 'Bar_foo',
-          default   => 'init in BAR'            
-      ))
-  );      
+  Bar->meta->add_attribute('foo' => (
+      accessor  => 'Bar_foo',
+      default   => 'init in BAR'            
+  ));      
   
   # ... later in other code ...