From: Stevan Little Date: Sun, 5 Feb 2006 16:37:44 +0000 (+0000) Subject: minor changes to class::mop:::class X-Git-Tag: 0_06~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5f3c057adabc76dca64d77e3a62484138f790930;p=gitmo%2FClass-MOP.git minor changes to class::mop:::class --- diff --git a/Changes b/Changes index 6a9afbb..b665511 100644 --- a/Changes +++ b/Changes @@ -13,7 +13,13 @@ Revision history for Perl extension Class-MOP. since all the same info can be gotten from the attribute meta-object itself - updated docs & tests to reflect - + - added &clone_instance method which does a deep clone + of the instance structure created by &construct_instance + - added docs & tests for this + - added &new_object and &clone_object convience methods to + return blessed new or cloned instances + - added docs & tests for this + * examples/ - adjusting code to use the &Class::MOP::Class::meta fix detailed above diff --git a/TODO b/TODO index c827047..86671c7 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,7 @@ TODO - clean up all ->initialize($_[0]) handling -(DONE) +(PARTIALLY DONE) - needs tests We should always be sure that $_[0] is a package name, and not a blessed intstance. @@ -17,7 +17,7 @@ All the info in the HASH is discoverable through the meta-object. - General Purpose &clone_instance method -(PARTIALLY DONE) - need to implement the deep cloning +(PARTIALLY DONE) - need to implement the deep cloning & tests It can be a method of the metaclass, like construct_instance is, actually it should be called clone_instance, and it should @@ -27,15 +27,17 @@ class to implement, as is the construct_instance. - General Purpose &new_object and &clone_object method +(PARTIALLY DONE) - needs tests + I seem to be writing a new method each time, but since we dont have a Object class to always inherit from, this is needed. However, there is nothing to say that I cannot do something like: - Foo->meta->new_object() + Foo->meta->new_object(%params) and ... - $foo->meta->clone_object() + $foo->meta->clone_object($foo, %params) Give it some more thought, but I think it is the best way to approach this. diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 8dc94d6..f98b44b 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -55,6 +55,10 @@ sub meta { Class::MOP::Class->initialize($_[0]) } } => $class; } else { + # NOTE: + # it is safe to use meta here because + # class will always be a subclass of + # Class::MOP::Class, which defines meta bless $class->meta->construct_instance(':package' => $package_name, @_) => $class } } @@ -92,6 +96,10 @@ sub create { # Instance Construction & Cloning +sub new_object { + my $class = shift; + bless $class->construct_instance(@_) => $class->name; +} sub construct_instance { my ($class, %params) = @_; @@ -109,6 +117,12 @@ sub construct_instance { return $instance; } +sub clone_object { + my $class = shift; + my $instance = shift; + bless $class->clone_instance($instance, @_) => $class->name; +} + sub clone_instance { my ($class, $self, %params) = @_; (blessed($self)) @@ -526,6 +540,20 @@ to use them or not. =over 4 +=item B + +This is a convience method for creating a new object of the class, and +blessing it into the appropriate package as well. Ideally your class +would call a C this method like so: + + sub MyClass::new { + my ($class, %param) = @_; + $class->meta->new_object(%params); + } + +Of course the ideal place for this would actually be in C +but that is considered bad style, so we do not do that. + =item B This method is used to construct an instace structure suitable for @@ -539,6 +567,20 @@ it will then initialize them using either use the corresponding key in C<%params> or any default value or initializer found in the attribute meta-object. +=item B + +This is a convience method for cloning an object instance, then +blessing it into the appropriate package. Ideally your class +would call a C this method like so: + + sub MyClass::clone { + my ($self, %param) = @_; + $self->meta->clone_object($self, %params); + } + +Of course the ideal place for this would actually be in C +but that is considered bad style, so we do not do that. + =item B This method is a compliment of C (which means if