foreach my $key ( keys %params ), not foreach my $key ( %params )
[gitmo/Class-MOP.git] / examples / InsideOutClass.pod
index aaf581a..fdd1691 100644 (file)
@@ -1,4 +1,76 @@
 
+package # hide the package from PAUSE
+    InsideOutClass::Attribute;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+use Carp         'confess';
+use Scalar::Util 'refaddr';
+
+use base 'Class::MOP::Attribute';
+
+sub initialize_instance_slot {
+    my ($self, $meta_instance, $instance, $params) = @_;
+    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};
+    # if nothing was in the %params, we can use the 
+    # attribute's default value (if it has one)
+    if (!defined $val && defined $self->{default}) {
+        $val = $self->default($instance);
+    }
+    $self->associated_class
+         ->get_meta_instance
+         ->set_slot_value($instance, $self->name, $val);
+}
+
+## Method generation helpers
+
+sub generate_accessor_method {
+    my $self = shift;
+    my $meta_class = $self->associated_class;  
+    my $attr_name  = $self->name;
+    return sub {
+        my $meta_instance = $meta_class->get_meta_instance;
+        $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
+        $meta_instance->get_slot_value($_[0], $attr_name);
+    };
+}
+
+sub generate_reader_method {
+    my $self = shift;
+    my $meta_class = $self->associated_class;    
+    my $attr_name  = $self->name;
+    return sub { 
+        confess "Cannot assign a value to a read-only accessor" if @_ > 1;
+        $meta_class->get_meta_instance
+                   ->get_slot_value($_[0], $attr_name); 
+    }; 
+}
+
+sub generate_writer_method {
+    my $self = shift;
+    my $meta_class = $self->associated_class;    
+    my $attr_name  = $self->name;
+    return sub { 
+        $meta_class->get_meta_instance
+                   ->set_slot_value($_[0], $attr_name, $_[1]);
+    };
+}
+
+sub generate_predicate_method {
+    my $self = shift;
+    my $meta_class = $self->associated_class;   
+    my $attr_name  = $self->name;
+    return sub { 
+        defined $meta_class->get_meta_instance
+                           ->get_slot_value($_[0], $attr_name) ? 1 : 0;
+    };   
+}
 
 package # hide the package from PAUSE
     InsideOutClass::Instance;
@@ -6,7 +78,7 @@ package # hide the package from PAUSE
 use strict;
 use warnings;
 
-our $VERSION = '0.06';
+our $VERSION = '0.01';
 
 use Carp         'confess';
 use Scalar::Util 'refaddr';
@@ -30,7 +102,8 @@ sub set_slot_value {
 
 sub initialize_slot {
     my ($self, $instance, $slot_name) = @_;
-    $self->{meta}->add_package_variable('%' . $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;
 }
 
@@ -40,8 +113,6 @@ sub is_slot_initialized {
        return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
 }
 
-## &remove_slot is left as an exercise for the reader :)
-
 1;
 
 __END__
@@ -56,11 +127,9 @@ InsideOutClass - A set of example metaclasses which implement the Inside-Out tec
 
   package Foo;
   
-  use metaclass 'Class::MOP::Class' => (
-     # tell our metaclass to use the 
-     # InsideOut attribute metclass 
-     # to construct all it's attributes
-    ':instance_metaclass' => 'InsideOutClass::Instance'
+  use metaclass (
+    ':attribute_metaclass' => 'InsideOutClass::Attribute',
+    ':instance_metaclass'  => 'InsideOutClass::Instance'
   );
   
   __PACKAGE__->meta->add_attribute('foo' => (
@@ -97,9 +166,7 @@ an exercise to the reader.
 
 Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
-=head1 SEE ALSO
-
-L<Tie::RefHash::Weak>
+Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE