Module: ChartMogul::ConfigAttributes

Included in:
ChartMogul
Defined in:
lib/chartmogul/config_attributes.rb

Instance Method Summary collapse

Instance Method Details

#config_accessor(attribute, default_value = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chartmogul/config_attributes.rb', line 5

def config_accessor(attribute, default_value = nil)
  define_method(attribute) do
    attr = config.send(attribute) || global_config.send(attribute) || default_value
    if attr.nil?
      raise ConfigurationError, "Configuration for #{attribute} not set"
    end

    attr
  end

  define_method("global_#{attribute}") do
    attr = global_config.send(attribute) || default_value
    if attr.nil?
      raise ConfigurationError, "Global configuration for #{attribute} not set"
    end

    attr
  end

  define_method("#{attribute}=") do |val|
    config.send("#{attribute}=", val)
    Thread.current[ChartMogul::APIResource::THREAD_CONNECTION_KEY] = nil
    val
  end

  define_method("global_#{attribute}=") do |val|
    global_config.send("#{attribute}=", val)
    Thread.current[ChartMogul::APIResource::THREAD_CONNECTION_KEY] = nil
    val
  end
end