Sha256: 3e13ba0b645609dcc40e1755b59ad30bfcc1a4afbcda900fb53ad62f6c845e92

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'dry/validation'
require 'dry/schema'
require 'dried_interaction/instance_methods'

module DriedInteraction
  module Dsl
    MODES = %i[strict soft].freeze
    attr_accessor :contract_validator, :contract_mode, :for_method

    def contract(kind: :simple, mode: MODES.first, &block)
      @contract_mode = resolve_contract_mode(mode)
      @contract_validator = resolve_contract_validator(kind, &block)
    end

    def soft_contract(args = {}, &block)
      contract(args.merge(mode: :soft), &block)
    end

    def strict_contract(args = {}, &block)
      contract(args.merge(mode: :strict))
    end

    private

    def resolve_contract_mode(mode)
      MODES.include?(mode.to_sym) ? mode.to_sym : (raise ArgumentError)
    end

    def resolve_contract_validator(kind, &block)
      case kind
      when :simple
        Dry::Schema.Params(&block)
      when :extended
        Dry::Validation.Contract(&block)
      when Dry::Schema::Params
        kind.merge(Dry::Schema.Params(&block))
      when Dry::Validation::Contract
        kind.build(&block)
      else
        raise ArgumentError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dried_interaction-0.3.0 lib/dried_interaction/dsl.rb