Module: Rango::ActionArgsMixin

Defined in:
lib/rango/mixins/action_args.rb

Instance Method Summary collapse

Instance Method Details

#invoke_action(action) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rango/mixins/action_args.rb', line 20

def invoke_action(action)
  view       = self.method(action)
  parameters = view.parameters.map! { |type, name| [type, name.to_s] }
  names      = parameters.map { |type, name| name }
  required   = parameters.map { |type, name| name if type.eql?(:req) }.compact

  # validate names
  unless (extra_keys = required - self.params.keys).empty?
    raise ArgumentError, "Following keys aren't available in params: #{extra_keys.inspect}\nAvailable keys: #{self.params.keys.inspect}"
  end

  args = Array.new
  parameters.each do |type, name|
    args.push(self.params[name]) if type.eql?(:req) || (type.eql?(:opt) && !self.params[name].nil?) # this is a bit complex, but we have to do because of rewriting optional args by nil value if we use just map with params[name]
  end
  puts "Rendering #{self.class}##{action} with #{args.map(&:inspect).join(", ")}"
  self.response.body = self.send(action, *args)
end