Sha256: 7cc78f8007e57f62ac133e84a261135b88f7594bd18410a1fd399b70a0c9ad95

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module UnifiedSettings
  module Handlers
    # Base handler for a setting source
    #
    # All handers should inherit from this base class and implement the method
    #   def get(key, case_sensitive: nil)
    class Base
      KEY_NESTING_SEPARATOR = '.'

      # rubocop:disable Lint/UnusedMethodArgument
      def get(key, case_sensitive: nil)
        raise 'Needs to be implemented by subclasss'
      end
      # rubocop:enable Lint/UnusedMethodArgument

      protected

      def split(val, separator)
        case val
        when String
          val.split(separator)
        when Symbol
          val.to_s.split(separator)
        when Array
          val
        else
          raise 'key must either be a string or an array'
        end
      end

      def case_sensitive?(case_sensitive)
        if case_sensitive.nil?
          UnifiedSettings.config.case_sensitive
        else
          case_sensitive
        end
      end

      def to_symbol_array(val, separator: KEY_NESTING_SEPARATOR)
        split(val, separator).map(&:to_sym)
      end

      def nested_key_exists?(hash, keys)
        current_level = hash
        keys.each do |key|
          return false if current_level.nil?
          return true if current_level.key?(key)

          current_level = current_level[key]
        end
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unified_settings-0.1.1 lib/unified_settings/handlers/base.rb
unified_settings-0.1.0 lib/unified_settings/handlers/base.rb