fixed all the attribute name to be more Perl6ish and then removed the : in the init_a...
[gitmo/Class-MOP.git] / lib / Class / MOP / Attribute.pm
index 6f13f38..db13b1a 100644 (file)
@@ -9,7 +9,7 @@ use Class::MOP::Method::Accessor;
 use Carp         'confess';
 use Scalar::Util 'blessed', 'reftype', 'weaken';
 
-our $VERSION   = '0.12';
+our $VERSION   = '0.14';
 our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Class::MOP::Object';
@@ -45,17 +45,20 @@ sub new {
             if exists $options{default} && ref $options{default};      
             
     bless {
-        name      => $name,
-        accessor  => $options{accessor},
-        reader    => $options{reader},
-        writer    => $options{writer},
-        predicate => $options{predicate},
-        clearer   => $options{clearer},
-        init_arg  => $options{init_arg},
-        default   => $options{default},
+        '$!name'      => $name,
+        '$!accessor'  => $options{accessor},
+        '$!reader'    => $options{reader},
+        '$!writer'    => $options{writer},
+        '$!predicate' => $options{predicate},
+        '$!clearer'   => $options{clearer},
+        '$!init_arg'  => $options{init_arg},
+        '$!default'   => $options{default},
         # keep a weakened link to the 
         # class we are associated with
-        associated_class => undef,
+        '$!associated_class' => undef,
+        # and a list of the methods 
+        # associated with this attr
+        '@!associated_methods' => [],
     } => $class;
 }
 
