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

Included in:
HtmlResponder, JsonResponder
Defined in:
lib/cuprum/rails/responders/matching.rb

Overview

Implements matching an action result to a response clause.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#matcherCuprum::Matcher (readonly)

Returns an optional matcher specific to the action.

Returns:

  • (Cuprum::Matcher)

    an optional matcher specific to the action.



86
87
88
# File 'lib/cuprum/rails/responders/matching.rb', line 86

def matcher
  @matcher
end

Instance Method Details

#call(result) ⇒ #call

Finds and calls the response clause that matches the given result.

  1. Checks for an exact match (the result status, value, and error all match) in the given matcher (if any), then the responder class, then each ancestor of the responder class in ascending order.

  2. If a match is not found, checks for a partial match (the result status, and either the value or the error match) in the same order.

  3. If there is still no match found, checks for a generic match (the result status matches, and the match clause does not specify either an error or a value.

  4. If there is no matching response clause, raises an exception.

Parameters:

  • result (Cuprum::Result)

    the result of the action call.

Returns:

  • (#call)

    the response object from the matching response clause.

Raises:

  • (Cuprum::Matching::NoMatchError)

    if none of the response clauses match the result.



106
107
108
109
110
111
112
# File 'lib/cuprum/rails/responders/matching.rb', line 106

def call(result)
  @result = result

  Cuprum::MatcherList
    .new(matchers.map { |matcher| matcher.with_context(self) })
    .call(result)
end

#formatSymbol?

Returns the format of the responder.

Returns:

  • (Symbol, nil)

    the format of the responder.



115
116
117
# File 'lib/cuprum/rails/responders/matching.rb', line 115

def format
  nil
end

#initialize(action_name:, controller:, request:, matcher: nil, member_action: false, **options) ⇒ Object

Parameters:

  • action_name (String, Symbol)

    the name of the action to match.

  • controller (Cuprum::Rails::Controller)

    the called controller.

  • matcher (Cuprum::Matcher) (defaults to: nil)

    an optional matcher specific to the action. This will be matched before any of the generic matchers.

  • member_action (Boolean) (defaults to: false)

    true if the action acts on a collection item, not on the collection as a whole.

  • request (Cuprum::Rails::Request)

    the request to the controller.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cuprum/rails/responders/matching.rb', line 66

def initialize( # rubocop:disable Metrics/ParameterLists
  action_name:,
  controller:,
  request:,
  matcher:       nil,
  member_action: false,
  **options
)
  super(
    action_name:   action_name,
    controller:    controller,
    member_action: member_action,
    request:       request,
    **options
  )

  @matcher = matcher
end

#member_action?Boolean

Returns true if the action acts on a collection item, not on the collection as a whole.

Returns:

  • (Boolean)

    true if the action acts on a collection item, not on the collection as a whole.



121
122
123
# File 'lib/cuprum/rails/responders/matching.rb', line 121

def member_action?
  @member_action
end