Class: EacRubyUtils::SettingsProvider::SettingValue

Inherits:
Object
  • Object
show all
Includes:
Listable, EacRubyUtils::SimpleCache
Defined in:
lib/eac_ruby_utils/settings_provider/setting_value.rb

Constant Summary

Constants included from EacRubyUtils::SimpleCache

EacRubyUtils::SimpleCache::UNCACHED_METHOD_NAME_SUFFIX, EacRubyUtils::SimpleCache::UNCACHED_METHOD_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EacRubyUtils::SimpleCache

#method_missing, #reset_cache, #respond_to_missing?, uncached_method_name

Constructor Details

#initialize(source, key, options) ⇒ SettingValue

Returns a new instance of SettingValue.


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

def initialize(source, key, options)
  @source = source
  @key = key
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EacRubyUtils::SimpleCache

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


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

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options


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

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source


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

def source
  @source
end

Instance Method Details

#constant_name(fullname = false) ⇒ Object


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

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

#valueObject


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

def value
  parsed_options.order.each do |method|
    value = send("value_by_#{method}")
    return value if value
  end
  return parsed_options.default if parsed_options.respond_to?(OPTION_DEFAULT)
  return nil unless parsed_options.required

  raise_key_not_found
end

#value_by_constantObject


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

def value_by_constant
  source.class.const_get(constant_name)
rescue NameError
  nil
end

#value_by_methodObject


46
47
48
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 46

def value_by_method
  source.respond_to?(key, true) ? source.send(key) : nil
end

#value_by_settings_objectObject


50
51
52
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 50

def value_by_settings_object
  source.settings_object[key.to_s] || source.settings_object[key.to_sym]
end