Module: Stall::Utils::ConfigDSL

Included in:
Config, Payments::Config
Defined in:
lib/stall/utils/config_dsl.rb

Overview

The DSL allows for declaring instance-level configuration params with defaults in a single line

Instance Method Summary collapse

Instance Method Details

#param(name, default = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stall/utils/config_dsl.rb', line 7

def param(name, default = nil)
  attr_writer name

  instance_variable_name = :"@#{ name }"

  define_method(name) do
    if (value = instance_variable_get(instance_variable_name))
      value
    else
      default = default.call if default.is_a?(Proc)
      instance_variable_set(instance_variable_name, default)
    end
  end
end