module Cuprum::Rails::Responders::Matching
Implements matching an action result to a response clause.
Attributes
@return [String, Symbol] the name of the action to match.
@return [Cuprum::Matcher] an optional matcher specific to the action.
@return [Cuprum::Rails::Resource] the resource for the controller.
@return [Cuprum::Result] the result of calling the action.
Public Class Methods
@private
# File lib/cuprum/rails/responders/matching.rb, line 50 def self.included(other) super other.extend(ClassMethods) end
@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
Finds and calls the response clause that matches the given result.
-
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.
-
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.
-
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.
-
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
@return [Symbol, nil] the format of the responder.
# File lib/cuprum/rails/responders/matching.rb, line 113 def format nil end
@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
# 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
# File lib/cuprum/rails/responders/matching.rb, line 135 def matcher_options {} end
# File lib/cuprum/rails/responders/matching.rb, line 139 def matchers return class_matchers if matcher.nil? [matcher, *class_matchers] end