Class: Keynote::Presenter

Inherits:
Object
  • Object
show all
Includes:
Rumble
Defined in:
lib/keynote/presenter.rb

Overview

Keynote::Presenter is a base class for presenters, objects that encapsulate view logic.

See Also:

Constant Summary

Constants included from Rumble

Rumble::BASIC, Rumble::COMPLETE, Rumble::SELFCLOSING

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • view_context (ActionView::Base)

See Also:



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.

Examples:

def title_link
  link_to blog_post_url(blog_post) do
    "#{h author.name} — #{h blog_post.title}".html_safe
  end
end


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_namesArray<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]`.

Returns:

  • (Array<Symbol>)


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.

Examples:

class PostPresenter
  presents :blog_post, :author
end

# In a view
presenter = k(:post, @some_post, @some_user)
presenter.blog_post # == @some_post
presenter.author    # == @some_user

Parameters:

  • objects (Array<Symbol>)

    One symbol for each of the models that will be required to instantiate this presenter. Each symbol will be used as the accessor and instance variable name for its associated parameter, but an object of any class can be given for any parameter.



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_tagsObject

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 use_html_5_tags = Rumble.use_html_5_tags(self)

Instance Method Details

#inspectObject



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

#loggerObject

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

#presentObject Also known as: k

Instantiate another presenter.

See Also:



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

Returns:

  • (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