Sha256: e1a9c617aab6056f2d487549109d4bbe4060c3a69a16993e6dd2bb8f1201f7e1

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module UnifiedSettings
  module Handlers
    # Setting handler for Ruby constants
    class Constants < Base
      CONSTANT_KEY_NESTING_SEPARATOR = '::'

      def defined?(key, case_sensitive: nil)
        klass, variable_names = key_to_class_and_variable(
          key, case_sensitive:
        )

        variable_names.each do |name|
          if klass
            return true if klass.const_defined?(name)
          elsif Object.const_defined?(name)
            return true
          end
        rescue NameError
          # Ignore if the constant is not defined
        end

        false
      end

      def get(key, case_sensitive: nil)
        klass, variable_names = key_to_class_and_variable(
          key, case_sensitive:
        )

        variable_names.each do |name|
          return klass.const_get(name) if klass

          return Object.const_get(name)
        rescue NameError
          # Ignore if the constant is not defined
        end

        nil
      end

      private

      def key_to_class_and_variable(key, case_sensitive: nil)
        key_arr = to_symbol_array(key,
                                  separator: CONSTANT_KEY_NESTING_SEPARATOR)
        case_sensitive = case_sensitive?(case_sensitive)

        klass, variable =
          if key_arr.length > 1
            [
              key_arr[0..-2].join(CONSTANT_KEY_NESTING_SEPARATOR)
                            .safe_constantize,
              key_arr[-1]
            ]
          else
            [nil, key_arr[0]]
          end

        variable_names = if case_sensitive
                           [variable]
                         else
                           [variable, variable.upcase, variable.downcase]

                         end

        [klass, variable_names]
      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/constants.rb
unified_settings-0.1.1 lib/unified_settings/handlers/constants.rb