module Adorn::Helper

Public Instance Methods

presenting(object, klass=nil, options={}) { |const.new(object, self, options)| ... } click to toggle source

A helper method to present objects with an implicit or explicilty set Adorn::Decorator Accepts single or multiple objects and Decorators for presentation.

ex: Single Object, dynamic presenter loading presenting @bar do |bar_presenter|

bar_presenter will respond to methods from the BarPresenter

end

ex: Single Object, explicit Decorator Class

presenting @bar, BazPresenter do |bar_presenter|

bar_presenter will respond to methods from the BazPresenter

end

ex: Multiple Objects NOTE: implicit detection only

presenting([@foo, @bar]) do |foo_presenter, bar_presenter|

@foo will respond to methods from the FooPresenter
@bar will respond to methods from the BarPresenter

end

@param [Object, Class, Hash] @return [Adorn::Decorator]

# File lib/adorn/helpers.rb, line 33
def presenting(object, klass=nil, options={})
  if object.is_a?(Array)
    object.each do |presentable|

      proxy = "#{presentable.class}Presenter"
      yield Object.const_get(proxy.to_s).new(object, self, options) if Object.const_defined?(proxy.to_s)

      provided_klass = Object.const_get(proxy.to_s, false)
      presenter      = provided_klass.new(presentable, self, options) # self is context
      yield presenter
      proxy = nil
    end
    nil
  else
    proxy = klass || "#{object.class}Presenter"
    yield Object.const_get(proxy.to_s).new(object, self, options) if Object.const_defined?(proxy.to_s)

    provided_klass = Object.const_get(proxy.to_s, false)
    presenter = provided_klass.new(object, self, options) # self is context
    yield presenter
    proxy = nil
  end
end