module Que::Utils::Assertions

Public Instance Methods

assert(*args) { || ... } click to toggle source
# File lib/que/utils/assertions.rb, line 12
def assert(*args)
  comparison, object, pass = _check_assertion_args(*args)
  return object if pass

  message =
    if block_given?
      yield.to_s
    elsif comparison
      "Expected #{comparison.inspect}, got #{object.inspect}!"
    else
      "Assertion failed!"
    end

  # Remove this method from the backtrace, to make errors clearer.
  raise AssertionFailed, message, caller
end
assert?(*args) click to toggle source
# File lib/que/utils/assertions.rb, line 29
def assert?(*args)
  _, _, pass = _check_assertion_args(*args)
  !!pass
end

Private Instance Methods

_check_assertion_args(first, second = (second_omitted = true; nil)) click to toggle source

Want to support:

assert(x)                       # Truthiness.
assert(thing, other)            # Trip-equals.
assert([thing1, thing2], other) # Multiple Trip-equals.
# File lib/que/utils/assertions.rb, line 40
def _check_assertion_args(first, second = (second_omitted = true; nil))
  if second_omitted
    comparison = nil
    object     = first
  else
    comparison = first
    object     = second
  end

  pass =
    if second_omitted
      object
    elsif comparison.is_a?(Array)
      comparison.any? { |k| k === object }
    else
      comparison === object
    end

  [comparison, object, pass]
end