Class: Daddy::Utils::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/daddy/utils/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(yaml_path_or_hash = nil) ⇒ Config

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
# File 'lib/daddy/utils/config.rb', line 6

def initialize(yaml_path_or_hash = nil)
  if yaml_path_or_hash.is_a?(Hash)
    @hash = yaml_path_or_hash
  elsif yaml_path_or_hash and File.exist?(yaml_path_or_hash)
    @hash = YAML.load_file(yaml_path_or_hash)
  else
    @hash = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



38
39
40
# File 'lib/daddy/utils/config.rb', line 38

def method_missing(name, *args)
  self[name]
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/daddy/utils/config.rb', line 16

def [](key)
  ret = false
    
  if key.to_s.end_with?('?')
    ret = @hash[key.to_s[0..-2]] ? true : false
  else
    ret = @hash[key.to_s]
    
    if ret.nil?
      ret = self.class.new
    elsif ret.is_a?(Hash)
      ret = self.class.new(ret)
    end
  end
    
  ret
end

#[]=(key, value) ⇒ Object



34
35
36
# File 'lib/daddy/utils/config.rb', line 34

def []=(key, value)
  @hash[key.to_s] = value
end