Sha256: 34029f46413ab272fb3440147943d5013ec471e3bd919a02593090ca0424a73d
Contents?: true
Size: 1.9 KB
Versions: 166
Compression:
Stored size: 1.9 KB
Contents
# frozen_string_literal: true require 'active_support/core_ext/string/inflections' require 'eac_ruby_utils/listable' require 'eac_ruby_utils/simple_cache' require 'eac_ruby_utils/struct' module EacRubyUtils module SettingsProvider class SettingValue include ::EacRubyUtils::Listable include ::EacRubyUtils::SimpleCache attr_reader :source, :key, :options lists.add_symbol :option, :default, :order, :required def initialize(source, key, options) @source = source @key = key @options = options end def constant_name(fullname = false) name = key.to_s.underscore.upcase name = "#{source.class.name}::#{name}" if fullname name end 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 def value_by_constant source.class.const_get(constant_name) rescue NameError nil end def value_by_method source.respond_to?(key, true) ? source.send(key) : nil end def value_by_settings_object source.settings_object[key.to_s] || source.settings_object[key.to_sym] end private def parsed_options_uncached r = self.class.lists.option.hash_keys_validate!(options.symbolize_keys) r[:required] = true unless r.key?(OPTION_REQUIRED) r[:order] = source.setting_search_order if r[OPTION_ORDER].nil? ::EacRubyUtils::Struct.new(r) end def raise_key_not_found raise "Setting \"#{key}\" not found. Supply in #{source.settings_object_name}, implement "\ "a \"#{key}\" method or declare a #{constant_name(true)} constant." end end end end
Version data entries
166 entries across 166 versions & 4 rubygems