Module: Presenter::PresentingMethod

Included in:
Base, Spec::Rails::Matchers
Defined in:
lib/presenter/method.rb

Instance Method Summary collapse

Instance Method Details

#presenting(*args) ⇒ Object

Return a new presenter. This method can be called in several configurations to present both an instance and an array:

You can call this method in several configurations to present both singular objects and arrays. Using an explicit type followed by value:

presenting(class, object_or_array)

The first argument specifies the expected type of the object/array that follows, and that argument is used to find the presenter class. For example, presenting(Foo,[])<tt> uses <tt>FooPresenter.

presenting(symbol, object_or_array)

This method is similar but replaces explicit class with symbol, so foo:<tt> instead of <tt>Foo.

presenting(object)

The third form takes a single argument and finds the presenter class based on the argument type, for example, presenting(Foo.new) will use the presenter class FooPresenter.

presenting(array)

Since arrays may be empty it is not always possible to use the array content to figure out the presenter class. Either specify it explicitly as the first argument (see above), or let the presenting method figure it out from the controller name, for example, inferring FooPresenter from FooController.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/presenter/method.rb', line 25

def presenting(*args)
  controller = ActionController::Base === self ? self : self.controller
  case args.first
  when Class    # Presented class followed by instance/array
    name = args.shift.to_s
    value = args.shift
  when Symbol   # Presenter name (symbol) followed by instance/array
    name = args.shift.to_s.capitalize
    value = args.shift
  when Array    # Array of values, pick presenter from item type
    value = args.shift
    name = controller.class.to_s.gsub(/Controller$/, '').classify
  else          # Presenter for single instance.
    value = args.shift
    name = value.class.name
  end
  raise ArgumentError, "Unexpected arguments #{args.inspect}" unless args.empty?
  Class.const_get("#{name}Presenter").new(controller, value)
end