X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FTree%2FAdjacencyList.pm;fp=lib%2FDBIx%2FClass%2FTree%2FAdjacencyList.pm;h=58c09611c5185f84ca6db172ad2c8a66646abe80;hb=74d97bdc74f7285386ea957e42188a9eba235f52;hp=4c9a7586127f317d223005db06708e50fde0e0b2;hpb=bb17efa0869c51aea205960012b06c956512a45a;p=dbsrgits%2FDBIx-Class-Tree.git 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__