Class: Oprah::Presenter
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Oprah::Presenter
- Defined in:
- lib/oprah/presenter.rb
Overview
Instance Attribute Summary collapse
-
#view_context ⇒ ActionView::Base
(also: #h)
readonly
The view context.
Class Method Summary collapse
-
.cache ⇒ ActiveSupport::Cache::MemoryStore
Returns the shared presenter cache object.
-
.default_view_context ⇒ ActionView::Context
Returns the default view context to use if no view context is explicitly passed to the presenter.
-
.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.
-
.present_many(objects, **kwargs) ⇒ Object
Presents the given ‘objects` with all their matching presenters.
-
.presents_many(association) ⇒ Symbol
Automatically wrap the objects returned by the given one-to-many or many-to-many ‘association` method in presenters.
-
.presents_one(association) ⇒ Symbol
Automatically wrap the objects returned by the given one-to-one ‘association` method in presenters.
Instance Method Summary collapse
-
#initialize(object, view_context: self.class.default_view_context) ⇒ Presenter
constructor
Initializes a new Presenter.
-
#instance_of?(klass) ⇒ Boolean
Returns ‘true` if the presented object or the presenter is an instance of the given `class`.
-
#kind_of?(other) ⇒ Boolean
(also: #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.
-
#present(*args, **kwargs, &block) ⇒ Object
Presents a single object.
-
#present_many(*args, **kwargs, &block) ⇒ Object
Presents a collection of objects.
Constructor Details
#initialize(object, view_context: self.class.default_view_context) ⇒ Presenter
Initializes a new Presenter.
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_context ⇒ ActionView::Base (readonly) Also known as: h
Returns The view context.
4 5 6 |
# File 'lib/oprah/presenter.rb', line 4 def view_context @view_context end |
Class Method Details
.cache ⇒ ActiveSupport::Cache::MemoryStore
Returns the shared presenter cache object.
15 16 17 |
# File 'lib/oprah/presenter.rb', line 15 def cache @@cache end |
.default_view_context ⇒ ActionView::Context
Returns the default view context to use if no view context is explicitly passed to the presenter.
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.
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.
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.
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.
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`.
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.
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.
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.
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 |