Module: ActionController::Expose

Defined in:
lib/action_controller/expose.rb,
lib/action_controller/expose/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#expose(*names, &block) ⇒ Object

Declares helper-accessible reader methods on the controller.

expose :foo, :bar           # attr_reader + helper_method (controller assigns @foo / @bar)
expose :foo, -> { ... }     # memoized: first call runs the lambda, later calls return the cached result
expose(:foo) { ... }        # same, with a block

The memoizing form uses instance_variable_defined? so nil/false cache.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/action_controller/expose.rb', line 15

def expose(*names, &block)
  if block
    define_exposed(names.first, &block)
  elsif names.length == 2 && names.last.respond_to?(:call)
    define_exposed(names.first, &names.last)
  else
    names.each do |name|
      attr_reader name
      helper_method name
    end
  end
end