/
keynote.rb
73 lines (67 loc) · 2.29 KB
/
keynote.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# encoding: UTF-8
require "keynote/version"
require "keynote/rumble"
require "keynote/inline"
require "keynote/presenter"
require "keynote/controller"
require "keynote/helper"
require "keynote/railtie"
require "keynote/cache"
# Keynote is a library for defining and instantiating presenters,
# objects that encapsulate view logic.
#
# @see file:README.md
module Keynote
class << self
# Create or retrieve a presenter wrapping zero or more objects.
# If a block is given, yield the presenter into it.
#
# The first parameter is a Rails view context, but you'll usually access
# this method through `Keynote::Helper#present`,
# `Keynote::Controller#present`, or `Keynote::Presenter#present`, all of
# which handle the view context parameter automatically.
#
# @see Keynote::Helper#present
# @see Keynote::Controller#present
# @see Keynote::Presenter#present
#
# @overload present(view, *objects)
# Return a presenter wrapping the given objects. The type of the
# presenter will be inferred from the type of the first object.
# @example
# present(view, MyModel.new) # => #<MyModelPresenter:0x0001>
# @param [ActionView::Base] view
# @param [Array] objects
# @return [Keynote::Presenter]
#
# @overload present(view, presenter_name, *objects)
# Return a presenter wrapping the given objects. The type of the
# presenter is specified in underscore form by the first parameter.
# @example
# present(view, :foo_bar, MyModel.new) # => #<FooBarPresenter:0x0002>
# @param [ActionView::Base] view
# @param [Symbol, String] presenter_name
# @param [Array] objects
# @return [Keynote::Presenter]
#
def present(view, *objects)
if objects[0].is_a?(Symbol) || objects[0].is_a?(String)
name = objects.shift
else
name = presenter_name_from_object(objects[0])
end
Cache.fetch(name, view, *objects) do
presenter_from_name(name).new(view, *objects).tap do |presenter|
yield presenter if block_given?
end
end
end
private
def presenter_name_from_object(object)
object.class.to_s.underscore
end
def presenter_from_name(name)
"#{name.to_s.camelize}Presenter".constantize
end
end
end