module Riveter::Presenter::Enumerator

Public Instance Methods

with_presenter(klass) { |klass(item, (is_a_view ? view : nil))| ... } click to toggle source
# File lib/riveter/presenter.rb, line 16
def with_presenter(klass, &block)
  raise ArgumentError, 'klass argument cannot be nil' if klass.nil?
  raise ArgumentError, "#{klass} is not a valid presenter class" unless klass <= Presenter::Base
  return to_enum(__method__, klass) unless block_given?

  ##
  # some magic!
  # assuming that this method is used within the view
  # the block which is supplied will be in the scope of the view
  # therefore, getting it's binding and evaluating self will
  # get us the view object
  ##
  view = block.binding.eval('self')
  is_a_view = view.is_a?(ActionView::Base)

  self.each do |item|
    yield klass.new(item, (is_a_view ? view : nil))
  end
end