more-method-refactoring
[gitmo/Class-MOP.git] / examples / InsideOutClass.pod
index fdd1691..e99237e 100644 (file)
@@ -5,7 +5,7 @@ package # hide the package from PAUSE
 use strict;
 use warnings;
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 use Carp         'confess';
 use Scalar::Util 'refaddr';
@@ -23,17 +23,32 @@ sub initialize_instance_slot {
     if (!defined $val && defined $self->{default}) {
         $val = $self->default($instance);
     }
-    $self->associated_class
-         ->get_meta_instance
-         ->set_slot_value($instance, $self->name, $val);
+    my $_meta_instance = $self->associated_class->get_meta_instance;
+    $_meta_instance->initialize_slot($instance, $self->name);
+    $_meta_instance->set_slot_value($instance, $self->name, $val);
 }
 
+sub accessor_metaclass { 'InsideOutClass::Method::Accessor' }
+
+package # hide the package from PAUSE
+    InsideOutClass::Method::Accessor;
+    
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+use Carp         'confess';
+use Scalar::Util 'refaddr';
+
+use base 'Class::MOP::Method::Accessor';
+
 ## Method generation helpers
 
 sub generate_accessor_method {
-    my $self = shift;
-    my $meta_class = $self->associated_class;  
-    my $attr_name  = $self->name;
+    my $attr       = (shift)->associated_attribute;
+    my $meta_class = $attr->associated_class;  
+    my $attr_name  = $attr->name;
     return sub {
         my $meta_instance = $meta_class->get_meta_instance;
         $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
@@ -42,9 +57,9 @@ sub generate_accessor_method {
 }
 
 sub generate_reader_method {
-    my $self = shift;
-    my $meta_class = $self->associated_class;    
-    my $attr_name  = $self->name;
+    my $attr       = (shift)->associated_attribute;
+    my $meta_class = $attr->associated_class;  
+    my $attr_name  = $attr->name;
     return sub { 
         confess "Cannot assign a value to a read-only accessor" if @_ > 1;
         $meta_class->get_meta_instance
@@ -53,9 +68,9 @@ sub generate_reader_method {
 }
 
 sub generate_writer_method {
-    my $self = shift;
-    my $meta_class = $self->associated_class;    
-    my $attr_name  = $self->name;
+    my $attr       = (shift)->associated_attribute;
+    my $meta_class = $attr->associated_class;  
+    my $attr_name  = $attr->name;
     return sub { 
         $meta_class->get_meta_instance
                    ->set_slot_value($_[0], $attr_name, $_[1]);
@@ -63,9 +78,9 @@ sub generate_writer_method {
 }
 
 sub generate_predicate_method {
-    my $self = shift;
-    my $meta_class = $self->associated_class;   
-    my $attr_name  = $self->name;
+    my $attr       = (shift)->associated_attribute;
+    my $meta_class = $attr->associated_class;  
+    my $attr_name  = $attr->name;
     return sub { 
         defined $meta_class->get_meta_instance
                            ->get_slot_value($_[0], $attr_name) ? 1 : 0;
@@ -92,25 +107,25 @@ sub create_instance {
 
 sub get_slot_value {
        my ($self, $instance, $slot_name) = @_;
-       $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance};
+       $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance};
 }
 
 sub set_slot_value {
        my ($self, $instance, $slot_name, $value) = @_;
-       $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = $value;
+       $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} = $value;
 }
 
 sub initialize_slot {
     my ($self, $instance, $slot_name) = @_;
-    $self->{meta}->add_package_variable(('%' . $slot_name) => {})
-        unless $self->{meta}->has_package_variable('%' . $slot_name); 
-    $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} = undef;
+    $self->{meta}->add_package_symbol(('%' . $slot_name) => {})
+        unless $self->{meta}->has_package_symbol('%' . $slot_name); 
+    $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} = undef;
 }
 
 sub is_slot_initialized {
        my ($self, $instance, $slot_name) = @_;
-       return 0 unless $self->{meta}->has_package_variable('%' . $slot_name);
-       return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
+       return 0 unless $self->{meta}->has_package_symbol('%' . $slot_name);
+       return exists $self->{meta}->get_package_symbol('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
 }
 
 1;
@@ -162,7 +177,7 @@ inside-out objects to be C<DESTROY>-ed, and some other details as
 well (threading, etc), but this is an example. A real implementation is left as
 an exercise to the reader.
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>