From: Stevan Little Date: Thu, 9 Feb 2006 04:27:29 +0000 (+0000) Subject: refactoring the BinaryTree X-Git-Tag: 0_06~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=de64f9ba38d99232ec6bebea12237d4b52c5f1ef;p=gitmo%2FClass-MOP.git refactoring the BinaryTree --- diff --git a/t/lib/BinaryTree.pm b/t/lib/BinaryTree.pm index 7ba63d1..4fb45bd 100644 --- a/t/lib/BinaryTree.pm +++ b/t/lib/BinaryTree.pm @@ -4,70 +4,60 @@ package BinaryTree; use strict; use warnings; -use Class::MOP 'meta'; - -our $VERSION = '0.01'; - -__PACKAGE__->meta->add_attribute( - Class::MOP::Attribute->new('$:uid' => ( - reader => 'getUID', - writer => 'setUID', - default => sub { - my $instance = shift; - ("$instance" =~ /\((.*?)\)$/); +use metaclass; + +our $VERSION = '0.02'; + +BinaryTree->meta->add_attribute('$:uid' => ( + reader => 'getUID', + writer => 'setUID', + default => sub { + my $instance = shift; + ("$instance" =~ /\((.*?)\)$/); + } +)); + +BinaryTree->meta->add_attribute('$:node' => ( + reader => 'getNodeValue', + writer => 'setNodeValue', + init_arg => ':node' +)); + +BinaryTree->meta->add_attribute('$:parent' => ( + predicate => 'hasParent', + reader => 'getParent', + writer => 'setParent' +)); + +BinaryTree->meta->add_attribute('$:left' => ( + predicate => 'hasLeft', + reader => 'getLeft', + writer => { + 'setLeft' => sub { + my ($self, $tree) = @_; + $tree->setParent($self) if defined $tree; + $self->{'$:left'} = $tree; + $self; } - )) -); - -__PACKAGE__->meta->add_attribute( - Class::MOP::Attribute->new('$:node' => ( - reader => 'getNodeValue', - writer => 'setNodeValue', - init_arg => ':node' - )) -); - -__PACKAGE__->meta->add_attribute( - Class::MOP::Attribute->new('$:parent' => ( - predicate => 'hasParent', - reader => 'getParent', - writer => 'setParent' - )) -); - -__PACKAGE__->meta->add_attribute( - Class::MOP::Attribute->new('$:left' => ( - predicate => 'hasLeft', - reader => 'getLeft', - writer => { - 'setLeft' => sub { - my ($self, $tree) = @_; - $tree->setParent($self) if defined $tree; - $self->{'$:left'} = $tree; - $self; - } - }, - )) -); - -__PACKAGE__->meta->add_attribute( - Class::MOP::Attribute->new('$:right' => ( - predicate => 'hasRight', - reader => 'getRight', - writer => { - 'setRight' => sub { - my ($self, $tree) = @_; - $tree->setParent($self) if defined $tree; - $self->{'$:right'} = $tree; - $self; - } + }, +)); + +BinaryTree->meta->add_attribute('$:right' => ( + predicate => 'hasRight', + reader => 'getRight', + writer => { + 'setRight' => sub { + my ($self, $tree) = @_; + $tree->setParent($self) if defined $tree; + $self->{'$:right'} = $tree; + $self; } - )) -); + } +)); sub new { my $class = shift; - bless $class->meta->construct_instance(':node' => shift) => $class; + $class->meta->new_object(':node' => shift); } sub removeLeft { @@ -131,3 +121,4 @@ sub height { return 1 + (($left_height > $right_height) ? $left_height : $right_height); } +1; \ No newline at end of file