/
view.rb
293 lines (278 loc) · 7.27 KB
/
view.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
require 'set'
require 'pathname'
require 'lotus/utils/class_attribute'
require 'lotus/view/version'
require 'lotus/view/configuration'
require 'lotus/view/inheritable'
require 'lotus/view/rendering'
require 'lotus/view/escape'
require 'lotus/view/dsl'
require 'lotus/layout'
require 'lotus/presenter'
module Lotus
# View
#
# @since 0.1.0
module View
# Missing template error
#
# This is raised at the runtime when Lotus::View cannot find a template for
# the requested format.
#
# We can't raise this error during the loading phase, because at that time
# we don't know if a view implements its own rendering policy.
# A view is allowed to override `#render`, and this scenario can make the
# presence of a template useless. One typical example is the usage of a
# serializer that returns the output string, without rendering a template.
#
# @since 0.1.0
class MissingTemplateError < ::StandardError
def initialize(template, format)
super("Can't find template '#{ template }' for '#{ format }' format.")
end
end
# Missing format error
#
# This is raised at the runtime when rendering context lacks of the :format
# key.
#
# @since 0.1.0
#
# @see Lotus::View::Rendering#render
class MissingFormatError < ::StandardError
end
include Utils::ClassAttribute
# Framework configuration
#
# @since 0.2.0
# @api private
class_attribute :configuration
self.configuration = Configuration.new
# Configure the framework.
# It yields the given block in the context of the configuration
#
# @param blk [Proc] the configuration block
#
# @since 0.2.0
#
# @see Lotus::View::Configuration
#
# @example
# require 'lotus/view'
#
# Lotus::View.configure do
# root '/path/to/root'
# end
def self.configure(&blk)
configuration.instance_eval(&blk)
end
# Duplicate Lotus::View in order to create a new separated instance
# of the framework.
#
# The new instance of the framework will be completely decoupled from the
# original. It will inherit the configuration, but all the changes that
# happen after the duplication, won't be reflected on the other copies.
#
# @return [Module] a copy of Lotus::View
#
# @since 0.2.0
# @api private
#
# @example Basic usage
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.dupe
# end
#
# MyApp::View == Lotus::View # => false
#
# MyApp::View.configuration ==
# Lotus::View.configuration # => false
#
# @example Inheriting configuration
# require 'lotus/view'
#
# Lotus::View.configure do
# root '/path/to/root'
# end
#
# module MyApp
# View = Lotus::View.dupe
# end
#
# module MyApi
# View = Lotus::View.dupe
# View.configure do
# root '/another/root'
# end
# end
#
# Lotus::View.configuration.root # => #<Pathname:/path/to/root>
# MyApp::View.configuration.root # => #<Pathname:/path/to/root>
# MyApi::View.configuration.root # => #<Pathname:/another/root>
def self.dupe
dup.tap do |duplicated|
duplicated.configuration = configuration.duplicate
end
end
# Duplicate the framework and generate modules for the target application
#
# @param mod [Module] the Ruby namespace of the application
# @param views [String] the optional namespace where the application's
# views will live
# @param blk [Proc] an optional block to configure the framework
#
# @return [Module] a copy of Lotus::View
#
# @since 0.2.0
#
# @see Lotus::View#dupe
# @see Lotus::View::Configuration
# @see Lotus::View::Configuration#namespace
#
# @example Basic usage
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self)
# end
#
# # It will:
# #
# # 1. Generate MyApp::View
# # 2. Generate MyApp::Layout
# # 3. Generate MyApp::Presenter
# # 4. Generate MyApp::Views
# # 5. Configure MyApp::Views as the default namespace for views
#
# module MyApp::Views::Dashboard
# class Index
# include MyApp::View
# end
# end
#
# @example Compare code
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self) do
# # ...
# end
# end
#
# # it's equivalent to:
#
# module MyApp
# View = Lotus::View.dupe
# Layout = Lotus::Layout.dup
#
# module Views
# end
#
# View.configure do
# namespace 'MyApp::Views'
# end
#
# View.configure do
# # ...
# end
# end
#
# @example Custom views module
# require 'lotus/view
#
# module MyApp
# View = Lotus::View.duplicate(self, 'Vs')
# end
#
# defined?(MyApp::Views) # => nil
# defined?(MyApp::Vs) # => "constant"
#
# # Developers can namespace views under Vs
# module MyApp::Vs::Dashboard
# # ...
# end
#
# @example Nil views module
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self, nil)
# end
#
# defined?(MyApp::Views) # => nil
#
# # Developers can namespace views under MyApp
# module MyApp
# # ...
# end
#
# @example Block usage
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self) do
# root '/path/to/root'
# end
# end
#
# Lotus::View.configuration.root # => #<Pathname:.>
# MyApp::View.configuration.root # => #<Pathname:/path/to/root>
def self.duplicate(mod, views = 'Views', &blk)
dupe.tap do |duplicated|
mod.module_eval %{ module #{ views }; end } if views
mod.module_eval %{
Layout = Lotus::Layout.dup
Presenter = Lotus::Presenter.dup
}
duplicated.configure do
namespace [mod, views].compact.join '::'
end
duplicated.configure(&blk) if block_given?
end
end
# Override Ruby's hook for modules.
# It includes basic Lotus::View modules to the given Class.
# It sets a copy of the framework configuration
#
# @param base [Class] the target view
#
# @since 0.1.0
# @api private
#
# @see http://www.ruby-doc.org/core-2.1.2/Module.html#method-i-included
#
# @see Lotus::View::Dsl
# @see Lotus::View::Inheritable
# @see Lotus::View::Rendering
#
# @example
# require 'lotus/view'
#
# class IndexView
# include Lotus::View
# end
def self.included(base)
conf = self.configuration
conf.add_view(base)
base.class_eval do
extend Inheritable
extend Dsl
extend Rendering
extend Escape.dup
include Utils::ClassAttribute
class_attribute :configuration
self.configuration = conf.duplicate
end
conf.copy!(base)
end
# Load the framework
#
# @since 0.1.0
# @api private
def self.load!
configuration.load!
end
end
end