Sha256: d9ac6e762ca30ac2dfa38395139501d750f101f3227f89c09d70c9761754d369

Contents?: true

Size: 936 Bytes

Versions: 7

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

require 'dry/initializer'
require 'dry/validation/constants'

module Dry
  module Validation
    # Abstract class for handling rule blocks
    #
    # @see Rule
    # @see Macro
    #
    # @api private
    class Function
      extend Dry::Initializer

      # @!attribute [r] block
      #   @return [Proc]
      #   @api private
      option :block

      # @!attribute [r] block_options
      #   @return [Hash]
      #   @api private
      option :block_options, default: -> { block ? map_keywords(block) : EMPTY_HASH }

      private

      # Extract options for the block kwargs
      #
      # @param [Proc] block Callable
      # @return Hash
      #
      # @api private
      def map_keywords(block)
        block
          .parameters
          .select { |arg,| arg.equal?(:keyreq) }
          .map { |_, name| [name, BLOCK_OPTIONS_MAPPINGS[name]] }
          .to_h
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-validation-1.4.2 lib/dry/validation/function.rb
dry-validation-1.4.1 lib/dry/validation/function.rb
dry-validation-1.4.0 lib/dry/validation/function.rb
dry-validation-1.3.1 lib/dry/validation/function.rb
dry-validation-1.3.0 lib/dry/validation/function.rb
dry-validation-1.2.1 lib/dry/validation/function.rb
dry-validation-1.2.0 lib/dry/validation/function.rb