Module: FastTree::Model::ClassMethods

Defined in:
lib/fast_tree/model.rb

Overview

Class Methods

Instance Method Summary collapse

Instance Method Details

#_create_parent_embedded_space(children) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/fast_tree/model.rb', line 48

def _create_parent_embedded_space(children)
  left  = children.max {|c| c.l_ptr}.l_ptr
  right = children.min {|c| c.r_ptr}.r_ptr

  sql = "UPDATE \#{self.to_s.underscore.pluralize}\n  SET l_ptr = CASE\n          WHEN l_ptr >= \#{left}\n            AND r_ptr <= \#{right}\n            THEN l_ptr + 1\n          WHEN l_ptr > \#{right}\n            THEN l_ptr + 2\n          ELSE l_ptr\n          END,\nr_ptr = CASE\n          WHEN l_ptr >= \#{left}\n            AND r_ptr <= \#{right}\n            THEN r_ptr + 1\n          WHEN l_ptr < \#{left}\n            AND r_ptr > \#{right}\n            THEN r_ptr + 2\n          WHEN l_ptr > \#{right}\n            THEN r_ptr + 2\n          ELSE r_ptr\n        END\n  WHERE r_ptr > \#{left}\n  EOS\n\n  ActiveRecord::Base.connection.execute(sql)\n\n  # return left and right pointers between which parent is embedded\n  {l_ptr: left, r_ptr: right + 2}\nend\n"

#add_parent(parent, children, &block) ⇒ Object

structure operation



15
16
17
18
19
20
21
22
23
# File 'lib/fast_tree/model.rb', line 15

def add_parent(parent, children, &block)
  # create space for parent
  ptrs = _create_parent_embedded_space(children)

  # parent node's pointer
  parent.l_ptr = ptrs[:l_ptr]
  parent.r_ptr = ptrs[:r_ptr]
  parent.save
end

#create_parent(attributes = {}, children, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fast_tree/model.rb', line 25

def create_parent(attributes = {}, children, &block)
  # create space for parent
  ptrs = _create_parent_embedded_space(children)

  # parent node's pointer
  attributes[:l_ptr] = ptrs[:l_ptr]
  attributes[:r_ptr] = ptrs[:r_ptr]

  self.create(attributes, &block)
end

#create_tree(attributes = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/fast_tree/model.rb', line 36

def create_tree(attributes={}, &block)
  root = self.find_root
  if root
    root
  else
    attributes[:l_ptr] = 0
    attributes[:r_ptr] = 1
    self.create(attributes, &block)
  end
end

#find_rootObject

model operation



86
87
88
# File 'lib/fast_tree/model.rb', line 86

def find_root
  self.find_by(l_ptr: 0)
end

#find_subtree_by_root(node) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/fast_tree/model.rb', line 90

def find_subtree_by_root(node)
  l_ptr = node.l_ptr
  r_ptr = node.r_ptr

  self.where(self.arel_table[:l_ptr].gteq(l_ptr))
      .where(self.arel_table[:r_ptr].lteq(r_ptr))
end

for debugging



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fast_tree/model.rb', line 102

def print_subtree(root)
  puts("printing sub tree for #{root.name}...")
  subtree = find_subtree_by_root(root)
  subtree.order(l_ptr: :asc).each do |st_node|
    st_node.reload
    # white spaces on the left
    wsl = st_node.l_ptr.times.map{|s| "_"}.join('')
    # arrows
    ars = (st_node.r_ptr - st_node.l_ptr ).times.map{|s| "-"}.join('')
    # white spaces on the right
    wsr = (root.width + 2 - wsl.size - ars.size).times.map{|s| " "}.join('')

    puts("#{wsl}<#{ars}>#{wsr} ... #{st_node.name}")
  end
  puts("done.\n")
end