Added INHERITED METHODS sections
[dbsrgits/DBIx-Class-Tree.git] / lib / DBIx / Class / Tree / AdjacencyList.pm
index 93ccb16..457602c 100644 (file)
@@ -15,7 +15,7 @@ Create a table for your tree data.
 
   CREATE TABLE employees (
     employee_id INTEGER PRIMARY KEY AUTOINCREMENT,
-    parent_id INTEGER NOT NULL,
+    parent_id INTEGER NOT NULL DEFAULT 0,
     name TEXT NOT NULL
   );
 
@@ -23,18 +23,15 @@ In your Schema or DB class add Tree::AdjacencyList to the top
 of the component list.
 
   __PACKAGE__->load_components(qw( Tree::AdjacencyList ... ));
-  # If you want positionable data make sure this 
-  # module comes first, as in:
-  __PACKAGE__->load_components(qw( Tree::AdjacencyList Positional ... ));
 
-Specify the column that contains the parent ID each row.
+Specify the column that contains the parent ID of each row.
 
   package My::Employee;
   __PACKAGE__->parent_column('parent_id');
 
 Thats it, now you can modify and analyze the tree.
 
-  #!/use/bin/perl
+  #!/usr/bin/perl
   use My::Employee;
   
   my $employee = My::Employee->create({ name=>'Matt S. Trout' });
@@ -52,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
 
@@ -62,14 +60,26 @@ other rows.
   __PACKAGE__->parent_column('parent_id');
 
 Declares the name of the column that contains the self-referential 
-ID which defines the parent row.  Defaults to "parent_id".
-
-If you are useing the L<DBIx::Class::Positional> component then this 
-parent_column will automatically be used as the collection_column.
+ID which defines the parent row.  Defaults to "parent_id".  This 
+will create a has_many (children) and belongs_to (parent) 
+relationship.
 
 =cut
 
-__PACKAGE__->mk_classdata( 'parent_column' => 'parent_id' );
+__PACKAGE__->mk_classdata( '_parent_column' => 'parent_id' );
+
+sub parent_column {
+    my $class = shift;
+    if (@_) {
+        my $parent_col = shift;
+        my $primary_col = ($class->primary_columns())[0];
+        $class->belongs_to( '_parent' => $class => { "foreign.$primary_col" => "self.$parent_col" } );
+        $class->has_many( 'children' => $class => { "foreign.$parent_col" => "self.$primary_col" } );
+        $class->_parent_column( $parent_col );
+        return 1;
+    }
+    return $class->_parent_column();
+}
 
 =head2 parent
 
@@ -77,40 +87,30 @@ __PACKAGE__->mk_classdata( 'parent_column' => 'parent_id' );
   $employee->parent( $parent_obj );
   $employee->parent( $parent_id );
 
-Retrieves the object's parent ID, or sets the object's 
-parent ID.  If setting the parent ID then 0 will be returned 
-if the object already has the specified parent, and 1 on 
-success.
+Retrieves the object's parent object, or changes the object's 
+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 using the L<DBIx::Class::Positional> component this 
-module will first move the object to the last position of 
-the list, change the parent ID, then move the object to the 
-last position of the new list.  This ensures the intergrity 
-of the positions.
+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
 
 sub parent {
-    my( $self, $new_parent ) = @_;
-    my $parent_column = $self->parent_column();
-    if ($new_parent) {
+    my $self = shift;
+    if (@_) {
+        my $new_parent = shift;
+        my $parent_col = $self->_parent_column();
         if (ref($new_parent)) {
-            $new_parent = $new_parent->id() || 0;
-        }
-        return 0 if ($new_parent == ($self->get_column($parent_column)||0));
-        $self->move_last() if ($self->positional());
-        $self->set_column( $parent_column => $new_parent );
-        if ($self->positional()) {
-            $self->set_column(
-                $self->position_column() => $self->search( {$self->_collection_clause()} )->count() + 1
-            );
+            $new_parent = $new_parent->id() || croak('Parent object does not have an ID');;
         }
+        return 0 if ($new_parent == ($self->get_column($parent_col)||0));
+        $self->set_column( $parent_col => $new_parent );
         $self->update();
         return 1;
     }
-    else {
-        return $self->find( $self->get_column( $parent_column ) );
-    }
+    return $self->_parent();
 }
 
 =head2 children
@@ -119,158 +119,122 @@ sub parent {
   my @children = $employee->children();
 
 Returns a list or record set, depending on context, of all 
-the objects one level below the current one.
-
-If you are using the L<DBIx::Class::Positional> component then this method 
-will return the children sorted by the position column.
-
-=cut
-
-sub children {
-    my( $self ) = @_;
-    my $rs = $self->search(
-        { $self->parent_column()=>$self->id() },
-        ( $self->isa('DBIx::Class::Position') ? {order_by=>$self->position_column()} : () )
-    );
-    return $rs->all() if (wantarray());
-    return $rs;
-}
+the objects one level below the current one.  This method 
+is created when parent_column() is called, which sets up a 
+has_many relationship called children.
 
 =head2 attach_child
 
   $parent->attach_child( $child );
+  $parent->attach_child( $child, $child, ... );
 
-Sets (or moves) 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 ) = @_;
-    $child->parent( $self );
+    my $self = shift;
+    my $return = 1;
+    foreach my $child (@_) {
+        $child->parent( $self );
+    }
+    return $return;
 }
 
