Module: Taxonomite::Tree

Extended by:
ActiveSupport::Concern
Defined in:
lib/taxonomite/tree.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#ancestor_of?(nd) ⇒ Boolean

is this an ancestor of another node?

Returns:

  • (Boolean)


63
64
65
# File 'lib/taxonomite/tree.rb', line 63

def ancestor_of?(nd)
  nd.is_ancestor?(self)
end

#ancestorsObject

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?

Returns:

  • (Boolean)


69
70
71
# File 'lib/taxonomite/tree.rb', line 69

def descendant_of?(nd)
  (self == nd) ? true : is_ancestor?(nd)
end

#descendantsObject

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_childrenObject

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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


39
40
41
# File 'lib/taxonomite/tree.rb', line 39

def is_root?
  self.parent == nil
end

#leavesArray

get all of the leaves from this node downward

Returns:

  • (Array)

    an array of all of the leaves



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_parentObject

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_childrenObject

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

#rootArray

get the root above this node

Returns:

  • (Array)

    an array of all of the roots



51
52
53
# File 'lib/taxonomite/tree.rb', line 51

def root
  self.is_root? ? self : self.parent.root
end

#self_and_ancestorsObject

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_descendantsObject

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