Module: Konfigurator::Simple::ClassMethods

Defined in:
lib/konfigurator/simple.rb

Instance Method Summary collapse

Instance Method Details

#disable(name) ⇒ Object

It “disables” given setting. It means that it assigns false value to the specified setting key.

disable :foo # => set :foo, false


60
61
62
# File 'lib/konfigurator/simple.rb', line 60

def disable(name)
  set(name, false)
end

#enable(name) ⇒ Object

It “enables” given setting. It means that it assigns true value to the specified setting key.

enable :foo # => set :foo, true


52
53
54
# File 'lib/konfigurator/simple.rb', line 52

def enable(name)
  set(name, true)
end

#load_settings(fname, use_envs = true) ⇒ Object

It loads settings from given .yml file. File should have structure like this one:

development:
  foo: bar
  bar: true
production: 
  bla: foobar


88
89
90
91
92
93
94
95
# File 'lib/konfigurator/simple.rb', line 88

def load_settings(fname, use_envs=true)
  conf = YAML.load_file(fname)
  if use_envs
    conf[env.to_s].each {|k,v| set k.to_sym, v }
  else
    conf.each {|k,v| set k.to_sym, v }
  end
end

#set(name, value) ⇒ Object

Assigns given value to the specified setting key.

set :foo, "YadaYadaYaday!"
set :bae, true

See also shortcuts for boolean settings: #enable and #disable methods.



71
72
73
74
75
76
77
78
# File 'lib/konfigurator/simple.rb', line 71

def set(name, value)
  name = name.to_sym
  unless self.respond_to?(name)
    meta = class << self; self; end
    meta.send(:define_method, name) { settings[name] }
  end 
  settings[name] = value
end