-=head2 attach_sibling
+=head2 siblings
 
-  $this->attach_sibling( $that );
+  my $rs = $node->siblings();
+  my @siblings = $node->siblings();
 
-Sets the passed in object to have the same parent 
-as the calling object.
+Returns either a result set or an array of all other objects 
+with the same parent as the calling object.
 
 =cut
 
-sub attach_sibling {
-    my( $self, $child ) = @_;
-    $child->parent( $self->parent() );
+sub siblings {
+    my( $self ) = @_;
+    my $parent_col = $self->_parent_column;
+    my $primary_col = ($self->primary_columns())[0];
+    my $rs = $self->result_source->resultset->search(
+        {
+            $parent_col => $self->get_column($parent_col),
+            $primary_col => { '!=' => $self->get_column($primary_col) },
+        },
+    );
+    return $rs->all() if (wantarray());
+    return $rs;
 }
 
-=head1 POSITIONAL METHODS
-
-If you are useing the L<DBIx::Class::Postional> component 
-in conjunction with this module then you will also have 
-these methods available to you.
-
-=head2 append_child
-
-  $parent->append_child( $child );
-
-Sets the child to have the specified parent and moves the 
-child to the last position.
-
 =cut
 
-sub append_child {
-    my( $self, $child ) = @_;
-    croak('This method may only be used with the Positional component') if (!$self->positional());
-    $child->parent( $self );
-}
-
-=head2 prepend_child
+=head2 attach_sibling
 
-  $parent->prepend_child( $child );
+  $obj->attach_sibling( $sibling );
+  $obj->attach_sibling( $sibling, $sibling, ... );
 
-Sets the child to have the specified parent and moves the 
-child to the first position.
+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 prepend_child {
-    my( $self, $child ) = @_;
-    croak('This method may only be used with the Positional component') if (!$self->positional());
-    $child->parent( $self );
-    $child->move_first();
+sub attach_sibling {
+    my $self = shift;
+    my $return = 1;
+    foreach my $node (@_) {
+        $return = 0 if (!$node->parent( $self->parent() ));
+    }
+    return $return;
 }
 
-=head2 attach_before
+1;
+__END__
 
-  $this->attach_before( $that );
+=head1 INHERITED METHODS
 
-Attaches the object at the position just before the 
-calling object's position.
+=head2 DBIx::Class
 
-=cut
+=over 4
 
-sub attach_before {
-    my( $self, $sibling ) = @_;
-    croak('This method may only be used with the Positional component') if (!$self->positional());
-    $sibling->parent( $self->parent() );
-    $sibling->move_to( $self->get_column($self->position_column()) );
-}
+=item *
 
-=head2 attach_after
+L<mk_classdata|DBIx::Class/mk_classdata>
 
-  $this->attach_after( $that );
+=item *
 
-Attaches the object at the position just after the 
-calling object's position.
+L<component_base_class|DBIx::Class/component_base_class>
 
-=cut
+=back
 
-sub attach_after {
-    my( $self, $sibling ) = @_;
-    croak('This method may only be used with the Positional component') if (!$self->positional());
-    $sibling->parent( $self->parent() );
-    $sibling->move_to( $self->get_column($self->position_column()) + 1 );
-}
+=head2 DBIx::Class::Componentised
 
-=head2 positional
+=over 4
 
-  if ($object->positional()) { ... }
+=item *
 
-Returns true if the object is a DBIx::Class::Positional 
-object.
+L<inject_base|DBIx::Class::Componentised/inject_base>
 
-=cut
+=item *
 
-sub positional {
-    my( $self ) = @_;
-    return $self->isa('DBIx::Class::Positional');
-}
+L<load_components|DBIx::Class::Componentised/load_components>
 
-=head1 PRIVATE METHODS
+=item *
 
-These methods are used internally.  You should never have the 
-need to use them.
+L<load_own_components|DBIx::Class::Componentised/load_own_components>
 
-=head2 _collection_clause
+=back
 
-This method is provided as an override of the method in 
-L<DBIx::Class::Positional>.  This way Positional and Tree::AdjacencyList 
-may be used together without conflict.  Make sure that in 
-your component list that you load Tree::AdjacencyList before you 
-load Positional.
+=head2 Class::Data::Accessor
 
-=cut
+=over 4
 
-sub _collection_clause {
-    my( $self ) = @_;
-    return (
-        $self->parent_column() =>
-        $self->get_column($self->parent_column())
-    );
-}
+=item *
 
-1;
-__END__
+L<mk_classaccessor|Class::Data::Accessor/mk_classaccessor>
+
+=back
 
 =head1 AUTHOR