Module: Keynote

Defined in:
lib/keynote.rb,
lib/keynote/cache.rb,
lib/keynote/helper.rb,
lib/keynote/inline.rb,
lib/keynote/rumble.rb,
lib/keynote/railtie.rb,
lib/keynote/version.rb,
lib/keynote/presenter.rb,
lib/keynote/controller.rb

Overview

Keynote is a library for defining and instantiating presenters, objects that encapsulate view logic.

See Also:

Defined Under Namespace

Modules: Cache, Controller, Helper, Inline, Rumble Classes: Presenter

Class Method Summary collapse

Class Method Details

.present(view, *objects) ⇒ Keynote::Presenter .present(view, presenter_name, *objects) ⇒ Keynote::Presenter

Create or retrieve a presenter wrapping zero or more objects. If a block is given, yield the presenter into it.

The first parameter is a Rails view context, but you'll usually access this method through Keynote::Helper#present, Keynote::Controller#present, or Keynote::Presenter#present, all of which handle the view context parameter automatically.

Overloads:

  • .present(view, *objects) ⇒ Keynote::Presenter

    Return a presenter wrapping the given objects. The type of the presenter will be inferred from the type of the first object.

    Examples:

    present(view, MyModel.new)           # => #<MyModelPresenter:0x0001>

    Parameters:

    • view (ActionView::Base)
    • objects (Array)

    Returns:

  • .present(view, presenter_name, *objects) ⇒ Keynote::Presenter

    Return a presenter wrapping the given objects. The type of the presenter is specified in underscore form by the first parameter.

    Examples:

    present(view, :foo_bar, MyModel.new) # => #<FooBarPresenter:0x0002>

    Parameters:

    • view (ActionView::Base)
    • presenter_name (Symbol, String)
    • objects (Array)

    Returns:

See Also:



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/keynote.rb', line 49

def present(view, *objects)
  if objects[0].is_a?(Symbol) || objects[0].is_a?(String)
    name = objects.shift
  else
    name = presenter_name_from_object(objects[0])
  end

  Cache.fetch(name, view, *objects) do
    presenter_from_name(name).new(view, *objects).tap do |presenter|
      yield presenter if block_given?
    end
  end
end