Class: TreeHash

Inherits:
Object
  • Object
show all
Defined in:
lib/bblib/core/classes/tree_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = {}, parent = nil) ⇒ TreeHash

Returns a new instance of TreeHash.



4
5
6
7
8
9
# File 'lib/bblib/core/classes/tree_hash.rb', line 4

def initialize(object = {}, parent = nil)
  @children = {}
  @parent   = parent
  object    = object.value if object.is_a?(TreeHash)
  replace_with(object)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



2
3
4
# File 'lib/bblib/core/classes/tree_hash.rb', line 2

def children
  @children
end

#node_classObject (readonly)

Returns the value of attribute node_class.



2
3
4
# File 'lib/bblib/core/classes/tree_hash.rb', line 2

def node_class
  @node_class
end

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/bblib/core/classes/tree_hash.rb', line 2

def parent
  @parent
end

Instance Method Details

#[](key) ⇒ Object



60
61
62
# File 'lib/bblib/core/classes/tree_hash.rb', line 60

def [](key)
  children[key]
end

#[]=(key, value) ⇒ Object



64
65
66
# File 'lib/bblib/core/classes/tree_hash.rb', line 64

def []=(key, value)
  add_child(key, value)
end

#absolute_pathObject



223
224
225
226
# File 'lib/bblib/core/classes/tree_hash.rb', line 223

def absolute_path
  anc = ancestors[1..-1]
  (anc.nil? ? [] : anc.map(&:path) + [path]).join('.')
end

#absolute_pathsObject



287
288
289
# File 'lib/bblib/core/classes/tree_hash.rb', line 287

def absolute_paths
  root.paths
end

#ancestorsObject



217
218
219
220
221
# File 'lib/bblib/core/classes/tree_hash.rb', line 217

def ancestors
  return [] if root?
  return [parent] if parent.root?
  parent.ancestors + [parent]
end

#bridge(paths) ⇒ Object

Generate a path using a dot (.) delimited path e.g. bridge(‘cats.jackson’ => :my_value) This modifies the underlying container in place



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bblib/core/classes/tree_hash.rb', line 109

def bridge(paths)
  paths = paths.map { |a| [a, nil] }.to_h if paths.is_a?(Array)
  paths.each do |path, value|
    parts     = path.to_s.split(/(?<=[^\\])\./)
    node      = self
    next_part = false
    until next_part.nil?
      part      = next_part || process_bridge_part(parts.shift)
      next_part = process_bridge_part(parts.shift)
      if node.child_exists?(part)
        if next_part.is_a?(Integer)
          next_next = process_bridge_part(parts.first)
          if next_next.is_a?(Integer)
            node[part][next_part] = [] unless node[part][next_part] && node[part][next_part].node_class == Array
          else
            node[part][next_part] = {} unless node[part][next_part] && node[part][next_part].node_class == Hash
          end
        end
        next_part.nil? ? node[part] = value : node = node.child(part)
      else
        if next_part.nil?
          node[part] = value
        else
          node[part] = next_part.is_a?(Integer) ? Array.new(next_part) : {}
        end
        node[part][next_part] = process_bridge_part(parts.first).is_a?(Integer) ? [] : {} if next_part.is_a?(Integer)
        node = node[part]
      end
    end
  end
  self
end

#child(key, symbol_sensitive = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/bblib/core/classes/tree_hash.rb', line 24

def child(key, symbol_sensitive = false)
  case
  when Hash >= node_class
    children[key] || (symbol_sensitive ? nil : children[key.to_s.to_sym])
  when Array >= node_class
    children[key.to_i]
  else
    nil
  end
end

#child?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/bblib/core/classes/tree_hash.rb', line 20

def child?
  !root?
end

#child_exists?(key, symbol_sensitive = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
# File 'lib/bblib/core/classes/tree_hash.rb', line 35

def child_exists?(key, symbol_sensitive = false)
  case
  when Hash >= node_class
    children.include?(key) || (!symbol_sensitive && children.include?(key.to_s.to_sym))
  when Array >= node_class
    [0...children.size] === key.to_i
  else
    false
  end
end

