Class: EacRubyUtils::PathsHash

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

Defined Under Namespace

Classes: EntryKeyError, Node

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_hash = {}) ⇒ PathsHash

Returns a new instance of PathsHash.


24
25
26
# File 'lib/eac_ruby_utils/paths_hash.rb', line 24

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root


22
23
24
# File 'lib/eac_ruby_utils/paths_hash.rb', line 22

def root
  @root
end

Class Method Details

.parse_entry_key(entry_key) ⇒ Object

Raises:


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

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

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

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

Instance Method Details

#[](entry_key) ⇒ Object


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

def [](entry_key)
  root.read_entry(self.class.parse_entry_key(entry_key), [])
end

#[]=(entry_key, entry_value) ⇒ Object


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

def []=(entry_key, entry_value)
  root.write_entry(self.class.parse_entry_key(entry_key), entry_value, [])
end