Sha256: 22cf306bb9d6b18a3c991948dddb3441058e305f46bdd8a9b1992a9bbb1e5c1c

Contents?: true

Size: 835 Bytes

Versions: 1

Compression:

Stored size: 835 Bytes

Contents

require 'xpect/failed_spec'

module Xpect
  class Pred
    attr_reader :default

    def initialize(pred:, error_msg: nil, default: nil)
      unless pred.is_a?(Proc)
        raise "pred must be a Proc"
      end

      @pred = pred
      @error_msg = error_msg
      @default = default
    end

    def conform!(value:, path: nil)
      return @default if @default && value.nil?

      if value.nil?
        raise FailedSpec, "the value at path '#{ path }' is missing"
      end

      unless @pred.call(value)
        error_msg = if @error_msg
                      "'#{ value }' does not meet spec for '#{ path }': '#{ @error_msg }'"
                    else
                      "'#{ value }' does not meet spec for '#{ path }'"
                    end

        raise FailedSpec, error_msg
      end

      value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xpect-0.1.0 lib/xpect/pred.rb