Sha256: 5c8621d717ecfadffbbb8aa1b4ee531670cfa8d70139d152fb95d6db79eaec0c

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require_relative 'base'

module UnifiedSettings
  module Handlers
    # Setting handler for Rails.application.credentials
    class Credentials < Base
      def defined?(key, case_sensitive: nil)
        key_arr = to_symbol_array(key)
        case_sensitive = case_sensitive?(case_sensitive)

        return true if nested_key_exists?(Rails.application.credentials,
                                          key_arr)
        return false if case_sensitive

        return true if nested_key_exists?(
          Rails.application.credentials, key_arr.map(&:upcase)
        )
        return true if nested_key_exists?(
          Rails.application.credentials, 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 = Rails.application.credentials.dig(*key_arr)
        return val unless val.nil?
        return nil if case_sensitive

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

        Rails.application.credentials.dig(*key_arr.map(&:upcase))
      end

      protected

      def nested_key_exists?(hash, keys)
        if Rails.gem_version < Gem::Version.new('7.1.0')
          super
        else
          return false if hash.nil?

          # Rails 7.1 adds a key? method and so we need to get the config
          # out of the credential object so that it can be treated as a hash
          #  https://github.com/rails/rails/blob/v7.1.1/activesupport/lib/active_support/encrypted_file.rb#L58-L60
          current_level = hash.config
          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
end

Version data entries

1 entries across 1 versions & 1 rubygems

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