Sha256: 29d6a0aab12e769b8561f8bdfda78410d11c22a7fe19d8b360867e0eaa5fe178

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module UnifiedSettings
  module Handlers
    # Setting handler for Config gem
    class ConfigGem < Base
      # By default the gem makes a `Settings` object available
      DEFAULT_CONST_NAME = 'Settings'

      def initialize(const_name: nil)
        super()
        @const_name = const_name || DEFAULT_CONST_NAME
      end

      def defined?(key, case_sensitive: nil)
        key_arr = to_symbol_array(key)
        case_sensitive = case_sensitive?(case_sensitive)

        return true if nested_key_exists?(setting_obj, key_arr)
        return false if case_sensitive

        return true if nested_key_exists?(setting_obj, key_arr.map(&:upcase))
        return true if nested_key_exists?(setting_obj, key_arr.map(&:downcase))

        false
      end

      def get(key, case_sensitive: nil)
        key_arr = to_symbol_array(key)
        case_sensitive = case_sensitive?(case_sensitive)

        val = setting_obj.dig(*key_arr)
        return val unless val.nil?
        return nil if case_sensitive

        val = setting_obj.dig(*key_arr.map(&:downcase))
        return val unless val.nil?

        setting_obj.dig(*key_arr.map(&:upcase))
      end

      private

      def setting_obj
        Object.const_get("::#{@const_name}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unified_settings-0.1.2 lib/unified_settings/handlers/config_gem.rb
unified_settings-0.1.1 lib/unified_settings/handlers/config_gem.rb