Module: MongoTree::InstanceMethods

Defined in:
lib/mongo_tree.rb

Constant Summary collapse

@@fieldSep =
'|'
@@recordSep =
"\n"

Instance Method Summary collapse

Instance Method Details

#<<(child) ⇒ Object

Convenience synonym for Tree#add method. This method allows a convenient method to add children hierarchies in the tree. E.g. root << child << grand_child



48
49
50
# File 'lib/mongo_tree.rb', line 48

def <<(child)
    add(child)
end

#<=>(other) ⇒ Object

Provides a comparision operation for the nodes. Comparision is based on the natural character-set ordering for the node names.



213
214
215
216
# File 'lib/mongo_tree.rb', line 213

def <=>(other)
    return +1 if other == nil
    self.name <=> other.name
end

#[](key) ⇒ Object

Returns the requested node from the set of immediate children.

If the key is numeric, then the in-sequence array of children is accessed (see Tree#children). If the key is not numeric, then it is assumed to be the name of the child node to be returned.



167
168
169
170
171
172
173
174
175
# File 'lib/mongo_tree.rb', line 167

def [](key)
    raise "Key needs to be provided" if key == nil
    
    if key.kind_of?(Integer)
      children[key]
    else
      descendants.select {|child, level| child.name == key}.first[0]
    end
end

#_dump(depth) ⇒ Object



225
226
227
228
229
# File 'lib/mongo_tree.rb', line 225

def _dump(depth)
    strRep = String.new
    each {|node| strRep << node.createDumpRep}
    strRep
end

#add(child) ⇒ Object

Adds the specified child node to the receiver node. The child node’s parent is set to be the receiver. The child is added as the last child in the current list of children for the receiver node.



56
57
58
59
60
61
62
63
64
65
# File 'lib/mongo_tree.rb', line 56

def add(child)
    raise "Child already added" if child.parents.include?(self.id)
    child.parents = []
    child.parents << self.parents 
    child.parents << self.id
    child.parents.flatten!
    child.save
    return child

end

#ancestorsObject



149
150
151
# File 'lib/mongo_tree.rb', line 149

def ancestors
  self.class.find(self.parents)
end

#childrenObject

Returns all children of an object



136
137
138
# File 'lib/mongo_tree.rb', line 136

def children
  descendants(0).map {|c| c[0]}
end

#create_dump_repObject

Creates a dump representation



219
220
221
222
223
# File 'lib/mongo_tree.rb', line 219

def create_dump_rep
    strRep = String.new
    strRep << @name << @@fieldSep << (is_root? ? @name : parent.name)
    strRep << @@fieldSep << Marshal.dump(@content) << @@recordSep
end

#descendants(max_level = nil) ⇒ Object

Returns a multi-dimensional array of children objects and the level where level is the depth in the hierarchy.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mongo_tree.rb', line 120

def descendants(max_level=nil)
  children=[]
  self.class.find(:all).each do |obj|
    if obj.parents.include?(self.id)
      level = obj.parents.reverse.index(self.id)            
      if max_level.nil?
        children << [obj, level]
      else
        children << [obj, level] if level <= max_level
      end
    end
  end
  return children
end

#each {|_self| ... } ⇒ Object

Returns every node (including the receiver node) from the tree to the specified block.

Yields:

  • (_self)

Yield Parameters:



155
156
157
158
# File 'lib/mongo_tree.rb', line 155

def each &block
    yield self
    children { |child| child.each(&block) }
end

#has_children?Boolean

Indicates whether this node has any immediate child nodes.

Returns:

  • (Boolean)


114
115
116
# File 'lib/mongo_tree.rb', line 114

def has_children?
    children.length != 0
end

#has_content?Boolean

Indicates whether this node has any associated content.

Returns:

  • (Boolean)


103
104
105
# File 'lib/mongo_tree.rb', line 103

def has_content?
    @content != nil
end

#initialize(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/mongo_tree.rb', line 27

def initialize(options={})   
  super   
  self.set_as_root!
  
  if self.parents.nil?
    self.parents = []
  end
end

#is_root?Boolean

Indicates whether this node is a root node. Note that orphaned children will also be reported as root nodes.

Returns:

  • (Boolean)


109
110
111
# File 'lib/mongo_tree.rb', line 109

def is_root?
    parents == []
end

#lengthObject

Convenience synonym for Tree#size



184
185
186
# File 'lib/mongo_tree.rb', line 184

def length
    size()
end

#parentObject



145
146
147
# File 'lib/mongo_tree.rb', line 145

def parent
  self.class.find(self.parents.last) rescue nil
end

Pretty prints the tree starting with the receiver node.



189
190
191
192
# File 'lib/mongo_tree.rb', line 189

def print_tree(tab = 0)
    puts((' ' * tab) + self.to_s)
    children {|child| child.print_tree(tab + 4)}
end

#remove!(child) ⇒ Object

Removes the specified child node from the receiver node. The removed children nodes are orphaned but available if an alternate reference exists. Returns the child node.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongo_tree.rb', line 71

def remove!(child)
  # Make sure child belongs to the object trying to remove it
  child = self.children.select {|c| c.id == child.id}.first
  unless child.nil?
    old_parents = child.parents
    child.parents = []
    child.save
    # Update the parent tree of all this object's children
    child.descendants.each do |descendant, level|
      descendant.parents = descendant.parents - old_parents
      descendant.save
    end
  end
  
  return child
end

#remove_all!Object

Removes all children from the receiver node.



95
96
97
98
99
100
# File 'lib/mongo_tree.rb', line 95

def remove_all!
    for child in children
        child.remove_from_parent!
    end
    self
end

#remove_from_parent!Object

Removes this node from its parent. If this is the root node, then does nothing.



90
91
92
# File 'lib/mongo_tree.rb', line 90

def remove_from_parent!
    parent.remove!(self) unless is_root?
end

#rootObject

Returns the root for this node.



141
142
143
# File 'lib/mongo_tree.rb', line 141

def root
  self.parents.blank? ? self : self.class.find(self.parents.first)
end

#set_as_root!Object

Private method which sets this node as a root node.



233
234
235
# File 'lib/mongo_tree.rb', line 233

def set_as_root!
    parents = []
end

#siblingsObject

Returns an array of siblings for this node. If a block is provided, yeilds each of the sibling nodes to the block.



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/mongo_tree.rb', line 197

def siblings
    if block_given?
        return nil if is_root?
        for sibling in parent.children
          yield sibling if sibling != self
        end
    else
        siblings = []
        parent.children.each {|sibling| siblings << sibling if sibling != self}
        siblings
    end
end

#sizeObject

Returns the total number of nodes in this tree, rooted at the receiver node.



179
180
181
# File 'lib/mongo_tree.rb', line 179

def size
    descendants.size
end

#to_sObject



36
37
38
39
40
41
42
# File 'lib/mongo_tree.rb', line 36

def to_s
  s = size()
  "Node ID: #{@name} Content: #{@content} Parent: " +
          (is_root?()  ? "ROOT" : "#{@parent.name}") +
          " Children: #{children.length}" +
          " Total Nodes: #{s}"
end