small tweaks to meta method docs
[gitmo/Class-MOP.git] / lib / Class / MOP / Instance.pm
index 1875668..853081b 100644 (file)
@@ -6,7 +6,8 @@ use warnings;
 
 use Scalar::Util 'weaken', 'blessed';
 
-our $VERSION   = '0.65';
+our $VERSION   = '0.78';
+$VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
 use base 'Class::MOP::Object';
@@ -35,7 +36,17 @@ sub new {
     my $options = $class->BUILDARGS(@_);
 
     # FIXME replace with a proper constructor
-    my $instance = bless {
+    my $instance = $class->_new(%$options);
+
+    # FIXME weak_ref => 1,
+    weaken($instance->{'associated_metaclass'});
+
+    return $instance;
+}
+
+sub _new {
+    my ( $class, %options ) = @_;
+    bless {
         # NOTE:
         # I am not sure that it makes
         # sense to pass in the meta
@@ -46,16 +57,11 @@ sub new {
         # which is *probably* a safe
         # assumption,.. but you can
         # never tell <:)
-        'associated_metaclass' => $options->{associated_metaclass},
-        'attributes'           => $options->{attributes},
-        'slots'                => $options->{slots},
-        'slot_hash'            => $options->{slot_hash},
+        'associated_metaclass' => $options{associated_metaclass},
+        'attributes'           => $options{attributes},
+        'slots'                => $options{slots},
+        'slot_hash'            => $options{slot_hash},
     } => $class;
-
-    # FIXME weak_ref => 1,
-    weaken($instance->{'associated_metaclass'});
-
-    return $instance;
 }
 
 sub _class_name { $_[0]->{_class_name} ||= $_[0]->associated_metaclass->name }
@@ -64,9 +70,10 @@ sub associated_metaclass { $_[0]{'associated_metaclass'} }
 
 sub create_instance {
     my $self = shift;
-    $self->bless_instance_structure({});
+    bless {}, $self->_class_name;
 }
 
+# for compatibility
 sub bless_instance_structure {
     my ($self, $instance_structure) = @_;
     bless $instance_structure, $self->_class_name;
@@ -74,7 +81,7 @@ sub bless_instance_structure {
 
 sub clone_instance {
     my ($self, $instance) = @_;
-    $self->bless_instance_structure({ %$instance });
+    bless { %$instance }, $self->_class_name;
 }
 
 # operations on meta instance
@@ -84,6 +91,11 @@ sub get_all_slots {
     return @{$self->{'slots'}};
 }
 
+sub get_all_attributes {
+    my $self = shift;
+    return @{$self->{attributes}};
+}
+
 sub is_valid_slot {
     my ($self, $slot_name) = @_;
     exists $self->{'slot_hash'}->{$slot_name};
@@ -142,7 +154,9 @@ sub strengthen_slot_value {
 
 sub rebless_instance_structure {
     my ($self, $instance, $metaclass) = @_;
-    bless $instance, $metaclass->name;
+
+    # we use $_[1] here because of t/306_rebless_overload.t regressions on 5.8.8
+    bless $_[1], $metaclass->name;
 }
 
 sub is_dependent_on_superclasses {
@@ -160,7 +174,7 @@ sub inline_create_instance {
 
 sub inline_slot_access {
     my ($self, $instance, $slot_name) = @_;
-    sprintf "%s->{%s}", $instance, $slot_name;
+    sprintf q[%s->{"%s"}], $instance, quotemeta($slot_name);
 }
 
 sub inline_get_slot_value {
@@ -250,16 +264,19 @@ Returns the metaclass of L<Class::MOP::Instance>.
 
 =item B<create_instance>
 
-This creates the appropriate structure needed for the instance and
-then calls C<bless_instance_structure> to bless it into the class.
+This creates the appropriate structure needed for the instance and blesses it.
 
 =item B<bless_instance_structure ($instance_structure)>
 
 This does just exactly what it says it does.
 
+This method has been deprecated but remains for compatibility reasons. None of
+the subclasses of L<Class::MOP::Instance> ever bothered to actually make use of
+it, so it was deemed unnecessary fluff.
+
 =item B<clone_instance ($instance_structure)>
 
-This too does just exactly what it says it does.
+Creates a shallow clone of $instance_structure.
 
 =back
 
@@ -290,6 +307,12 @@ superclass changes.
 
 Defaults to false.
 
+=item B<get_all_attributes>
+
+This will return the current list of attributes (as
+Class::MOP::Attribute objects) based on what was given to this object
+in C<new>.
+
 =back
 
 =head2 Operations on Instance Structures
@@ -365,7 +388,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2008 by Infinity Interactive, Inc.
+Copyright 2006-2009 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>