Module: EacRubyUtils::SettingsProvider

Included in:
Console::DocoptRunner
Defined in:
lib/eac_ruby_utils/settings_provider.rb

Overview

Provide a option by constant, method or options object.

Instance Method Summary collapse

Instance Method Details

#setting_constant_name(key, fullname = false) ⇒ Object


9
10
11
12
13
# File 'lib/eac_ruby_utils/settings_provider.rb', line 9

def setting_constant_name(key, fullname = false)
  name = key.to_s.underscore.upcase
  name = "#{self.class.name}::#{name}" if fullname
  name
end

#setting_search_orderObject


15
16
17
# File 'lib/eac_ruby_utils/settings_provider.rb', line 15

def setting_search_order
  %w[settings_object method constant]
end

#setting_value(key, options = {}) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eac_ruby_utils/settings_provider.rb', line 27

def setting_value(key, options = {})
  options = parse_setting_value_options(options)
  options.order.each do |method|
    value = send("setting_value_by_#{method}", key)
    return value if value
  end
  return nil unless options.required

  raise "Setting \"#{key}\" not found. Supply in #{settings_object_name}, implement a " \
    "\"#{key}\" method or declare a #{setting_constant_name(key, true)} constant."
end

#setting_value_by_constant(key) ⇒ Object


39
40
41
42
43
44
45
46
# File 'lib/eac_ruby_utils/settings_provider.rb', line 39

def setting_value_by_constant(key)
  constant_name = setting_constant_name(key)
  begin
    self.class.const_get(constant_name)
  rescue NameError
    nil
  end
end

#setting_value_by_method(key) ⇒ Object


48
49
50
# File 'lib/eac_ruby_utils/settings_provider.rb', line 48

def setting_value_by_method(key)
  respond_to?(key) ? send(key) : nil
end

#setting_value_by_settings_object(key) ⇒ Object


52
53
54
# File 'lib/eac_ruby_utils/settings_provider.rb', line 52

def setting_value_by_settings_object(key)
  settings_object[key.to_s] || settings_object[key.to_sym]
end

#settings_objectObject


19
20
21
# File 'lib/eac_ruby_utils/settings_provider.rb', line 19

def settings_object
  respond_to?(settings_object_name) ? send(settings_object_name) : {}
end

#settings_object_nameObject


23
24
25
# File 'lib/eac_ruby_utils/settings_provider.rb', line 23

def settings_object_name
  'settings'
end