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?, #sanitize_cache_key, uncached_method_name

Constructor Details

#initialize(source, key, options) ⇒ SettingValue

Returns a new instance of SettingValue.



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

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

rubocop:disable Style/OptionalBooleanParameter



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

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

#valueObject



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

def value
  parsed_options.order.each do |method|
    return send("value_by_#{method}") if send("value_by_#{method}?")
  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
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 40

def value_by_constant
  source.class.const_get(constant_name)
end

#value_by_constant?Boolean

Returns:



44
45
46
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 44

def value_by_constant?
  source.class.const_defined?(constant_name)
end

#value_by_methodObject



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

def value_by_method
  source.send(key)
end

#value_by_method?Boolean

Returns:



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

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

#value_by_settings_objectObject



56
57
58
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 56

def value_by_settings_object
  source.settings_object.fetch(key)
end

#value_by_settings_object?Boolean

Returns:



60
61
62
# File 'lib/eac_ruby_utils/settings_provider/setting_value.rb', line 60

def value_by_settings_object?
  source.settings_object.key?(key)
end