Sha256: 9d4761341c7b16486d8bdc3aa1207a032fdd6a6399806df2ccba8aee979d4d69

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true
module Speculation
  # @private
  class TupleSpec < Spec
    include NamespacedSymbols
    S = Speculation

    def initialize(preds)
      @preds = preds

      @delayed_specs = Concurrent::Delay.new do
        preds.map { |pred| S.send(:specize, pred) }
      end
    end

    def conform(collection)
      specs = @delayed_specs.value!

      unless Utils.array?(collection) && collection.count == specs.count
        return S::INVALID
      end

      return_value = collection.class.new

      collection.zip(specs).each do |(value, spec)|
        conformed_value = spec.conform(value)

        if S.invalid?(conformed_value)
          return S::INVALID
        else
          return_value += [conformed_value]
        end
      end

      return_value
    end

    def explain(path, via, inn, value)
      if !Utils.array?(value)
        [{ :path => path, :val => value, :via => via, :in => inn, :pred => [Utils.method(:array?), [value]] }]
      elsif @preds.count != value.count
        [{ :path => path, :val => value, :via => via, :in => inn, :pred => [Utils.method(:count_eq), [@preds, value.count]] }]
      else
        probs = @preds.zip(value).each_with_index.flat_map { |(pred, x), index|
          unless S.pvalid?(pred, x)
            S.explain1(pred, Utils.conj(path, index), via, Utils.conj(inn, index), x)
          end
        }

        probs.compact
      end
    end

    def gen(overrides, path, rmap)
      return @gen if @gen

      gens = @preds.each_with_index.
        map { |p, i| S.gensub(p, overrides, Utils.conj(path, i), rmap) }

      ->(rantly) do
        gens.map { |g| g.call(rantly) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
speculation-0.3.0 lib/speculation/spec/tuple_spec.rb