module Cuprum::Rails::Responders::Matching

Implements matching an action result to a response clause.

Attributes

action_name[R]

@return [String, Symbol] the name of the action to match.

matcher[R]

@return [Cuprum::Matcher] an optional matcher specific to the action.

resource[R]

@return [Cuprum::Rails::Resource] the resource for the controller.

result[R]

@return [Cuprum::Result] the result of calling the action.

Public Class Methods

included(other) click to toggle source

@private

Calls superclass method
# File lib/cuprum/rails/responders/matching.rb, line 50
def self.included(other)
  super

  other.extend(ClassMethods)
end
new( action_name:, resource:, matcher: nil, member_action: false, **_options ) click to toggle source

@param action_name [String, Symbol] The name of the action to match. @param matcher [Cuprum::Matcher] An optional matcher specific to the

action. This will be matched before any of the generic matchers.

@param member_action [Boolean] True if the action acts on a collection

item, not on the collection as a whole.

@param resource [Cuprum::Rails::Resource] The resource for the controller.

# File lib/cuprum/rails/responders/matching.rb, line 62
def initialize(
  action_name:,
  resource:,
  matcher: nil,
  member_action: false,
  **_options
)
  @action_name   = action_name
  @matcher       = matcher
  @member_action = !!member_action # rubocop:disable Style/DoubleNegation
  @resource      = resource
end

Public Instance Methods

call(result) click to toggle source

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.

@return [#call, renderer] The response object from the matching response

clause.

@raise [Cuprum::Matching::NoMatchError] if none of the response clauses

match the result.
# File lib/cuprum/rails/responders/matching.rb, line 104
def call(result)
  @result = result

  Cuprum::MatcherList
    .new(matchers.map { |matcher| matcher.with_context(self) })
    .call(result)
end
format() click to toggle source

@return [Symbol, nil] the format of the responder.

# File lib/cuprum/rails/responders/matching.rb, line 113
def format
  nil
end
member_action?() click to toggle source

@return [Boolean] true if the action acts on a collection item, not on the

collection as a whole.
# File lib/cuprum/rails/responders/matching.rb, line 119
def member_action?
  @member_action
end

Private Instance Methods

class_matchers() click to toggle source
# File lib/cuprum/rails/responders/matching.rb, line 125
def class_matchers
  options = matcher_options

  singleton_class
    .ancestors
    .select { |ancestor| ancestor < Cuprum::Rails::Responders::Matching }
    .map { |ancestor| ancestor.matchers(**options) }
    .flatten
end
matcher_options() click to toggle source
# File lib/cuprum/rails/responders/matching.rb, line 135
def matcher_options
  {}
end
matchers() click to toggle source
# File lib/cuprum/rails/responders/matching.rb, line 139
def matchers
  return class_matchers if matcher.nil?

  [matcher, *class_matchers]
end