#children?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/bblib/core/classes/tree_hash.rb', line 46

def children?
  (node_class == Hash || node_class == Array) && !children.empty?
end

#copy(paths) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/bblib/core/classes/tree_hash.rb', line 142

def copy(paths)
  paths.each do |from, to|
    value = find(from).first
    bridge(to => value)
  end
  self
end

#copy_all(paths) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/bblib/core/classes/tree_hash.rb', line 150

def copy_all(paths)
  paths.each do |from, to|
    value = find(from)
    bridge(to => value)
  end
  self
end

#copy_to(hash, *paths) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/bblib/core/classes/tree_hash.rb', line 178

def copy_to(hash, *paths)
  hash = TreeHash.new(hash) unless hash.is_a?(TreeHash)
  paths.each do |path|
    hash.bridge(path => find(path).first)
  end
  hash
end

#delete(*paths) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bblib/core/classes/tree_hash.rb', line 244

def delete(*paths)
  paths.flat_map do |path|
    find(path).map do |child|
      if child.root?
        delete_child(path)
      else
        child.parent.delete_child(child.key)
      end
    end
  end
end

#delete_child(key, symbol_sensitive = false) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/bblib/core/classes/tree_hash.rb', line 329

def delete_child(key, symbol_sensitive = false)
  case
  when Hash >= node_class
    child = symbol_sensitive ? nil : children.delete(key.to_s.to_sym)
    child = children.delete(key) unless child
  when Array >= node_class
    children.delete(key.to_i)
  else
    nil
  end
end

#descendantsObject



50
51
52
53
54
55
56
57
58
# File 'lib/bblib/core/classes/tree_hash.rb', line 50

def descendants
  return [] unless children?
  desc = []
  children.each do |key, child|
    desc << child
    desc += child.descendants if child.children?
  end
  desc
end

#find(path) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/bblib/core/classes/tree_hash.rb', line 68

def find(path)
  path = HashPath.new(*path) unless path.is_a?(HashPath)
  matches = [self]
  path.parts.each do |part|
    break if matches.empty?
    matches = matches.flat_map do |match|
      part.matches(match)
    end
  end
  matches
end

#find_join(*paths) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/bblib/core/classes/tree_hash.rb', line 86

def find_join(*paths)
  results = find_multi(*paths)
  (0..(results.max_by(&:size).size - 1)).map do |index|
    results.map do |result|
      result[index]
    end
  end
end

#find_join_hash(key_path, val_path) ⇒ Object



95
96
97
# File 'lib/bblib/core/classes/tree_hash.rb', line 95

def find_join_hash(key_path, val_path)
  find_join(key_path, val_path).to_h
end

#find_multi(*paths) ⇒ Object



80
81
82
83
84
# File 'lib/bblib/core/classes/tree_hash.rb', line 80

def find_multi(*paths)
  paths.map do |path|
    find(path)
  end
end

#following_siblings(limit = 0) ⇒ Object



303
304
305
# File 'lib/bblib/core/classes/tree_hash.rb', line 303

def following_siblings(limit = 0)
  siblings[(index + 1)..-(limit.to_i + 1)]
end

#indexObject



240
241
242
# File 'lib/bblib/core/classes/tree_hash.rb', line 240

def index
  parent.children.values.index(self)
end

#inspectObject



228
229
230
# File 'lib/bblib/core/classes/tree_hash.rb', line 228

def inspect
  value
end

#keyObject



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/bblib/core/classes/tree_hash.rb', line 271

def key
  return nil if root?
  case
  when Hash >= parent.node_class
    parent.keys[index]
  when Array >= parent.node_class
    index
  else
    nil
  end
end

#keysObject



298
299
300
301
# File 'lib/bblib/core/classes/tree_hash.rb', line 298

def keys
  return [] unless @children.respond_to?(:keys)
  @children.keys
end

#killObject



256
257
258
# File 'lib/bblib/core/classes/tree_hash.rb', line 256

def kill
  root.delete(absolute_path)
end

#leaf_childrenObject



291
292
293
294
295
296
# File 'lib/bblib/core/classes/tree_hash.rb', line 291

