class Flows::Contract::CaseEq

Makes a contract from provided object's case equality check.

@example String contract

string_check = Flows::Contract::CaseEq.new(String)

string_check.check(111)
# => Flows::Result::Err.new('must match `String`')

string_check === 'sdfdsfsd'
# => true

@example Integer contract with custom error message

int_check = Flows::Contract::CaseEq.new(Integer, 'must be an integer')

int_check.check('111')
# => Flows::Result::Err.new('must be an integer')

string_check === 'sdfdsfsd'
# => true

Public Class Methods

new(object, error_message = nil) click to toggle source

@param object [#===] object with case equality @param error_message [String] you may override default error message

# File lib/flows/contract/case_eq.rb, line 25
def initialize(object, error_message = nil)
  @object = object
  @error_message = error_message
end

Public Instance Methods

check!(other) click to toggle source

@see Contract#check!

# File lib/flows/contract/case_eq.rb, line 31
def check!(other)
  unless @object === other
    value_error =
      @error_message ||
      "must match `#{@object.inspect}`, but has class `#{other.class.inspect}` and value `#{other.inspect}`"
    raise Error.new(other, value_error)
  end

  true
end