Class: SimpleConf::Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



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

def klass
  @klass
end

Instance Method Details

#config_file_nameObject



30
31
32
33
34
# File 'lib/simple-conf/loader.rb', line 30

def config_file_name
  klass.respond_to?(:config_file_name) ?
    klass.config_file_name :
    "#{klass.name.downcase.split("::").last}"
end

#pathsObject



23
24
25
26
27
28
# File 'lib/simple-conf/loader.rb', line 23

def paths
  [
    "./config/#{config_file_name}.yml",
    "./config/#{config_file_name}.local.yml",
  ]
end

#runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simple-conf/loader.rb', line 7

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

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

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

#yaml_file(path) ⇒ Object



36
37
38
39
40
# File 'lib/simple-conf/loader.rb', line 36

def yaml_file(path)
  return {} unless File.exists?(path)
  content = File.open(path).read
  YAML.load(ERB.new(content).result)
end