@@ -74,13 +77,13 @@ sub clone {
 
 sub initialize_instance_slot {
     my ($self, $meta_instance, $instance, $params) = @_;
-    my $init_arg = $self->{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};
     # if nothing was in the %params, we can use the 
     # attribute's default value (if it has one)
-    if (!defined $val && defined $self->{default}) {
+    if (!defined $val && defined $self->{'$!default'}) {
         $val = $self->default($instance);
     }
     $meta_instance->set_slot_value($instance, $self->name, $val);
@@ -90,30 +93,31 @@ sub initialize_instance_slot {
 # the next bunch of methods will get bootstrapped 
 # away in the Class::MOP bootstrapping section
 
-sub name { $_[0]->{name} }
+sub name { $_[0]->{'$!name'} }
 
-sub associated_class { $_[0]->{associated_class} }
+sub associated_class   { $_[0]->{'$!associated_class'}   }
+sub associated_methods { $_[0]->{'@!associated_methods'} }
 
-sub has_accessor  { defined($_[0]->{accessor})  ? 1 : 0 }
-sub has_reader    { defined($_[0]->{reader})    ? 1 : 0 }
-sub has_writer    { defined($_[0]->{writer})    ? 1 : 0 }
-sub has_predicate { defined($_[0]->{predicate}) ? 1 : 0 }
-sub has_clearer   { defined($_[0]->{clearer})   ? 1 : 0 }
-sub has_init_arg  { defined($_[0]->{init_arg})  ? 1 : 0 }
-sub has_default   { defined($_[0]->{default})   ? 1 : 0 }
+sub has_accessor  { defined($_[0]->{'$!accessor'})  ? 1 : 0 }
+sub has_reader    { defined($_[0]->{'$!reader'})    ? 1 : 0 }
+sub has_writer    { defined($_[0]->{'$!writer'})    ? 1 : 0 }
+sub has_predicate { defined($_[0]->{'$!predicate'}) ? 1 : 0 }
+sub has_clearer   { defined($_[0]->{'$!clearer'})   ? 1 : 0 }
+sub has_init_arg  { defined($_[0]->{'$!init_arg'})  ? 1 : 0 }
+sub has_default   { defined($_[0]->{'$!default'})   ? 1 : 0 }
 
-sub accessor  { $_[0]->{accessor}  } 
-sub reader    { $_[0]->{reader}    }
-sub writer    { $_[0]->{writer}    }
-sub predicate { $_[0]->{predicate} }
-sub clearer   { $_[0]->{clearer}   }
-sub init_arg  { $_[0]->{init_arg}  }
+sub accessor  { $_[0]->{'$!accessor'}  } 
+sub reader    { $_[0]->{'$!reader'}    }
+sub writer    { $_[0]->{'$!writer'}    }
+sub predicate { $_[0]->{'$!predicate'} }
+sub clearer   { $_[0]->{'$!clearer'}   }
+sub init_arg  { $_[0]->{'$!init_arg'}  }
 
 # end bootstrapped away method section.
 # (all methods below here are kept intact)
 
 sub is_default_a_coderef { 
-    ('CODE' eq (reftype($_[0]->{default}) || ''))    
+    ('CODE' eq (reftype($_[0]->{'$!default'} || $_[0]->{default}) || ''))    
 }
 
 sub default { 
@@ -123,9 +127,9 @@ sub default {
         # we pass in the instance and default
         # can return a value based on that 
         # instance. Somewhat crude, but works.
-        return $self->{default}->($instance);
+        return $self->{'$!default'}->($instance);
     }           
-    $self->{default};
+    $self->{'$!default'};
 }
 
 # slots
@@ -138,12 +142,19 @@ sub attach_to_class {
     my ($self, $class) = @_;
     (blessed($class) && $class->isa('Class::MOP::Class'))
         || confess "You must pass a Class::MOP::Class instance (or a subclass)";
-    weaken($self->{associated_class} = $class);    
+    weaken($self->{'$!associated_class'} = $class);    
 }
 
 sub detach_from_class {
     my $self = shift;
-    $self->{associated_class} = undef;        
+    $self->{'$!associated_class'} = undef;        
+}
+
+# method association 
+
+sub associate_method {
+    my ($self, $method) = @_;
+    push @{$self->{'@!associated_methods'}} => $method;
 }
 
 ## Slot management
@@ -151,19 +162,35 @@ sub detach_from_class {
 sub set_value {
     my ($self, $instance, $value) = @_;
 
-    Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
+    Class::MOP::Class->initialize(blessed($instance))
                      ->get_meta_instance
-                     ->set_slot_value( $instance, $self->name, $value );
+                     ->set_slot_value($instance, $self->name, $value);
 }
 
 sub get_value {
     my ($self, $instance) = @_;
 
-    Class::MOP::Class->initialize(Scalar::Util::blessed($instance))
+    Class::MOP::Class->initialize(blessed($instance))
                      ->get_meta_instance
                      ->get_slot_value($instance, $self->name);
 }
 
+sub has_value {
+    my ($self, $instance) = @_;
+    
+    defined Class::MOP::Class->initialize(blessed($instance))
+                             ->get_meta_instance
+                             ->get_slot_value($instance, $self->name) ? 1 : 0;    
+}
+
+sub clear_value {
+    my ($self, $instance) = @_;
+        
+    Class::MOP::Class->initialize(blessed($instance))
+                     ->get_meta_instance
+                     ->deinitialize_slot($instance, $self->name);    
+}
+
 ## load em up ...
 
 sub accessor_metaclass { 'Class::MOP::Method::Accessor' }
@@ -174,7 +201,9 @@ sub process_accessors {
         (reftype($accessor) eq 'HASH')
             || confess "bad accessor/reader/writer/predicate/clearer format, must be a HASH ref";
         my ($name, $method) = %{$accessor};
-        return ($name, $self->accessor_metaclass->wrap($method));        
+        $method = $self->accessor_metaclass->wrap($method);
+        $self->associate_method($method);
+        return ($name, $method);        
     }
     else {
         my $inline_me = ($generate_as_inline_methods && $self->associated_class->instance_metaclass->is_inlinable);         
@@ -182,11 +211,12 @@ sub process_accessors {
         eval {
             $method = $self->accessor_metaclass->new(
                 attribute     => $self,
-                as_inline     => $inline_me,
+                is_inline     => $inline_me,
                 accessor_type => $type,
             );            
         };
         confess "Could not create the '$type' method for " . $self->name . " because : $@" if $@;        
+        $self->associate_method($method);
         return ($accessor, $method);
     }    
 }
@@ -417,16 +447,20 @@ back to their "unfulfilled" state.
 
 =over 4
 
-=item set_value $instance, $value
+=item B<set_value ($instance, $value)>
 
 Set the value without going through the accessor. Note that this may be done to
 even attributes with just read only accessors.
 
-=item get_value $instance
+=item B<get_value ($instance)>
 
 Return the value without going through the accessor. Note that this may be done
 even to attributes with just write only accessors.
 
+=item B<has_value ($instance)>
+
+=item B<clear_value ($instance)>
+
 =back
 
 =head2 Informational
@@ -497,12 +531,6 @@ These are all basic predicate methods for the values passed into C<new>.
 
 =item B<detach_from_class>
 
-=item B<slot_name>
-
-=item B<allocate_slots>
-
-=item B<deallocate_slots>
-
 =back
 
 =head2 Attribute Accessor generation
@@ -511,6 +539,10 @@ These are all basic predicate methods for the values passed into C<new>.
 
 =item B<accessor_metaclass>
 
+=item B<associate_method>
+
+=item B<associated_methods>
+
 =item B<install_accessors>
 
 This allows the attribute to generate and install code for it's own