Class: Mystique::Presenter

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

Direct Known Subclasses

HashPresenter

Constant Summary collapse

FORMATS =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context) ⇒ Presenter

Returns a new instance of Presenter.



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

def initialize(object, context)
  @__object__  = object
  @__context__ = context || self.class.context || NullContext
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mystique/presenter.rb', line 34

def method_missing(method, *args, &block)
  return target.send(method, *args, &block) if method.to_s.start_with?("to_")

  value = target.send(method, *args, &block)

  case
  when formatted_method?(method)
    format( value )
  when presented_method?(method)
    Mystique.present(value, context: context)
  when presented_collection?(method)
    Mystique.present_collection(value, context: context, &block)
  else
    value
  end
end

Class Method Details

.for(object, context = nil) ⇒ Object



12
13
14
15
16
# File 'lib/mystique/presenter.rb', line 12

def self.for(object, context=nil)
  self.new(object, context).tap do |presenter|
    yield presenter if block_given?
  end
end

Instance Method Details

#contextObject



18
19
20
# File 'lib/mystique/presenter.rb', line 18

def context
  @__context__
end

#ctxObject



21
22
23
# File 'lib/mystique/presenter.rb', line 21

def context
  @__context__
end

#hObject



22
23
24
# File 'lib/mystique/presenter.rb', line 22

def context
  @__context__
end

#inspectObject



28
29
30
# File 'lib/mystique/presenter.rb', line 28

def inspect
  "<#{self.class}(#{target.inspect}) context: #{context.inspect}>"
end

#targetObject



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

def target
  @__object__
end