Module: Card::Format::MethodDelegation

Included in:
Card::Format
Defined in:
lib/card/format/method_delegation.rb

Overview

missing methods may be render calls or action view methods these methods sort that out

Constant Summary collapse

RENDER_METHOD_RE =
/^
   (?<underscore>_)?  # leading underscore to skip permission check
   render
   (?:_(?<view>\w+))? # view name
   (?<bang>!)?        # trailing bang to skip optional check
$/x

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *opts, &proc) ⇒ Object (private)

TODO: make it so we fall back to super if action_view can’t handle method. It’s not as easy as ‘elsif api_render? method`, because respond_to gives false for many methods action view can actually handle, like `h`



41
42
43
44
45
46
47
# File 'lib/card/format/method_delegation.rb', line 41

def method_missing method, *opts, &proc
  if (match = api_render? method)
    api_render match, opts
  else
    delegate_to_action_view method, opts, &proc
  end
end

Instance Method Details

#action_viewObject



20
21
22
# File 'lib/card/format/method_delegation.rb', line 20

def action_view
  @action_view ||= root? ? new_action_view : root.action_view
end

#api_render(match, opts) ⇒ Object



14
15
16
17
18
# File 'lib/card/format/method_delegation.rb', line 14

def api_render match, opts
  # view can be part of method name or first argument
  view = match[:view] || opts.shift
  render! view, render_args(match[:underscore], match[:bang], opts)
end