Class: BoostInfo::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/boost_info/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
13
14
# File 'lib/boost_info/node.rb', line 7

def initialize(params={})
  @root = params[:root]
  @key = params[:key].to_s if params[:key] # normalize key
  @value = params[:value]
  @parent = params[:parent]

  @childrens = nil
end

Instance Attribute Details

#childrensObject

Returns the value of attribute childrens.



5
6
7
# File 'lib/boost_info/node.rb', line 5

def childrens
  @childrens
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/boost_info/node.rb', line 5

def key
  @key
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/boost_info/node.rb', line 5

def parent
  @parent
end

#rootObject

Returns the value of attribute root.



5
6
7
# File 'lib/boost_info/node.rb', line 5

def root
  @root
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/boost_info/node.rb', line 5

def value
  @value
end

Instance Method Details

#auto_insert(path, value, params = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/boost_info/node.rb', line 97

def auto_insert(path, value, params={})
  last_node = create_path(path, params)
  new_node = if value.is_a?(BoostInfo::Node)
               last_node.insert_node(value, params)
             else
               last_node.value = value
               last_node
             end
  new_node
end

#create_path(path, params = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/boost_info/node.rb', line 75

def create_path(path, params={})
  return self if path.empty?

  params[:force] = true unless params.key?(:force)

  current_node = self
  path.each do |new_key|
    existing_nodes = current_node.get(new_key)
    current_node = if existing_nodes.empty?
                     current_node.insert(new_key, nil, params)
                   else
                     # force - использовать существующие ноды
                     if params[:force]
                       existing_nodes.first
                     else
                       current_node.insert(new_key, nil, params)
                     end
                   end
  end
  current_node
end

#delete(key) ⇒ Object



145
146
147
148
149
150
# File 'lib/boost_info/node.rb', line 145

def delete(key)
  node = find_children(key.to_s)
  return unless node

  delete_children(node)
end

#delete_children(node) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/boost_info/node.rb', line 158

def delete_children(node)
  return unless @childrens

  node.parent = nil

  index = @childrens.index(node)
  @childrens.delete_at(index)
  @childrens = nil if @childrens.empty?

  node
end

#find_by_key(key, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/boost_info/node.rb', line 42

def find_by_key(key, params={})
  return [] unless childrens

  result = []
  childrens.each do |node|
    if key.is_a?(Regexp)
      result << node if node.key =~ key
    else
      result << node if node.key == key.to_s
    end

    if node.childrens
      result_from_children = node.find_by_key(key, params)
      result.concat(result_from_children)
    end
  end
  result
end

#find_by_path(path, params = {}) ⇒ Object



26
27
28
29
# File 'lib/boost_info/node.rb', line 26

def find_by_path(path, params={})
  path_copy = path.dup
  traverse_path(path_copy, params)
end

#find_children(key_to_find) ⇒ Object



152
153
154
155
156
# File 'lib/boost_info/node.rb', line 152

def find_children(key_to_find)
  return unless @childrens
  key_to_find = key_to_find.to_s
  @childrens.find { |children| children.key == key_to_find }
end

#get(key_to_get, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/boost_info/node.rb', line 16

def get(key_to_get, params={})
  return [] unless @childrens

  if key_to_get.is_a?(Regexp)
    @childrens.select { |c| c.key =~ key_to_get }
  else
    @childrens.select { |c| c.key == key_to_get.to_s }
  end
end

#insert(key, value, params = {}) ⇒ Object



108
109
110
111
112
# File 'lib/boost_info/node.rb', line 108

def insert(key, value, params={})
  node = Node.new(key: key, value: value)
  insert_node(node, params)
  node
end

#insert_node(node, params = {}) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/boost_info/node.rb', line 114

def insert_node(node, params={})
  node.parent = self
  @childrens ||= []

  if params[:after]
    insert_node_after(params[:after], node)
  elsif params[:before]
    insert_node_before(params[:before], node)
  elsif params[:prepend]
    @childrens.unshift(node)
  else
    @childrens << node
  end
  node
end

#insert_node_after(key, node) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/boost_info/node.rb', line 130

def insert_node_after(key, node)
  index = @childrens.index { |c| c.key == key.to_s }
  index += 1 if index
  index = @childrens.size if index > @childrens.size || index.nil?
  @childrens.insert(index, node)
  node
end

#insert_node_before(key, node) ⇒ Object



138
139
140
141
142
143
# File 'lib/boost_info/node.rb', line 138

def insert_node_before(key, node)
  index = @childrens.index { |c| c.key == key.to_s }
  index ||= 0
  @childrens.insert(index, node)
  node
end

#parentsObject



61
62
63
64
65
66
67
68
# File 'lib/boost_info/node.rb', line 61

def parents
  result = []
  unless root
    result << parent
    result.concat(parent.parents)
  end
  result
end

#siblingsObject



70
71
72
73
# File 'lib/boost_info/node.rb', line 70

def siblings
  parent_childrens = (parent && parent.childrens) || []
  parent_childrens.reject { |c| c == self }
end

#to_h(params = {}) ⇒ Object



170
171
172
# File 'lib/boost_info/node.rb', line 170

def to_h(params={})
  BoostInfo::Generator.new(self, params).to_hash
end

#to_info(params = {}) ⇒ Object



174
175
176
# File 'lib/boost_info/node.rb', line 174

def to_info(params={})
  BoostInfo::Generator.new(self, params).to_info
end

#traverse_path(path, params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/boost_info/node.rb', line 31

def traverse_path(path, params={})
  if path.size > 1
    next_node_key = path.shift
    node = get(next_node_key).first
    node.find_by_path(path) if node
  elsif path.size == 1
    last_key = path.last
    get(last_key).first
  end
end