Class: ConfigReader::ConfigHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/config_reader/config_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



26
27
28
# File 'lib/config_reader/config_hash.rb', line 26

def method_missing(key, *args, &block)
  ignore_missing_keys ? self[key] : fetch(key)
end

Instance Attribute Details

#ignore_missing_keysObject

Returns the value of attribute ignore_missing_keys.



3
4
5
# File 'lib/config_reader/config_hash.rb', line 3

def ignore_missing_keys
  @ignore_missing_keys
end

Class Method Details

.convert_hash(hash, ignore_missing_keys = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/config_reader/config_hash.rb', line 5

def self.convert_hash(hash, ignore_missing_keys = false)
  config_hash = new
  config_hash.ignore_missing_keys = ignore_missing_keys

  hash.each_pair do |key, value|
    config_hash[key.to_sym] = if value.is_a?(Hash)
      convert_hash(value, ignore_missing_keys)
    else
      value
    end
  end

  config_hash
end

Instance Method Details

#[](key) ⇒ Object



20
21
22
23
24
# File 'lib/config_reader/config_hash.rb', line 20

def [](key)
  fetch(key.to_sym)
rescue KeyError => e
  raise e unless ignore_missing_keys
end

#respond_to_missing?(m) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/config_reader/config_hash.rb', line 30

def respond_to_missing?(m, *)
  config.key?(m)
end