Class: EacRubyUtils::PathsHash

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

Defined Under Namespace

Classes: EntryKeyError, Node, PathSearch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_hash = {}) ⇒ PathsHash

Returns a new instance of PathsHash.



28
29
30
# File 'lib/eac_ruby_utils/paths_hash.rb', line 28

def initialize(source_hash = {})
  @root = Node.new(source_hash)
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



26
27
28
# File 'lib/eac_ruby_utils/paths_hash.rb', line 26

def root
  @root
end

Class Method Details

.parse_entry_key(entry_key) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eac_ruby_utils/paths_hash.rb', line 10

def parse_entry_key(entry_key)
  r = entry_key.to_s.strip
  raise ::EacRubyUtils::PathsHash::EntryKeyError, 'Entry key cannot start or end with dot' if
  r.start_with?('.') || r.end_with?('.')

  r = r.split('.').map(&:strip)
  if r.empty?
    raise ::EacRubyUtils::PathsHash::EntryKeyError, "Entry key \"#{entry_key}\" is empty"
  end
  return r.map(&:to_sym) unless r.any?(&:blank?)

  raise ::EacRubyUtils::PathsHash::EntryKeyError,
        "Entry key \"#{entry_key}\" has at least one blank part"
end

Instance Method Details

#[](entry_key) ⇒ Object



32
33
34
# File 'lib/eac_ruby_utils/paths_hash.rb', line 32

def [](entry_key)
  root.read_entry(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
end

#[]=(entry_key, entry_value) ⇒ Object



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

def []=(entry_key, entry_value)
  root.write_entry(
    ::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key), entry_value
  )
end

#fetch(entry_key) ⇒ Object



42
43
44
# File 'lib/eac_ruby_utils/paths_hash.rb', line 42

def fetch(entry_key)
  root.fetch(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
end

#key?(entry_key) ⇒ Boolean

Returns:



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

def key?(entry_key)
  root.entry?(::EacRubyUtils::PathsHash::PathSearch.parse_entry_key(entry_key))
end