Class: Rendition::Presenter

Inherits:
Object
  • Object
show all
Defined in:
lib/rendition/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, model, attributes = {}) ⇒ Presenter

Returns a new instance of Presenter.



8
9
10
11
12
13
14
15
16
# File 'lib/rendition/presenter.rb', line 8

def initialize (view_context, model, attributes = {})
  @view_context = view_context
  @model = model

  @parent_presenter = attributes[:parent_presenter] if attributes.has_key? :parent_presenter
  set_attrs(attributes)

  setup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rendition/presenter.rb', line 27

def method_missing(method, *args, &block)
  if model and model.respond_to? method
    val = model.send method, *args, &block

    # if the value is an array then we want to extend that instance of the array to have a presenters method
    if val.is_a? Array and !val.respond_to? :presenters
      parent = self
      val.define_singleton_method :presenters do |type = nil, attributes = {}|
        type ||= :default
        @__presenters ||= {}
        @__presenters.fetch(type) do
          parent.presenters(type, self, attributes)
        end
      end
    end
    val
    #elsif view_context.respond_to? method
    #  view_context.send method, *args, &block
  else
    super
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/rendition/presenter.rb', line 5

def model
  @model
end

#parent_presenterObject (readonly)

Returns the value of attribute parent_presenter.



6
7
8
# File 'lib/rendition/presenter.rb', line 6

def parent_presenter
  @parent_presenter
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



4
5
6
# File 'lib/rendition/presenter.rb', line 4

def view_context
  @view_context
end

Class Method Details

.presenter(view_context, types = nil, model = nil, attributes = {}, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rendition/presenter.rb', line 62

def presenter(view_context, types = nil, model = nil, attributes = {}, &block)

  types, model, attributes = _process_args(types, model, attributes, &block)

  if model
    _find_presenter_class(types, model, attributes).new(view_context, model, attributes)
  else
    nil
  end
end

.presenters(view_context, type = nil, models = nil, attributes = {}, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/rendition/presenter.rb', line 73

def presenters(view_context, type = nil, models = nil, attributes = {}, &block)
  type, models, attributes = _process_args(type, models, attributes, &block)

  return [] if models.empty?

  klass = _find_presenter_class(type, models.first, attributes)
  models.collect do |model|
    klass.new(view_context, model, attributes.clone) if model
  end
end

Instance Method Details

#presenter(type = :default, model = nil, attributes = {}, &block) ⇒ Object



50
51
52
53
# File 'lib/rendition/presenter.rb', line 50

def presenter(type = :default, model = nil, attributes = {}, &block)
  attributes[:parent_presenter] = self
  self.class.presenter(view_context, type, model, attributes, &block)
end

#presenters(type = :default, models = nil, attributes = {}, &block) ⇒ Object



55
56
57
58
# File 'lib/rendition/presenter.rb', line 55

def presenters(type = :default, models = nil, attributes = {}, &block)
  attributes[:parent_presenter] = self
  self.class.presenters(view_context, type, models, attributes, &block)
end

#set_attrs(attributes) ⇒ Object



18
19
20
21
22
# File 'lib/rendition/presenter.rb', line 18

def set_attrs(attributes)
  attributes.each do |name, value|
    self.send "#{name}=", value if self.respond_to? :"#{name}="
  end
end

#setupObject



24
25
# File 'lib/rendition/presenter.rb', line 24

def setup
end