From: Aran Deltac Date: Sun, 16 Apr 2006 10:34:24 +0000 (+0000) Subject: is_* methods and primary key validation X-Git-Tag: 0.03001~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=74d97bdc74f7285386ea957e42188a9eba235f52;p=dbsrgits%2FDBIx-Class-Tree.git is_* methods and primary key validation --- diff --git a/Changes b/Changes index e2d3a71..e14e816 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,8 @@ Revision history for DBIx::Class::Tree 0.01000 + - Added is_leaf, is_root, and is_branch to AdjacencyList. + - Added a validation override for set_primary_key(). - Removed the _grouping_clause override method. - Created a TODO document. - attach_child() attach_sibling() now accept more than diff --git a/TODO b/TODO index 0029a96..1e666a1 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ - 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. diff --git a/lib/DBIx/Class/Tree.pm b/lib/DBIx/Class/Tree.pm index 86bc7ce..7ce2535 100644 --- a/lib/DBIx/Class/Tree.pm +++ b/lib/DBIx/Class/Tree.pm @@ -23,9 +23,26 @@ trees of data with DBIx::Class. =head1 COMPONENTS -L - Manage a tree of data using the common adjacency list model. (EXPERIMENTAL) - -L - Glue DBIx::Class::Ordered and DBIx::Class::Tree::AdjacencyList together. (EXPERIMENTAL) +L - Manage a tree of data using the +common adjacency list model. (EXPERIMENTAL) + +L - Glue DBIx::Class::Ordered +and DBIx::Class::Tree::AdjacencyList together. (EXPERIMENTAL) + +=head1 DAG + +All tree related modules must conform to have and use the basic traversal +methods of a DAG. For the most part this just means that Tree modules +must provide the appearance of having multiple parents per node (via a +parents() method) but may very well never return more than one parent. +All utility modules, such as a Visitor module, should do its best to +never assume that a node only has one parent. There are situations +where this is not possible - in those cases the module's documentation +should clearly state that it is not compatible with DAGs. + +So far there is no Tree::DAG module, but there will be. These requirements +are vague, and the requirements of Tree modules to be DAG compatible will +become more defined in due course. =head1 AUTHOR diff --git a/lib/DBIx/Class/Tree/AdjacencyList.pm b/lib/DBIx/Class/Tree/AdjacencyList.pm index 4c9a758..58c0961 100644 --- a/lib/DBIx/Class/Tree/AdjacencyList.pm +++ b/lib/DBIx/Class/Tree/AdjacencyList.pm @@ -207,6 +207,67 @@ sub attach_sibling { return $return; } +=head2 is_leaf + + if ($obj->is_leaf()) { ... } + +Returns 1 if the object has no children, and 0 otherwise. + +=cut + +sub is_leaf { + my( $self ) = @_; + return $self->result_source->resultset->search( + { $self->_parent_column => $self->id() }, + { limit => 1 } + )->count(); +} + +=head2 is_root + + if ($obj->is_root()) { ... } + +Returns 1 if the object has no parent, and 0 otherwise. + +=cut + +sub is_root { + my( $self ) = @_; + return ( $self->get_column( $self->_parent_column ) ? 1 : 0 ); +} + +=head2 is_branch + + if ($obj->is_branch()) { ... } + +Returns 1 if the object has a parent and has children. +Returns 0 otherwise. + +=cut + +sub is_branch { + my( $self ) = @_; + return ( ($self->is_leaf() or $self->is_root()) ? 0 : 1 ); +} + +=head2 set_primary_key + +This method is an override of DBIx::Class' method for setting the +class' primary key column(s). This method passes control right on +to the normal method after first validating that only one column is +being selected as a primary key. If more than one column is then +an error will be thrown. + +=cut + +sub set_primary_ley { + my $self = shift; + if (@_>1) { + croak('You may only specify a single column as the primary key for adjacency tree classes'); + } + return $self->next::method( @_ ); +} + 1; __END__