Sha256: dd0de3ecd1644e805e293da84b766435a4e527d34bef00a2dfb8cec450b0bf00

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 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)
        return false if hash.nil?

        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

1 entries across 1 versions & 1 rubygems

Version Path
unified_settings-0.1.2 lib/unified_settings/handlers/base.rb