Sha256: ad0da958fcd1a3167e785b8d1403359d5bb70fbc869d030f31a156fad558e1fa

Contents?: true

Size: 879 Bytes

Versions: 1

Compression:

Stored size: 879 Bytes

Contents

# frozen_string_literal: true

module Solid::Result::Contract
  require_relative 'contract/error'
  require_relative 'contract/type_checker'
  require_relative 'contract/interface'
  require_relative 'contract/evaluator'
  require_relative 'contract/disabled'
  require_relative 'contract/for_types'
  require_relative 'contract/for_types_and_values'

  NONE = Evaluator.new(Disabled, Disabled).freeze

  def self.evaluate(data, contract)
    contract ||= NONE

    contract.type_and_value!(data)

    TypeChecker.new(data.type, expectations: contract)
  end

  ToEnsure = ->(spec, config) do
    return Disabled if spec.nil?

    spec.is_a?(::Hash) ? ForTypesAndValues.new(spec, config) : ForTypes.new(Array(spec))
  end

  def self.new(success:, failure:, config:)
    Evaluator.new(ToEnsure[success, config], ToEnsure[failure, config])
  end

  private_constant :ToEnsure
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solid-result-2.0.0 lib/solid/result/contract.rb