Class: EacRubyUtils::PathsHash::Node

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

Constant Summary collapse

PATH_SEARCH_UNENDED_ERROR_MESSAGE =
'Path search is not a Node and is not ended'

Instance Method Summary collapse

Constructor Details

#initialize(source_hash) ⇒ Node

Returns a new instance of Node.


11
12
13
14
# File 'lib/eac_ruby_utils/paths_hash/node.rb', line 11

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

#entry?(path_search) ⇒ Boolean

Returns:


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

def entry?(path_search)
  return false unless data.key?(path_search.cursor)
  return true if path_search.ended?
  return data.fetch(path_search.cursor).entry?(path_search.succeeding) if
    data.fetch(path_search.cursor).is_a?(Node)

  false # Paths continue and there is not available nodes
end

#fetch(path_search) ⇒ Object


25
26
27
28
29
30
31
32
33
34
# File 'lib/eac_ruby_utils/paths_hash/node.rb', line 25

def fetch(path_search)
  if data.key?(path_search.cursor)
    node = data.fetch(path_search.cursor)
    return (node.is_a?(Node) ? node.to_h : node) if path_search.ended?
    return nil if node.blank?
    return node.fetch(path_search.succeeding) if node.is_a?(Node)
  end

  path_search.raise_error(PATH_SEARCH_UNENDED_ERROR_MESSAGE)
end

#read_entry(path_search) ⇒ Object


40
41
42
43
44
45
46
47
# File 'lib/eac_ruby_utils/paths_hash/node.rb', line 40

def read_entry(path_search)
  node = data[path_search.cursor]
  return (node.is_a?(Node) ? node.to_h : node) if path_search.ended?
  return nil if node.blank?
  return node.read_entry(path_search.succeeding) if node.is_a?(Node)

  path_search.raise_error(PATH_SEARCH_UNENDED_ERROR_MESSAGE)
end

#to_hObject


36
37
38
# File 'lib/eac_ruby_utils/paths_hash/node.rb', line 36

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

#write_entry(path_search, value) ⇒ Object


49
50
51
52
53
54
55
# File 'lib/eac_ruby_utils/paths_hash/node.rb', line 49

def write_entry(path_search, value)
  if path_search.ended?
    data[path_search.cursor] = value.is_a?(Hash) ? self.class.new(value) : value
  else
    assert_data_node(path_search.cursor).write_entry(path_search.succeeding, value)
  end
end