Module: Riveter::Presenter::Enumerator

Extended by:
ActiveSupport::Concern
Included in:
Enumerator
Defined in:
lib/riveter/presenter.rb

Instance Method Summary collapse

Instance Method Details

#with_presenter(klass, &block) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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