Class: TreeHash
- Inherits:
-
Object
- Object
- TreeHash
- Defined in:
- lib/bblib/core/classes/tree_hash.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#node_class ⇒ Object
readonly
Returns the value of attribute node_class.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #absolute_path ⇒ Object
- #absolute_paths ⇒ Object
- #ancestors ⇒ Object
-
#bridge(paths) ⇒ Object
Generate a path using a dot (.) delimited path e.g.
- #child(key, symbol_sensitive = false) ⇒ Object
- #child? ⇒ Boolean
- #child_exists?(key, symbol_sensitive = false) ⇒ Boolean
- #children? ⇒ Boolean
- #copy(paths) ⇒ Object
- #copy_all(paths) ⇒ Object
- #copy_to(hash, *paths) ⇒ Object
- #delete(*paths) ⇒ Object
- #delete_child(key, symbol_sensitive = false) ⇒ Object
- #descendants ⇒ Object
- #find(path) ⇒ Object
- #find_join(*paths) ⇒ Object
- #find_join_hash(key_path, val_path) ⇒ Object
- #find_multi(*paths) ⇒ Object
- #following_siblings(limit = 0) ⇒ Object
- #index ⇒ Object
-
#initialize(object = {}, parent = nil) ⇒ TreeHash
constructor
A new instance of TreeHash.
- #inspect ⇒ Object
- #key ⇒ Object
- #keys ⇒ Object
- #kill ⇒ Object
- #leaf_children ⇒ Object
- #move(paths) ⇒ Object
- #move_all(paths) ⇒ Object
- #move_to(hash, *paths) ⇒ Object
- #next_sibling ⇒ Object
- #path ⇒ Object
- #paths ⇒ Object
- #preceeding_siblings(limit = 0) ⇒ Object
- #previous_sibling ⇒ Object
- #process(processor, &block) ⇒ Object
- #replace_with(object) ⇒ Object
- #root ⇒ Object
- #root? ⇒ Boolean
- #set(paths) ⇒ Object
- #sibling(offset) ⇒ Object
- #siblings ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
- #to_tree_hash ⇒ Object
- #value ⇒ Object
Constructor Details
Instance Attribute Details
#children ⇒ Object (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_class ⇒ Object (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 |
#parent ⇒ Object (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_path ⇒ Object
222 223 224 225 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 222 def absolute_path anc = ancestors[1..-1] (anc.nil? ? [] : anc.map(&:path) + [path]).join('.') end |
#absolute_paths ⇒ Object
286 287 288 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 286 def absolute_paths root.paths end |
#ancestors ⇒ Object
216 217 218 219 220 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 216 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
20 21 22 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 20 def child? !root? end |
#child_exists?(key, symbol_sensitive = false) ⇒ 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 && key.respond_to?(:to_i) [0...children.size] === key.to_i else false end end |
#children? ⇒ 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
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 243 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
328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 328 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 |
#descendants ⇒ Object
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
302 303 304 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 302 def following_siblings(limit = 0) siblings[(index + 1)..-(limit.to_i + 1)] end |
#index ⇒ Object
239 240 241 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 239 def index parent.children.values.index(self) end |
#inspect ⇒ Object
227 228 229 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 227 def inspect value end |
#key ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 270 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 |
#keys ⇒ Object
297 298 299 300 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 297 def keys return [] unless @children.respond_to?(:keys) @children.keys end |
#kill ⇒ Object
255 256 257 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 255 def kill root.delete(absolute_path) end |
#leaf_children ⇒ Object
290 291 292 293 294 295 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 290 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_sibling ⇒ Object
315 316 317 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 315 def next_sibling sibling(1) end |
#path ⇒ Object
282 283 284 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 282 def path parent.node_class == Array ? "[#{key}]" : key.to_s.gsub('.', '\\.') end |
#paths ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 208 def paths if Array >= node_class || Hash >= node_class value.squish.keys else [] end end |
#preceeding_siblings(limit = 0) ⇒ Object
306 307 308 309 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 306 def preceeding_siblings(limit = 0) limit = limit.to_i siblings[(limit.zero? ? limit : (index - limit))..(index - 1)] end |
#previous_sibling ⇒ Object
319 320 321 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 319 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 |
#root ⇒ Object
323 324 325 326 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 323 def root return self if root? ancestors.find(&:root?) end |
#root? ⇒ 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
311 312 313 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 311 def sibling(offset) siblings[index + offset.to_i] end |
#siblings ⇒ Object
235 236 237 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 235 def siblings parent.children.values.map { |c| c == self ? nil : c }.compact end |
#size ⇒ Object
204 205 206 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 204 def size @children.respond_to?(:size) ? @children.size : 1 end |
#to_s ⇒ Object
231 232 233 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 231 def to_s value.to_s end |
#to_tree_hash ⇒ Object
196 197 198 |
# File 'lib/bblib/core/classes/tree_hash.rb', line 196 def to_tree_hash self end |