Class: EacRubyUtils::PathsHash::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_ruby_utils/paths_hash.rb

Instance Method Summary collapse

Constructor Details

#initialize(source_hash) ⇒ Node

Returns a new instance of Node.


46
47
48
49
# File 'lib/eac_ruby_utils/paths_hash.rb', line 46

def initialize(source_hash)
  source_hash.assert_argument(Hash, 'source_hash')
  @data = source_hash.map { |k, v| [k.to_sym, v.is_a?(Hash) ? Node.new(v) : v] }.to_h
end

Instance Method Details

#read_entry(path, current) ⇒ Object

Raises:


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eac_ruby_utils/paths_hash.rb', line 55

def read_entry(path, current)
  validate_path(path, current)
  node_key = path.shift
  node = data[node_key]
  return (node.is_a?(Node) ? node.to_h : node) if path.empty?
  return nil if node.blank?
  return node.read_entry(path, current + [node_key]) if node.is_a?(Node)

  raise(EntryKeyError,
        "Path #{current.join(',')} is not a Node and path continues (#{current + path})")
end

#to_hObject


51
52
53
# File 'lib/eac_ruby_utils/paths_hash.rb', line 51

def to_h
  data.map { |k, v| [k, v.is_a?(Node) ? v.to_h : v] }.to_h
end

#write_entry(path, value, current) ⇒ Object


67
68
69
70
71
# File 'lib/eac_ruby_utils/paths_hash.rb', line 67

def write_entry(path, value, current)
  validate_path(path, current)
  node_key = path.shift
  write_entry_value(path, node_key, value, current)
end