Class: YamlConfig

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

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ YamlConfig

Returns a new instance of YamlConfig.

Raises:

  • (Exception)


4
5
6
7
8
9
10
11
12
13
14
# File 'lib/yaml_config/yaml_config.rb', line 4

def initialize(file, options = {})
  @root = options.delete(:root).to_s
  @yaml = if file.respond_to?(:read)
    YAML.load(file.read)
  elsif file.is_a?(String)
    File.exists?(file) ? YAML.load_file(file) : YAML.load(file)
  end
  
  raise Exception.new("File is not valid YAML") unless @yaml.is_a?(Hash)
  raise NameError.new("Root :#{@root} doesn't exist in given YAML") unless @root.empty? || @yaml.has_key?(@root)
end

Instance Method Details

#get(key) ⇒ Object



16
17
18
# File 'lib/yaml_config/yaml_config.rb', line 16

def get(key)
  (@root.empty? ? @yaml[key.to_s] : @yaml[@root][key.to_s]) || NullProperty.new
end