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
-
#matcher ⇒ Cuprum::Matcher
readonly
An optional matcher specific to the action.
Instance Method Summary collapse
-
#call(result) ⇒ #call
Finds and calls the response clause that matches the given result.
-
#format ⇒ Symbol?
The format of the responder.
- #initialize(action_name:, controller:, request:, matcher: nil, member_action: false, **options) ⇒ Object
-
#member_action? ⇒ Boolean
True if the action acts on a collection item, not on the collection as a whole.
Instance Attribute Details
#matcher ⇒ Cuprum::Matcher (readonly)
Returns 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.
-
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.
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 |
#format ⇒ Symbol?
Returns 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
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, ** ) super( action_name: action_name, controller: controller, member_action: member_action, request: request, ** ) @matcher = matcher end |
#member_action? ⇒ Boolean
Returns 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 |