Class: Oprah::Presenter

Inherits:
SimpleDelegator
  • Object
show all
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.

Parameters:

  • object (Object)

    The object to present

  • view_context (ActionView::Context) (defaults to: self.class.default_view_context)

    View context to assign

Since:

  • 0.0.1



100
101
102
103
# File 'lib/oprah/presenter.rb', line 100

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

Instance Attribute Details

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

Returns The view context.

Returns:

  • (ActionView::Base)

    The view context

Since:

  • 0.0.1



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

def view_context
  @view_context
end

Class Method Details

.cacheActiveSupport::Cache::MemoryStore

Returns the shared presenter cache object.

Returns:

  • (ActiveSupport::Cache::MemoryStore)

Since:

  • 0.0.1



15
16
17
# File 'lib/oprah/presenter.rb', line 15

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.

Returns:

  • (ActionView::Context)

Since:

  • 0.0.1



78
79
80
# File 'lib/oprah/presenter.rb', line 78

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.

Parameters:

  • object (Object)

    The object to present

  • view_context (ActionView::Context) (defaults to: default_view_context)

    View context to assign

  • only (Class) (defaults to: nil)

    Class or Array of presenters to use

Returns:

Since:

  • 0.0.1



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

def present(object, view_context: default_view_context, only: nil)
  presenters = presenter_classes_for(object)
  presenters &= Array(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.

Parameters:

  • objects (Enumerable)

    The objects to present

See Also:

Since:

  • 0.0.1



40
41
42
# File 'lib/oprah/presenter.rb', line 40

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

.presents_many(association) ⇒ Symbol

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.

Parameters:

  • association (Symbol)

    Name of the association

Returns:

  • (Symbol)

    Name of the association

Since:

  • 0.0.1



66
67
68
69
70
71
72
# File 'lib/oprah/presenter.rb', line 66

def presents_many(association)
  define_method association do
    present_many(__getobj__.__send__(association))
  end

  association
end

.presents_one(association) ⇒ Symbol

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.

Parameters:

  • association (Symbol)

    Name of the association

Returns:

  • (Symbol)

    Name of the association

Since:

  • 0.0.1



51
52
53
54
55
56
57
# File 'lib/oprah/presenter.rb', line 51

def presents_one(association)
  define_method association do
    present(__getobj__.__send__(association))
  end

  association
end

Instance Method Details

#instance_of?(klass) ⇒ Boolean

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

Parameters:

  • klass (Class)

Returns:

  • (Boolean)

    result

Since:

  • 0.0.1



145
146
147
# File 'lib/oprah/presenter.rb', line 145

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

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

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

Parameters:

  • other (Module)

Returns:

  • (Boolean)

    result

Since:

  • 0.0.1



134
135
136
# File 'lib/oprah/presenter.rb', line 134

def kind_of?(other)
  super || __getobj__.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



111
112
113
114
# File 'lib/oprah/presenter.rb', line 111

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



122
123
124
125
# File 'lib/oprah/presenter.rb', line 122

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