Sha256: 461e7c061732c545e7c80d81d3b49b546cecc572608aa29b77b44575ae9fc87d

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

##
# @example
#   ConvenientService::Utils::Hash::AssertValidKeys.call({foo: "foo", bar: "bar"}, [:foo, :bar])
#
module ConvenientService
  module Utils
    module Hash
      class AssertValidKeys < Support::Command
        ##
        # @!attribute [r] hash
        #   @return [Hash]
        #
        attr_reader :hash

        ##
        # @!attribute [r] valid_keys
        #   @return [Array]
        #
        attr_reader :valid_keys

        ##
        # @param hash [Hash]
        # @param valid_keys [Array]
        # @return [void]
        #
        def initialize(hash, valid_keys)
          @hash = hash
          @valid_keys = valid_keys.flatten
        end

        ##
        # @return [Hash]
        #
        # @internal
        #   NOTE: Copied with minimal modifications from:
        #   https://api.rubyonrails.org/classes/Hash.html#method-i-assert_valid_keys
        #
        def call
          hash.each_key do |key|
            next if valid_keys.include?(key)

            ::ConvenientService.raise ::ArgumentError.new("Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(", ")}")
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.19.1 lib/convenient_service/utils/hash/assert_valid_keys.rb
convenient_service-0.19.0 lib/convenient_service/utils/hash/assert_valid_keys.rb
convenient_service-0.18.0 lib/convenient_service/utils/hash/assert_valid_keys.rb
convenient_service-0.17.0 lib/convenient_service/utils/hash/assert_valid_keys.rb