Sha256: b38e764c50ef9dcc96aad26666753c7abd18d7a83b3c2b914047db294fb60741

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

# This is a Ruby translation of clojure.spec:
#   https://github.com/clojure/clojure/blob/master/src/clj/clojure/spec.clj
# All credit belongs with Rich Hickey and contributors for their original work.

module Speculation
  # @private
  class PredicateSpec < Spec
    include NamespacedSymbols
    S = Speculation

    def initialize(predicate, should_conform, gen, unconformer)
      @predicate = predicate
      @should_conform = should_conform
      @gen = gen
      @unconformer = unconformer
    end

    def conform(value)
      ret = case @predicate
            when Set            then @predicate.include?(value)
            when Regexp, Module then @predicate === value
            else                     @predicate.call(value)
            end

      if @should_conform
        ret
      else
        ret ? value : :"Speculation/invalid"
      end
    end

    def unform(value)
      return value unless @should_conform

      if @unconformer
        @unconformer.call(value)
      else
        raise "no unformer for conformer"
      end
    end

    def explain(path, via, inn, value)
      if S.invalid?(S.dt(@predicate, value))
        [{ :path => path, :val => value, :via => via, :in => inn, :pred => [@predicate, [value]] }]
      end
    end

    def with_gen(gen)
      self.class.new(@predicate, @should_conform, gen, @unconformer)
    end

    def gen(_, _, _)
      if @gen
        @gen.call
      else
        Gen.gen_for_pred(@predicate)
      end
    end

    def inspect
      "#{self.class}(#{@name || @predicate.inspect})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
speculation-0.4.2 lib/speculation/spec/predicate_spec.rb