Class: Oprah::Presenter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/oprah/presenter.rb

Overview

Since:

  • 0.0.1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, view_context: self.class.default_view_context) ⇒ Presenter

Initializes a new Presenter.

Since:

  • 0.0.1


98
99
100
101
# File 'lib/oprah/presenter.rb', line 98

def initialize(object, view_context: self.class.default_view_context)
  @object = object
  @view_context = view_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegates all method calls not handled by the presenter to `object`.

Since:

  • 0.0.1


104
105
106
107
108
109
110
# File 'lib/oprah/presenter.rb', line 104

def method_missing(meth, *args, &block)
  if respond_to?(meth)
    object.__send__(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#objectObject (readonly)

Returns The presented object

Since:

  • 0.0.1


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

def object
  @object
end

#view_contextActionView::Base (readonly) Also known as: h

Returns The view context

Since:

  • 0.0.1


9
10
11
# File 'lib/oprah/presenter.rb', line 9

def view_context
  @view_context
end

Class Method Details

.cacheCache

Returns the shared presenter cache object.

Since:

  • 0.0.1


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

def cache
  @@cache
end

.default_view_contextActionView::Context

Returns the default view context to use if no view context is explicitly passed to the presenter.

Since:

  • 0.0.1


89
90
91
# File 'lib/oprah/presenter.rb', line 89

def default_view_context
  ActionController::Base.new.view_context
end

.present(object, view_context: default_view_context, only: nil) ⇒ Presenter

Presents the given `object` with all it's matching presenters, following it's ancestors in reverse.

Since:

  • 0.0.1


39
40
41
42
43
44
45
46
# File 'lib/oprah/presenter.rb', line 39

def present(object, view_context: default_view_context, only: nil)
  presenters = @@cache.lookup(object)
  presenters = presenters & (only.kind_of?(Array) ? only : [only]) if only

  presenters.inject(object) do |memo, presenter|
    presenter.new(memo, view_context: view_context)
  end
end

.present_many(objects, **kwargs) ⇒ Object

Presents the given `objects` with all their matching presenters. The behaviour and parameters are identical to `.present`'s.

See Also:

Since:

  • 0.0.1


53
54
55
# File 'lib/oprah/presenter.rb', line 53

def present_many(objects, **kwargs)
  objects.map { |object| present(object, **kwargs) }
end

.presents_many(association) ⇒ Boolean

Automatically wrap the objects returned by the given one-to-many or many-to-many `association` method in presenters.

Presenters will re-use the parent's assigned view context.

Since:

  • 0.0.1


77
78
79
80
81
82
83
# File 'lib/oprah/presenter.rb', line 77

def presents_many(association)
  define_method association do
    present_many(object.__send__(association), view_context: view_context)
  end

  true
end

.presents_one(association) ⇒ Boolean

Automatically wrap the objects returned by the given one-to-one `association` method in presenters.

Presenters will re-use the parent's assigned view context.

Since:

  • 0.0.1


64
65
66
67
68
# File 'lib/oprah/presenter.rb', line 64

def presents_one(association)
  define_method association do
    present(object.__send__(association), view_context: view_context)
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Returns `true` if `object` or the presenter tests positive for equality.

Since:

  • 0.0.1


169
170
171
# File 'lib/oprah/presenter.rb', line 169

def ==(other)
  super || object == other
end

#inspectString

See Also:

  • Object#inspect

22
# File 'lib/oprah/presenter.rb', line 22

def_delegators :@object, :inspect, :to_s

#instance_of?(klass) ⇒ Boolean

Returns `true` if `object` or the presenter is an instance of the given `class`.

Since:

  • 0.0.1


161
162
163
# File 'lib/oprah/presenter.rb', line 161

def instance_of?(klass)
  super || object.instance_of?(klass)
end

#kind_of?(other) ⇒ Boolean Also known as: is_a?

Returns true if `klass` is the class of `object` or the presenter, or if `#class` is one of the superclasses of `object`, the presenter or modules included in `object` or the presenter.

Since:

  • 0.0.1


150
151
152
# File 'lib/oprah/presenter.rb', line 150

def kind_of?(other)
  super || object.kind_of?(other)
end

#present(*args, **kwargs, &block) ⇒ Object

Presents a single object.

Will re-use the presenter's assigned view context if no `view_context`: parameter is given.

See Also:

Since:

  • 0.0.1


118
119
120
121
# File 'lib/oprah/presenter.rb', line 118

def present(*args, **kwargs, &block)
  kwargs = { view_context: view_context }.merge(kwargs)
  self.class.present(*args, **kwargs, &block)
end

#present_many(*args, **kwargs, &block) ⇒ Object

Presents a collection of objects.

Will re-use the presenter's assigned view context if no `view_context`: parameter is given.

See Also:

Since:

  • 0.0.1


129
130
131
132
# File 'lib/oprah/presenter.rb', line 129

def present_many(*args, **kwargs, &block)
  kwargs = { view_context: view_context }.merge(kwargs)
  self.class.present_many(*args, **kwargs, &block)
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns true if either `object` or `self` responds to the given method name.

Since:

  • 0.0.1


140
141
142
# File 'lib/oprah/presenter.rb', line 140

def respond_to?(method, include_private = false)
  super || object.respond_to?(method, include_private)
end

#to_sString

See Also:

  • Object#to_s

22
# File 'lib/oprah/presenter.rb', line 22

def_delegators :@object, :inspect, :to_s