Class: Commons::Yml

Inherits:
Hash
  • Object
show all
Defined in:
lib/commons/yml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from) ⇒ Yml

Returns a new instance of Yml.



11
12
13
# File 'lib/commons/yml.rb', line 11

def initialize(from)
  self.from = from
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



9
10
11
# File 'lib/commons/yml.rb', line 9

def from
  @from
end

Class Method Details

.prepare_configs(configs, file, symbolize_keys = false) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/commons/yml.rb', line 44

def self.prepare_configs(configs, file, symbolize_keys = false)
  if configs.is_a?(Hash) 
    configs.symbolize_keys! if symbolize_keys && configs.respond_to?(:symbolize_keys!)
    configs = Yml.new(file).merge(configs)
  end
  configs
end

.read(file) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/commons/yml.rb', line 15

def self.read(file)

  raise ArgumentError, 'Param file é required' if file == nil

  yaml_file = file.end_with?(".yml") ? file : "#{file}.yml"
  begin
    yaml_configs = YAML::load_file(yaml_file)
    return self.prepare_configs(yaml_configs, yaml_file, true)
  rescue Exception => e
    message = "Error when reading yml file: #{e.message}"        
    STDOUT.puts(message)
    raise e
  end
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
# File 'lib/commons/yml.rb', line 30

def [](key)
  prepare_value(super(key), key)
end

#fetch(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/commons/yml.rb', line 34

def fetch(*args)
  begin
    return prepare_value(super(*args), args.first)
  rescue IndexError => e
    message = "Missing attribute '#{args.first}' in #{self.from}"
    STDOUT.puts(message)
    raise RuntimeError.new(message)
  end
end