Method: Cuprum::Matching::ClassMethods#match

Defined in:
lib/cuprum/matching.rb

#match(status, error: nil, value: nil) {|result| ... } ⇒ Object

Defines a match clause for the matcher.

Parameters:

  • status (Symbol)

    The status to match. The clause will match a result only if the result has the same status as the match clause.

  • error (Class) (defaults to: nil)

    The type of error to match. If given, the clause will match a result only if the result error is an instance of the given class, or an instance of a subclass.

  • value (Class) (defaults to: nil)

    The type of value to match. If given, the clause will match a result only if the result value is an instance of the given class, or an instance of a subclass.

Yields:

  • The code to execute on a successful match.

Yield Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cuprum/matching.rb', line 27

def match(status, error: nil, value: nil, &block)
  validate_status!(status)
  validate_error!(error)
  validate_value!(value)

  clause  = MatchClause.new(block, error, status, value)
  clauses = match_clauses[status]
  index   = clauses.bsearch_index { |item| clause <= item } || -1

  # Clauses are sorted from most specific to least specific.
  clauses.insert(index, clause)
end