Class: Substance::Presenter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/substance_presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, view) ⇒ Presenter

Returns a new instance of Presenter.



29
30
31
32
# File 'lib/substance_presenter.rb', line 29

def initialize(object, view)
  super(object)
  @view = view
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



7
8
9
# File 'lib/substance_presenter.rb', line 7

def view
  @view
end

Class Method Details

.decorated?(object) ⇒ Boolean

Returns true if object has been wrapped in a Presenter

Returns:

  • (Boolean)


11
12
13
# File 'lib/substance_presenter.rb', line 11

def decorated?(object)
  object.is_a?(Presenter)
end

.present(object, view) ⇒ Object

Wrap object in presenter with access to a view context



16
17
18
19
# File 'lib/substance_presenter.rb', line 16

def present(object, view)
  return object if decorated?(object)
  new(object, view)
end

.present_collection(collection, view) ⇒ Object

Wrap each object in presenter with access to a view context



22
23
24
25
26
# File 'lib/substance_presenter.rb', line 22

def present_collection(collection, view)
  collection.map do |item|
    present(item, view)
  end
end

Instance Method Details

#objectObject



34
35
36
# File 'lib/substance_presenter.rb', line 34

def object
  undecorated(__getobj__)
end