Class: Presenter::Base

Inherits:
Object
  • Object
show all
Includes:
ActionController::UrlWriter, PresentingMethod
Defined in:
lib/presenter/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PresentingMethod

#presenting

Constructor Details

#initialize(controller, value) ⇒ Base

Creates new presenter using the given controller and item/array.



26
27
28
29
30
31
32
33
# File 'lib/presenter/base.rb', line 26

def initialize(controller, value)
  @controller = controller
  if value.is_a?(Array)
    @array = value
  else
    @item = value
  end
end

Class Attribute Details

.array_nameObject (readonly)

Array name, derived from the class name (e.g. TaskPresenter becomes ‘tasks’). Used for the XML document element name, also name of the accessor method used to read the presented array of objects.



13
14
15
# File 'lib/presenter/base.rb', line 13

def array_name
  @array_name
end

.item_nameObject (readonly)

Item name, derived from the class name (e.g. TaskPresenter becomes ‘task’). Used for the XML document element name, also name of the accessor method used to read the presented object.



8
9
10
# File 'lib/presenter/base.rb', line 8

def item_name
  @item_name
end

Instance Attribute Details

#arrayObject (readonly)

Array being presented. This accessor has a value when presenting an array of objects, for single item see #item. Can also be accessed from named attribute, e.g. the #tasks method on TaskPresenter.



49
50
51
# File 'lib/presenter/base.rb', line 49

def array
  @array
end

#controllerObject (readonly)

Controller associated with this presenter. Used primarily to create URLs and access various helper methods.



39
40
41
# File 'lib/presenter/base.rb', line 39

def controller
  @controller
end

#itemObject (readonly)

Item being presented. This accessor has a value when presenting a single object, for arrays see #array. Can also be accessed from named attribute, e.g. the #task method on TaskPresenter.



44
45
46
# File 'lib/presenter/base.rb', line 44

def item
  @item
end

Class Method Details

.inherited(klass) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/presenter/base.rb', line 15

def inherited(klass)
  name = klass.name[/(.*?)(Presenter)?$/, 1].underscore
  klass.instance_variable_set(:@item_name, name)
  klass.class_eval "def #{klass.item_name} ; @item ; end"
  klass.instance_variable_set(:@array_name, name.pluralize)
  klass.class_eval "def #{klass.array_name} ; @array ; end"
end

Instance Method Details

#to_json(options = {}) ⇒ Object

Converts to JSON document, returns a String. The default implementation uses #map to convert the instance or each member of the array, and calls #to_json on the result.

For example:

render :json=>presenting(@item)


57
58
59
60
# File 'lib/presenter/base.rb', line 57

def to_json(options = {})
  @array ? @array.map { |i| hash_for(i) }.to_json(options) :
           hash_for(@item).to_json(options)
end

#to_xml(options = {}) ⇒ Object

Converts to XML document, returns a String. The default implementation uses #map to convert the instance or each member of the array, and calls #to_xml on the result.

For example:

render :xml=>presenting(@items)


68
69
70
71
# File 'lib/presenter/base.rb', line 68

def to_xml(options = {})
  @array ? @array.map { |i| hash_for(i) }.to_xml({:root=>self.class.array_name}.merge(options)) :
           hash_for(@item).to_xml({:root=>self.class.item_name}.merge(options))
end