Class: BBLib::HashPathProc

Inherits:
Object
  • Object
show all
Includes:
Effortless
Defined in:
lib/hash_path/proc.rb

Overview

This class wraps around a hash path or set of paths and maps a set of actions for modifying elements at the matching path.

Instance Method Summary collapse

Methods included from Effortless

#_attrs, included

Instance Method Details

#check_condition(value) ⇒ Object



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

def check_condition(value)
  return true unless condition
  if condition.is_a?(String)
    eval(condition)
  else
    condition.call(value)
  end
rescue => e
  false
end

#process(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hash_path/proc.rb', line 18

def process(hash)
  return hash unless @action && hash
  tree = hash.to_tree_hash
  paths.each do |path|
    children = recursive ? tree.find(path).flat_map(&:descendants) : tree.find(path)
    children.each do |child|
      next unless check_condition(child.value)
      HashPathProcs.send(find_action(action), child, *full_args, class_based: class_based)
    end
  end
  hash.replace(tree.value) rescue tree.value
end