Class: WithAction::ActionResponder

Inherits:
Object
  • Object
show all
Defined in:
lib/with_action/with_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ ActionResponder

Returns a new instance of ActionResponder.



10
11
12
13
# File 'lib/with_action/with_action.rb', line 10

def initialize(controller)
  @controller = controller
  @order, @responses = [], {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(action, &block) ⇒ Object



33
34
35
36
37
# File 'lib/with_action/with_action.rb', line 33

def method_missing(action, &block)
  @order << action
  block ||= lambda { @controller.send(action) }
  @responses[action] = block
end

Instance Method Details

#any(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/with_action/with_action.rb', line 23

def any(&block)
  method_missing(:any) do
    # reset
    @order, @responses = [], {}
    block.call.tap do
      respond
    end
  end
end

#respondObject



15
16
17
18
19
20
21
# File 'lib/with_action/with_action.rb', line 15

def respond
  action = @order.detect do |a|
    !@controller.params[a].blank? || !@controller.params["#{a}.x"].blank?
  end
  action ||= @order.include?(:any) ? :any : @order.first
  @responses[action].call if @responses[action]
end