Module: Konfigurator::ClassMethods
- Defined in:
- lib/konfigurator.rb
Overview
InstanceMethods
Instance Method Summary collapse
-
#configure(*envs, &block) ⇒ Object
Run once, at startup, in any environment.
-
#disable(name) ⇒ Object
It “disables” given setting.
-
#enable(name) ⇒ Object
It “enables” given setting.
-
#environment ⇒ Object
(also: #env)
It returns name of current environment.
-
#load_settings(fname) ⇒ Object
It loads settings from given
.yml
file. -
#set(name, value) ⇒ Object
Assigns given value to the specified setting key.
-
#settings ⇒ Object
(also: #config)
Returns hash with defined configuration options.
Instance Method Details
#configure(*envs, &block) ⇒ Object
Run once, at startup, in any environment. To add an option use the set
method (For boolean values You can use #enable
and #disable
methods):
configure do
set :foo, 'bar'
enable :bar
disable :yadayada
end
Run only when the environment (or the APP_ENV
env variable)is set to :production
:
configure :production do
...
end
Run when the environment is set to either :production
or :test
:
configure :production, :test do
...
end
80 81 82 |
# File 'lib/konfigurator.rb', line 80 def configure(*envs, &block) class_eval(&block) if envs.empty? || envs.include?(env.to_sym) end |
#disable(name) ⇒ Object
It “disables” given setting. It means that it assigns false
value to the specified setting key.
disable :foo # => set :foo, false
115 116 117 |
# File 'lib/konfigurator.rb', line 115 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
107 108 109 |
# File 'lib/konfigurator.rb', line 107 def enable(name) set(name, true) end |
#environment ⇒ Object Also known as: env
It returns name of current environment.
136 137 138 |
# File 'lib/konfigurator.rb', line 136 def environment settings[:environment] ||= settings[:env] || ENV["APP_ENV"] || :development end |
#load_settings(fname) ⇒ Object
It loads settings from given .yml
file. File should have structure like this one:
development:
foo: bar
bar: true
production:
bla: foobar
92 93 94 95 |
# File 'lib/konfigurator.rb', line 92 def load_settings(fname) conf = YAML.load_file(fname) conf[env.to_s].each {|k,v| set k.to_sym, v } 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.
126 127 128 129 130 131 132 133 |
# File 'lib/konfigurator.rb', line 126 def set(name, value) name = name.to_sym unless self.respond_to?(name) = class << self; self; end .send(:define_method, name) { settings[name] } end settings[name] = value end |
#settings ⇒ Object Also known as: config
Returns hash with defined configuration options.
98 99 100 |
# File 'lib/konfigurator.rb', line 98 def settings @settings ||= {} end |