Sha256: b436ad350fe0f93c6de0fae89bf3ae59c958d813e9f318cae80a61b7c6413296

Contents?: true

Size: 1.43 KB

Versions: 2

Compression:

Stored size: 1.43 KB

Contents

module BloodContracts::Core
  # Refinement type which represents invalid data
  class ContractFailure < Refined
    # Constructs a ContractsFailure using the given value
    # (for ContractFailure value is an error)
    #
    # @return [ContractFailure]
    #
    def initialize(value = nil, **)
      super
      @match = self
      return unless @value
      @context[:errors] = (@context[:errors].to_a << @value.to_h)
    end

    # Merge errors with the errors of another ContractFailure
    #
    # @param contract_failure [ContractFailure] other errors container which to
    #   merge with
    # @return [ContractFailure]
    #
    def merge!(contract_failure)
      @context[:errors] = @context[:errors].to_a + contract_failure.errors
      self
    end

    # List of errors per type after the data matching process
    #
    # @return [Array<Hash<BC::Refined, String>>]
    #
    def errors
      @context[:errors].to_a
    end

    # Flatten list of error messages
    #
    # @return [Array<String>]
    #
    def messages
      errors.reduce(:merge).values.flatten!
    end

    # Merged map of errors per type after the data matching process
    #
    # @return [Hash<BC::Refined, String>]
    #
    def errors_h
      errors.reduce(:merge)
    end
    alias to_h errors_h

    # The type which is the result of validation
    # (for ContractFailure is always self)
    #
    # @return [BC::Refined]
    #
    def match
      self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blood_contracts-core-0.4.4 lib/blood_contracts/core/contract_failure.rb
blood_contracts-core-0.4.3 lib/blood_contracts/core/contract_failure.rb