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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rumble

#build_html, #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:



71
72
73
# File 'lib/keynote/presenter.rb', line 71

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, *args, &block) ⇒ 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


108
109
110
111
112
113
114
# File 'lib/keynote/presenter.rb', line 108

def method_missing(method_name, *args, &block)
  if @view.respond_to?(method_name, true)
    @view.send(method_name, *args, &block)
  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>)


59
60
61
# File 'lib/keynote/presenter.rb', line 59

def object_names
  @object_names ||= []
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 { |o| "@#{o}" }.join(', ')

  class_eval <<-RUBY
    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
52
53
# File 'lib/keynote/presenter.rb', line 51

def use_html_5_tags
  Rumble.use_html_5_tags(self)
end

Instance Method Details

#present(*objects, &blk) ⇒ Object Also known as: k

Instantiate another presenter.

See Also:



77
78
79
# File 'lib/keynote/presenter.rb', line 77

def present(*objects, &blk)
  Keynote.present(@view, *objects, &blk)
end