Class: Keynote::Presenter
- Inherits:
-
Object
- Object
- Keynote::Presenter
- Includes:
- Rumble
- Defined in:
- lib/keynote/presenter.rb
Overview
Keynote::Presenter is a base class for presenters, objects that encapsulate view logic.
Constant Summary
Constants included from Rumble
Rumble::BASIC, Rumble::COMPLETE, Rumble::SELFCLOSING
Class Attribute Summary collapse
-
.object_names ⇒ Array<Symbol>
List the object names this presenter wraps.
Instance Attribute Summary collapse
- #view ⇒ Object writeonly
Class Method Summary collapse
-
.presents(*objects) ⇒ Object
Define the names and number of the objects presented by this class.
-
.use_html_5_tags ⇒ Object
Define a more complete set of HTML5 tag methods.
Instance Method Summary collapse
-
#initialize(view_context) ⇒ Presenter
constructor
Create a presenter.
- #inspect ⇒ Object
-
#logger ⇒ Object
We have to make a logger method available so that ActionView::Template can safely treat a presenter as a view object.
-
#method_missing(method_name) ⇒ Object
Presenters proxy unknown method calls to the view context, allowing you to call ‘h`, `link_to`, and anything else defined in a helper module.
-
#present ⇒ Object
(also: #k)
Instantiate another presenter.
- #respond_to_missing?(method_name, include_private = true) ⇒ Boolean
Methods included from Rumble
#build_html, define_tags, html_escape, included, #rumble_cleanup, #rumble_context, #text
Constructor Details
#initialize(view_context) ⇒ Presenter
Create a presenter. The default constructor takes one parameter, but calling ‘presents` replaces it with a generated constructor.
69 70 71 |
# File 'lib/keynote/presenter.rb', line 69 def initialize(view_context) @view = view_context end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
Presenters proxy unknown method calls to the view context, allowing you to call ‘h`, `link_to`, and anything else defined in a helper module.
106 107 108 109 110 111 112 |
# File 'lib/keynote/presenter.rb', line 106 def method_missing(method_name, ...) if @view.respond_to?(method_name, true) @view.send(method_name, ...) else super end end |
Class Attribute Details
.object_names ⇒ Array<Symbol>
List the object names this presenter wraps. The default is an empty array; calling ‘presents :foo, :bar` in the presenter’s class body will cause ‘object_names` to return `[:foo, :bar]`.
57 58 59 |
# File 'lib/keynote/presenter.rb', line 57 def object_names @object_names ||= [] end |
Instance Attribute Details
#view=(value) ⇒ Object (writeonly)
63 64 65 |
# File 'lib/keynote/presenter.rb', line 63 def view=(value) @view = value end |
Class Method Details
.presents(*objects) ⇒ Object
Define the names and number of the objects presented by this class. This replaces the default one-parameter constructor with one that takes an extra parameter for each presented object.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/keynote/presenter.rb', line 33 def presents(*objects) self.object_names = objects.dup objects.unshift :view attr_reader(*objects) param_list = objects.join(", ") ivar_list = objects.map { "@#{it}" }.join(", ") class_eval <<-RUBY, __FILE__, __LINE__ + 1 def initialize(#{param_list}) # def initialize(view, foo) #{ivar_list} = #{param_list} # @view, @foo = view, foo end # end RUBY end |
.use_html_5_tags ⇒ Object
Define a more complete set of HTML5 tag methods. The extra tags are listed in Rumble::COMPLETE.
51 |
# File 'lib/keynote/presenter.rb', line 51 def = Rumble.(self) |
Instance Method Details
#inspect ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/keynote/presenter.rb', line 81 def inspect objects = self.class.object_names render = proc { |name| "#{name}: #{send(name).inspect}" } if objects.any? "#<#{self.class} #{objects.map(&render).join(", ")}>" else "#<#{self.class}>" end end |
#logger ⇒ Object
We have to make a logger method available so that ActionView::Template can safely treat a presenter as a view object.
117 |
# File 'lib/keynote/presenter.rb', line 117 def logger = Rails.logger |
#present ⇒ Object Also known as: k
Instantiate another presenter.
75 76 77 |
# File 'lib/keynote/presenter.rb', line 75 def present(...) Keynote.present(@view, ...) end |
#respond_to_missing?(method_name, include_private = true) ⇒ Boolean
93 94 95 |
# File 'lib/keynote/presenter.rb', line 93 def respond_to_missing?(method_name, include_private = true) @view.respond_to?(method_name, true) end |