def leaf_children
  return self unless children?
  children.map do |k, v|
    v.children? ? v.leaf_children : v
  end.flatten
end

#move(paths) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/bblib/core/classes/tree_hash.rb', line 158

def move(paths)
  paths.each do |from, to|
    values = find(from)
    next if values.empty?
    value = values.first
    bridge(to => value)
    value.kill if value
  end
  self
end

#move_all(paths) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/bblib/core/classes/tree_hash.rb', line 169

def move_all(paths)
  paths.each do |from, to|
    value = find(from)
    bridge(to => value)
    value.map(&:kill)
  end
  self
end

#move_to(hash, *paths) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/bblib/core/classes/tree_hash.rb', line 186

def move_to(hash, *paths)
  hash = TreeHash.new(hash) unless hash.is_a?(TreeHash)
  paths.each do |path|
    value = find(path).first
    hash.bridge(path => value)
    value.kill if value
  end
  hash
end

#next_siblingObject



316
317
318
# File 'lib/bblib/core/classes/tree_hash.rb', line 316

def next_sibling
  sibling(1)
end

#pathObject



283
284
285
# File 'lib/bblib/core/classes/tree_hash.rb', line 283

def path
  parent.node_class == Array ? "[#{key}]" : key.to_s.gsub('.', '\\.')
end

#pathsObject



208
209
210
211
212
213
214
215
# File 'lib/bblib/core/classes/tree_hash.rb', line 208

def paths
  case
  when Array >= node_class, Hash >= node_class
    value.squish.keys
  else
    []
  end
end

#preceeding_siblings(limit = 0) ⇒ Object



307
308
309
310
# File 'lib/bblib/core/classes/tree_hash.rb', line 307

def preceeding_siblings(limit = 0)
  limit = limit.to_i
  siblings[(limit.zero? ? limit : (index - limit))..(index - 1)]
end

#previous_siblingObject



320
321
322
# File 'lib/bblib/core/classes/tree_hash.rb', line 320

def previous_sibling
  sibling(-1)
end

#process(processor, &block) ⇒ Object



200
201
202
# File 'lib/bblib/core/classes/tree_hash.rb', line 200

def process(processor, &block)
  # TODO Add ability to process values or keys in tree hashes
end

#replace_with(object) ⇒ Object



11
12
13
14
# File 'lib/bblib/core/classes/tree_hash.rb', line 11

def replace_with(object)
  @node_class = object.class
  build_from(object)
end

#rootObject



324
325
326
327
# File 'lib/bblib/core/classes/tree_hash.rb', line 324

def root
  return self if root?
  ancestors.find(&:root?)
end

#root?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/bblib/core/classes/tree_hash.rb', line 16

def root?
  @parent.nil?
end

#set(paths) ⇒ Object



99
100
101
102
103
104
# File 'lib/bblib/core/classes/tree_hash.rb', line 99

def set(paths)
  paths.each do |path, value|
    find(path).each { |child| child.replace_with(value) }
  end
  self
end

#sibling(offset) ⇒ Object



312
313
314
# File 'lib/bblib/core/classes/tree_hash.rb', line 312

def sibling(offset)
  siblings[index + offset.to_i]
end

#siblingsObject



236
237
238
# File 'lib/bblib/core/classes/tree_hash.rb', line 236

def siblings
  parent.children.values.map { |c| c == self ? nil : c }.compact
end

#sizeObject



204
205
206
# File 'lib/bblib/core/classes/tree_hash.rb', line 204

def size
  @children.respond_to?(:size) ? @children.size : 1
end

#to_sObject



232
233
234
# File 'lib/bblib/core/classes/tree_hash.rb', line 232

def to_s
  value.to_s
end

#to_tree_hashObject



196
197
198
# File 'lib/bblib/core/classes/tree_hash.rb', line 196

def to_tree_hash
  self
end

#valueObject



260
261
262
263
264
265
266
267
268
269
# File 'lib/bblib/core/classes/tree_hash.rb', line 260

def value
  case
  when Hash >= node_class
    children.hmap { |k, v| [k, v.value] }
  when Array >= node_class
    children.values.map(&:value)
  else
    children
  end
end