Class: Flows::Contract::CaseEq

Inherits:
Flows::Contract show all
Defined in:
lib/flows/contract/case_eq.rb

Overview

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

Examples:

String contract

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

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

string_check === 'sdfdsfsd'
# => true

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

Since:

  • 0.4.0

Instance Method Summary collapse

Methods inherited from Flows::Contract

#===, #check, make, #to_proc, #transform, #transform!

Constructor Details

#initialize(object, error_message = nil) ⇒ CaseEq

Returns a new instance of CaseEq.

Parameters:

  • object (#===)

    object with case equality

  • error_message (String) (defaults to: nil)

    you may override default error message

Since:

  • 0.4.0



25
26
27
28
# File 'lib/flows/contract/case_eq.rb', line 25

def initialize(object, error_message = nil)
  @object = object
  @error_message = error_message
end

Instance Method Details

#check!(other) ⇒ Object

See Also:

Since:

  • 0.4.0



31
32
33
34
35
36
37
38
39
40
# 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