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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fast_tree/model.rb', line 60

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 = <<-"EOS"
UPDATE #{self.to_s.underscore.pluralize}
  SET l_ptr = CASE
          WHEN l_ptr >= #{left}
            AND r_ptr <= #{right}
            THEN l_ptr + 1
          WHEN l_ptr > #{right}
            THEN l_ptr + 2
          ELSE l_ptr
          END,
r_ptr = CASE
          WHEN l_ptr >= #{left}
            AND r_ptr <= #{right}
            THEN r_ptr + 1
          WHEN l_ptr < #{left}
            AND r_ptr > #{right}
            THEN r_ptr + 2
          WHEN l_ptr > #{right}
            THEN r_ptr + 2
          ELSE r_ptr
        END,
depth = CASE
          WHEN l_ptr >= #{left}
            AND r_ptr <= #{right}
            THEN depth + 1
          ELSE depth
        END
  WHERE r_ptr > #{left}
  EOS

  ActiveRecord::Base.connection.execute(sql)

  # return left and right pointers between which parent is embedded
  {l_ptr: left, r_ptr: right + 2}
end

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

structure operation



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fast_tree/model.rb', line 20

def add_parent(parent, children, &block)
  parent_depth = children.first.depth

  # 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.depth = parent_depth
  parent.save
end

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



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

def create_parent(attributes = {}, children, &block)
  parent_depth = children.first.depth

  # 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]
  attributes[:depth] = parent_depth

  self.create(attributes, &block)
end

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



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fast_tree/model.rb', line 47

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

#find_rootObject

model operation



104
105
106
# File 'lib/fast_tree/model.rb', line 104

def find_root
  self.find_by(l_ptr: 0)
end

#find_subtree_by_root(node) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/fast_tree/model.rb', line 108

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



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

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