Module: Cuprum::Rails::Responders::Matching::ClassMethods

Defined in:
lib/cuprum/rails/responders/matching.rb

Overview

Provides a DSL for generating response clauses for matching results.

Instance Method Summary collapse

Instance Method Details

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

Creates a match clause that maps a result to a response.

Parameters:

  • status (Symbol)

    The status of the result, either :success or :failure.

  • error (Class) (defaults to: nil)

    The class of the result error. If given, the clause will only match results with an error that is an instance of this class or a subclass.

  • value (Class) (defaults to: nil)

    The class of the result value. If given, the clause will only match results with a value that is an instance of this class or a subclass.

Yields:

  • The clause implementation. This block will be called in the context of the matcher.

Yield Returns:

  • (#call, #renderer)

    the response for the action.



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

def match(status, error: nil, value: nil, &block)
  matcher = @matcher || Cuprum::Matcher.new

  matcher.singleton_class.match(
    status, error: error, value: value, &block
  )

  @matcher = matcher
end

#matchers(**_keywords) ⇒ Object



38
39
40
41
42
# File 'lib/cuprum/rails/responders/matching.rb', line 38

def matchers(**_keywords)
  return [] unless @matcher

  [@matcher]
end