class Flows::Contract::Predicate

Makes a contract from 1-argument lambda.

Such lambdas works like [predicates](en.wikipedia.org/wiki/Predicate_(mathematical_logic)).

@example

positive_check = Flows::Contract::Predicate.new 'must be a positive integer' do |x|
  x.is_a?(Integer) && x > 0
end

positive_check === 10
# => true

positive_check === -100
# => false

Public Class Methods

new(error_message, &block) click to toggle source

@param error_message error message if check fails @yield [object] lambda to wrap into a contract @yieldreturn [Boolean] lambda should return a boolean

# File lib/flows/contract/predicate.rb, line 21
def initialize(error_message, &block)
  @error_message = error_message
  @block = block
end

Public Instance Methods

check!(other) click to toggle source

@see Contract#check!

# File lib/flows/contract/predicate.rb, line 27
def check!(other)
  raise Error.new(other, @error_message) unless @block === other

  true
end