Module: Taxonomite::Tree
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/taxonomite/tree.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#ancestor_of?(nd) ⇒ Boolean
is this an ancestor of another node?.
-
#ancestors ⇒ Object
return all ancestors of this node.
-
#descendant_of?(nd) ⇒ Boolean
is this a descendant of nd?.
-
#descendants ⇒ Object
return a chainable Mongoid criteria to get all descendants.
-
#destroy_children ⇒ Object
delete children; this *removes all of the children fromt he data base (and ensuing).
-
#is_ancestor?(nd) ⇒ Boolean
is the object an ancestor of nd?.
-
#is_leaf? ⇒ Boolean
is this a leaf?.
-
#is_root? ⇒ Boolean
is this a root node?.
-
#leaves ⇒ Array
get all of the leaves from this node downward.
-
#move_children_to_parent ⇒ Object
move all children to the parent node !!! need to perform validations here?.
-
#nullify_children ⇒ Object
nullifies all children’s parent id (cuts link).
-
#root ⇒ Array
get the root above this node.
-
#self_and_ancestors ⇒ Object
return self + all ancestors of this node.
-
#self_and_descendants ⇒ Object
return a chainable Mongoid criteria to get self + all descendants.
-
#validate_child!(ch) ⇒ Object
perform validation on whether this child is an acceptable child or not? the base_class must have a method ‘validate_child?’ to implement domain logic there.
Instance Method Details
#ancestor_of?(nd) ⇒ Boolean
is this an ancestor of another node?
63 64 65 |
# File 'lib/taxonomite/tree.rb', line 63 def ancestor_of?(nd) nd.is_ancestor?(self) end |
#ancestors ⇒ Object
return all ancestors of this node
75 76 77 78 79 |
# File 'lib/taxonomite/tree.rb', line 75 def ancestors a = Array.new self.parent._ancestors(a) unless self.parent.nil? return a end |
#descendant_of?(nd) ⇒ Boolean
is this a descendant of nd?
69 70 71 |
# File 'lib/taxonomite/tree.rb', line 69 def descendant_of?(nd) (self == nd) ? true : is_ancestor?(nd) end |
#descendants ⇒ Object
return a chainable Mongoid criteria to get all descendants
89 90 91 |
# File 'lib/taxonomite/tree.rb', line 89 def descendants self.children.collect { |c| [c] + c.descendants }.flatten end |
#destroy_children ⇒ Object
delete children; this *removes all of the children fromt he data base (and ensuing)
116 117 118 |
# File 'lib/taxonomite/tree.rb', line 116 def destroy_children children.destroy_all end |
#is_ancestor?(nd) ⇒ Boolean
is the object an ancestor of nd?
57 58 59 |
# File 'lib/taxonomite/tree.rb', line 57 def is_ancestor?(nd) self.is_root? ? false : (self.parent == nd || self.parent.is_ancestor?(nd)) end |
#is_leaf? ⇒ Boolean
is this a leaf?
45 46 47 |
# File 'lib/taxonomite/tree.rb', line 45 def is_leaf? self.children.empty? end |
#is_root? ⇒ Boolean
is this a root node?
39 40 41 |
# File 'lib/taxonomite/tree.rb', line 39 def is_root? self.parent == nil end |
#leaves ⇒ Array
get all of the leaves from this node downward
144 145 146 147 |
# File 'lib/taxonomite/tree.rb', line 144 def leaves return [self] if self.is_leaf? self.children.collect { |c| c.leaves }.flatten end |
#move_children_to_parent ⇒ Object
move all children to the parent node !!! need to perform validations here?
123 124 125 126 127 128 |
# File 'lib/taxonomite/tree.rb', line 123 def move_children_to_parent children.each do |c| self.parent.children << c c.parent = self.parent # is this necessary? end end |
#nullify_children ⇒ Object
nullifies all children’s parent id (cuts link)
107 108 109 110 111 112 |
# File 'lib/taxonomite/tree.rb', line 107 def nullify_children children.each do |c| c.parent = nil c.save end end |
#root ⇒ Array
get the root above this node
51 52 53 |
# File 'lib/taxonomite/tree.rb', line 51 def root self.is_root? ? self : self.parent.root end |
#self_and_ancestors ⇒ Object
return self + all ancestors of this node
83 84 85 |
# File 'lib/taxonomite/tree.rb', line 83 def self_and_ancestors return [self] + self.ancestors end |
#self_and_descendants ⇒ Object
return a chainable Mongoid criteria to get self + all descendants
101 102 103 |
# File 'lib/taxonomite/tree.rb', line 101 def self_and_descendants [self] + self.descendants end |
#validate_child!(ch) ⇒ Object
perform validation on whether this child is an acceptable child or not? the base_class must have a method ‘validate_child?’ to implement domain logic there
133 134 135 136 137 138 139 |
# File 'lib/taxonomite/tree.rb', line 133 def validate_child!(ch) raise InvalidChild.create(self, ch) if (ch == nil) raise CircularRelation.create(self, ch) if self.descendant_of?(ch) if base_class.method_defined? :validate_child self.validate_child(ch) # this should throw an error if not valid end end |