0.38
[gitmo/Class-MOP.git] / examples / LazyClass.pod
index 38482ec..839b609 100644 (file)
@@ -7,12 +7,12 @@ use warnings;
 
 use Carp 'confess';
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 use base 'Class::MOP::Attribute';
 
 sub initialize_instance_slot {
-    my ($self, $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
@@ -20,50 +20,75 @@ sub initialize_instance_slot {
 
        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);
+               $meta_instance->set_slot_value($instance, $self->name, $val);
        }
 }
 
+sub accessor_metaclass { 'LazyClass::Method::Accessor' }
+
+package # hide the package from PAUSE
+    LazyClass::Method::Accessor;
+
+use strict;
+use warnings;
+
+use Carp 'confess';
+
+our $VERSION = '0.01';
+
+use base 'Class::MOP::Method::Accessor';
+
 sub generate_accessor_method {
-    my $attr = shift;
+    my $attr = (shift)->associated_attribute;
 
-       my $slot_name = $attr->slot_name;
+       my $attr_name = $attr->name;
        my $meta_instance = $attr->associated_class->get_meta_instance;
 
     sub {
         if (scalar(@_) == 2) {
-                       $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $_[1] );
+                       $meta_instance->set_slot_value($_[0], $attr_name, $_[1]);
         }
         else {
-                       unless ( $meta_instance->slot_initialized( $_[0], $slot_name ) ) {
+                       unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
                                my $value = $attr->has_default ? $attr->default($_[0]) : undef;
-                               $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $value );
+                               $meta_instance->set_slot_value($_[0], $attr_name, $value);
             }
 
-            $meta_instance->get_slot_value( $_[0], $slot_name );
+            $meta_instance->get_slot_value($_[0], $attr_name);
         }
     };
 }
 
 sub generate_reader_method {
-       my $attr = shift;
+    my $attr = (shift)->associated_attribute;
 
-       my $slot_name = $attr->slot_name;
+       my $attr_name = $attr->name;
        my $meta_instance = $attr->associated_class->get_meta_instance;
 
     sub {
         confess "Cannot assign a value to a read-only accessor" if @_ > 1;        
 
-               unless ( $meta_instance->slot_initialized( $_[0], $slot_name ) ) {
+               unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
                        my $value = $attr->has_default ? $attr->default($_[0]) : undef;
-                       $meta_instance->set_slot_value_with_init( $_[0], $slot_name, $value );
+                       $meta_instance->set_slot_value($_[0], $attr_name, $value);
                }
 
-               $meta_instance->get_slot_value( $_[0], $slot_name );
+               $meta_instance->get_slot_value($_[0], $attr_name);
     };   
 }
 
+package # hide the package from PAUSE
+    LazyClass::Instance;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+use base 'Class::MOP::Instance';
+
+sub initialize_all_slots {}
+
 1;
 
 __END__
@@ -78,8 +103,9 @@ LazyClass - An example metaclass with lazy initialization
 
   package BinaryTree;
   
-  use metaclass 'Class::MOP::Class' => (
-      ':attribute_metaclass' => 'LazyClass::Attribute'
+  use metaclass (
+      ':attribute_metaclass' => 'LazyClass::Attribute',
+      ':instance_metaclass'  => 'LazyClass::Instance',      
   );
   
   BinaryTree->meta->add_attribute('$:node' => (
@@ -119,13 +145,15 @@ without complicating the programing of it. This would also be
 ideal for a class which has a large amount of attributes, 
 several of which are optional. 
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
+Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2006, 2007 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>