Class: Cuprum::Rails::Responders::BaseResponder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cuprum/rails/responders/base_responder.rb

Overview

Abstract base class for defining responders.

Direct Known Subclasses

HtmlResponder, JsonResponder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action_name:, controller:, request:, member_action: false, **_options) ⇒ BaseResponder

Returns a new instance of BaseResponder.

Parameters:

  • action_name (String, Symbol)

    the name of the action to match.

  • controller (Cuprum::Rails::Controller)

    the called controller.

  • 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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cuprum/rails/responders/base_responder.rb', line 15

def initialize(
  action_name:,
  controller:,
  request:,
  member_action: false,
  **_options
)
  @action_name     = action_name
  @controller      = controller
  @controller_name = controller.class.name
  @member_action   = !!member_action # rubocop:disable Style/DoubleNegation
  @request         = request
  @resource        = controller.class.resource
end

Instance Attribute Details

#action_nameString, Symbol (readonly)

Returns the name of the action to match.

Returns:

  • (String, Symbol)

    the name of the action to match.



31
32
33
# File 'lib/cuprum/rails/responders/base_responder.rb', line 31

def action_name
  @action_name
end

#controllerCuprum::Rails::Controller (readonly)

Returns the called controller.

Returns:



34
35
36
# File 'lib/cuprum/rails/responders/base_responder.rb', line 34

def controller
  @controller
end

#controller_nameString (readonly)

Returns the name of the called controller.

Returns:

  • (String)

    the name of the called controller.



37
38
39
# File 'lib/cuprum/rails/responders/base_responder.rb', line 37

def controller_name
  @controller_name
end

#requestCuprum::Rails::Request (readonly)

Returns the request to the controller.

Returns:



40
41
42
# File 'lib/cuprum/rails/responders/base_responder.rb', line 40

def request
  @request
end

#resourceCuprum::Rails::Resource (readonly)

Returns the resource for the controller.

Returns:



43
44
45
# File 'lib/cuprum/rails/responders/base_responder.rb', line 43

def resource
  @resource
end

#resultCuprum::Result (readonly)

Returns the result of calling the action.

Returns:

  • (Cuprum::Result)

    the result of calling the action.



46
47
48
# File 'lib/cuprum/rails/responders/base_responder.rb', line 46

def result
  @result
end

Instance Method Details

#call(result) ⇒ #call

Generates the response object for the result.

Parameters:

  • result (Cuprum::Result)

    the result of the action call.

Returns:

  • (#call)

    the response object from the matching response clause.



53
54
55
# File 'lib/cuprum/rails/responders/base_responder.rb', line 53

def call(result)
  @result = result
end

#member_action?true, false

Returns true if the action is a member action, otherwise false.

Returns:

  • (true, false)

    true if the action is a member action, otherwise false.



59
60
61
# File 'lib/cuprum/rails/responders/base_responder.rb', line 59

def member_action?
  @member_action
end

#routesCuprum::Rails::Routes

Helper for accessing the configured routes for the resource.

Any wildcards from the path params will be applied to the routes.

Returns:



68
69
70
# File 'lib/cuprum/rails/responders/base_responder.rb', line 68

def routes
  resource.routes.with_wildcards(routes_wildcards)
end