From: Aran Deltac Date: Tue, 4 Apr 2006 15:21:45 +0000 (+0000) Subject: A bunch of tweaks inspired by comments from Dave Howorth. X-Git-Tag: 0.03001~15 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9717d3ca4e452a4a6d092856565131ca7b5cc1f5;p=dbsrgits%2FDBIx-Class-Tree.git A bunch of tweaks inspired by comments from Dave Howorth. --- diff --git a/Changes b/Changes index 934f4aa..e2d3a71 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,10 @@ Revision history for DBIx::Class::Tree 0.01000 + - Removed the _grouping_clause override method. + - Created a TODO document. + - attach_child() attach_sibling() now accept more than + one object to attach. - Added tests for Ordered. - Renamed Positional as Ordered. - Added tests for AdjacencyList. diff --git a/TODO b/TODO new file mode 100644 index 0000000..d377ab3 --- /dev/null +++ b/TODO @@ -0,0 +1,8 @@ + + - Support DAGs. + - Add is_leaf, is_parent, is_branch, and is_root. + - Tree::Visitor + - Come up with a better name for attach_before and attach_after. + - Support multiple columns for ordering. + - Declare both the parent column and the position column in one call. + diff --git a/lib/DBIx/Class/Tree/AdjacencyList.pm b/lib/DBIx/Class/Tree/AdjacencyList.pm index 99f32ad..b3df744 100644 --- a/lib/DBIx/Class/Tree/AdjacencyList.pm +++ b/lib/DBIx/Class/Tree/AdjacencyList.pm @@ -49,8 +49,9 @@ adjacency list model is a very common way of representing a tree structure. In this model each row in a table has a prent ID column that references the primary key of another row in the same table. Because of this the primary key must only be one column and is usually some sort of integer. The row -with a parent ID of 0 is the root row and is usually the parent of all -other rows. +with a parent ID of 0 is the root node and is usually the parent of all +other rows. Although, there is no limitation in this module that would +stop you from having multiple root nodes. =head1 METHODS @@ -87,7 +88,12 @@ sub parent_column { $employee->parent( $parent_id ); Retrieves the object's parent object, or changes the object's -parent to the specified parent or parent ID. +parent to the specified parent or parent ID. If you would like +to make the object the root node, just set the parent to 0. + +If you are setting the parent then 0 will be returned if the +specified parent is already the object's parent and 1 on +success. =cut @@ -120,14 +126,21 @@ has_many relationship called children. =head2 attach_child $parent->attach_child( $child ); + $parent->attach_child( $child, $child, ... ); -Sets the child to the new parent. +Sets the child, or children, to the new parent. Returns 1 +on success and returns 0 if the parent object already has +the child. =cut sub attach_child { - my( $self, $child ) = @_; - return $child->parent( $self ); + my $self = shift; + my $return = 1; + foreach my $child (@_) { + $child->parent( $self ); + } + return $return; } =head2 siblings @@ -158,16 +171,22 @@ sub siblings { =head2 attach_sibling - $this->attach_sibling( $that ); + $obj->attach_sibling( $sibling ); + $obj->attach_sibling( $sibling, $sibling, ... ); -Sets the passed in object to have the same parent -as the calling object. +Sets the passed in object(s) to have the same parent +as the calling object. Returns 1 on success and +0 if the sibling already has the same parent. =cut sub attach_sibling { - my( $self, $node ) = @_; - return $node->parent( $self->parent() ); + my $self = shift; + my $return = 1; + foreach my $node (@_) { + $return = 0 if (!$node->parent( $self->parent() )); + } + return $return; } 1; diff --git a/lib/DBIx/Class/Tree/AdjacencyList/Ordered.pm b/lib/DBIx/Class/Tree/AdjacencyList/Ordered.pm index 2085f29..57ea891 100644 --- a/lib/DBIx/Class/Tree/AdjacencyList/Ordered.pm +++ b/lib/DBIx/Class/Tree/AdjacencyList/Ordered.pm @@ -6,8 +6,8 @@ use base qw( DBIx::Class ); use Carp qw( croak ); __PACKAGE__->load_components(qw( - Tree::AdjacencyList Ordered + Tree::AdjacencyList )); =head1 NAME @@ -26,7 +26,7 @@ Create a table for your tree data. ); In your Schema or DB class add Tree::AdjacencyList::Ordered -to the top of the component list. +to the front of the component list. __PACKAGE__->load_components(qw( Tree::AdjacencyList::Ordered ... )); @@ -73,16 +73,14 @@ position column. sub parent_column { my $class = shift; + my $position_col = $class->position_column() || croak('You must call position_column() before calling parent_column()'); if (@_) { - my $parent_col = shift; - my $primary_col = ($class->primary_columns())[0]; - my $position_col = $class->position_column() || croak('You must call position_column() before calling parent_column()'); - $class->belongs_to( '_parent' => $class => { "foreign.$primary_col" => "self.$parent_col" } ); - $class->has_many( 'children' => $class => { "foreign.$parent_col" => "self.$primary_col" }, { order_by=>$position_col } ); - $class->_parent_column( $parent_col ); + $class->grouping_column( @_ ); + $class->next::method( @_ ); + $class->relationship_info('children')->{attrs}->{order_by} = $position_col; return 1; } - return $class->_parent_column(); + return $class->grouping_column; } =head2 parent @@ -190,41 +188,30 @@ sub attach_after { $sibling->move_to( $self->get_column($self->position_column()) + 1 ); } -=head1 PRIVATE METHODS - -These methods are used internally. You should never have the -need to use them. - -=head2 grouping_column - -Postional's grouping_column method does not, and should not, be -defined when using this module. This method just throws out an -error if you try to use it. - -=cut - -sub grouping_column { - croak('Use parent_column() instead of grouping_column()'); -} +1; +__END__ -=head2 _grouping_clause +=head1 INHERITED METHODS -This method is provided as an override of the method in -L. This method is what provides the -glue between AdjacencyList and Ordered. +This module inherits all methods from L. -=cut + siblings + first_sibling + last_sibling + previous_sibling + next_sibling + move_previous + move_next + move_first + move_last + move_to + insert + delete -sub _grouping_clause { - my( $self ) = @_; - my $col = $self->_parent_column(); - return ( - $col => $self->get_column( $col ) - ); -} +And L. -1; -__END__ + attach_child + attach_sibling =head1 AUTHOR