Class: Alula::Config

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

Instance Method Summary collapse

Constructor Details

#initialize(override = {}, config_file = "config.yml") ⇒ Config

Returns a new instance of Config.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/alula/config.rb', line 5

def initialize(override = {}, config_file = "config.yml")
  @config = Hashie::Mash.new
  @_used = Hashie::Mash.new # Keep track of applied configuration
  
  # Load default configuration
  @config.update(DEFAULT_CONFIG)
  
  # Load project specific configuration
  @config.update(YAML.load_file(config_file)) if ::File.exists?(config_file)
  
  # Load overrides
  @config.update(override)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alula/config.rb', line 23

def method_missing(meth, *args, &blk)
  if meth[/=$/]
    @config.send(meth, *args)
  else
    value = @config.send(:[], meth)
    if value.kind_of?(Hashie::Mash)
      # Try environment
      value = value[environment] if value.has_key?(environment)
    end
    @_used.send("#{meth}=", value)
    
    value
  end
end

Instance Method Details

#write_cache(file) ⇒ Object



19
20
21
# File 'lib/alula/config.rb', line 19

def write_cache(file)
  File.open(file, 'w') {|io| io.puts JSON.parse(@_used.to_json).to_yaml }
end