Class: SimpleConf::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-conf/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Loader

Returns a new instance of Loader.



9
10
11
# File 'lib/simple-conf/loader.rb', line 9

def initialize(klass)
  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/simple-conf/loader.rb', line 7

def klass
  @klass
end

Instance Method Details

#pathObject



27
28
29
30
31
# File 'lib/simple-conf/loader.rb', line 27

def path
  class_name = klass.name.downcase.split("::").last

  "./config/#{class_name}.yml"
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simple-conf/loader.rb', line 13

def run
  yaml_file.each_pair do |key, value|
    set(key, value)
  end

  yaml_file.fetch(Rails.env, {}).each_pair do |key, value|
    set(key, value)
  end if rails_environment_defined?

  yaml_file.fetch(@klass.env, {}).each_pair do |key, value|
    set(key, value)
  end if @klass.respond_to?(:env)
end

#yaml_fileObject



33
34
35
36
# File 'lib/simple-conf/loader.rb', line 33

def yaml_file
  content = File.open(path).read
  YAML.load(ERB.new(content).result)
end