/
configuration.rb
497 lines (475 loc) · 14 KB
/
configuration.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
require 'set'
require 'lotus/utils/class'
require 'lotus/utils/kernel'
require 'lotus/utils/string'
require 'lotus/utils/load_paths'
require 'lotus/view/rendering/layout_finder'
require 'lotus/view/rendering/partial_templates_finder'
module Lotus
module View
# Configuration for the framework, controllers and actions.
#
# Lotus::Controller has its own global configuration that can be manipulated
# via `Lotus::View.configure`.
#
# Every time that `Lotus::View` and `Lotus::Layout` are included, that
# global configuration is being copied to the recipient. The copy will
# inherit all the settings from the original, but all the subsequent changes
# aren't reflected from the parent to the children, and viceversa.
#
# This architecture allows to have a global configuration that capture the
# most common cases for an application, and let views and layouts
# layouts to specify exceptions.
#
# @since 0.2.0
class Configuration
# Default root
#
# @since 0.2.0
# @api private
DEFAULT_ROOT = '.'.freeze
# Default encoding
#
# @since 0.5.0
# @api private
DEFAULT_ENCODING = Encoding::UTF_8
attr_reader :load_paths
attr_reader :views
attr_reader :layouts
attr_reader :modules
attr_reader :partials
# Return the original configuration of the framework instance associated
# with the given class.
#
# When multiple instances of Lotus::View are used in the same application,
# we want to make sure that a controller or an action will receive the
# expected configuration.
#
# @param base [Class] a view or a layout
#
# @return [Lotus::Controller::Configuration] the configuration associated
# to the given class.
#
# @since 0.2.0
# @api private
#
# @example Direct usage of the framework
# require 'lotus/view'
#
# class Show
# include Lotus::View
# end
#
# Lotus::View::Configuration.for(Show)
# # => will return from Lotus::View
#
# @example Multiple instances of the framework
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self)
#
# module Views::Dashboard
# class Index
# include MyApp::View
# end
# end
# end
#
# class Show
# include Lotus::Action
# end
#
# Lotus::View::Configuration.for(Show)
# # => will return from Lotus::View
#
# Lotus::View::Configuration.for(MyApp::Views::Dashboard::Index)
# # => will return from MyApp::View
def self.for(base)
# TODO this implementation is similar to Lotus::Controller::Configuration consider to extract it into Lotus::Utils
namespace = Utils::String.new(base).namespace
framework = Utils::Class.load_from_pattern!("(#{namespace}|Lotus)::View")
framework.configuration
end
# Initialize a configuration instance
#
# @return [Lotus::View::Configuration] a new configuration's instance
#
# @since 0.2.0
def initialize
@namespace = Object
reset!
end
# Set the Ruby namespace where to lookup for views.
#
# When multiple instances of the framework are used, we want to make sure
# that if a `MyApp` wants a `Dashboard::Index` view, we are loading the
# right one.
#
# If not set, this value defaults to `Object`.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload namespace(value)
# Sets the given value
# @param value [Class, Module, String] a valid Ruby namespace identifier
#
# @overload namespace
# Gets the value
# @return [Class, Module, String]
#
# @since 0.2.0
#
# @example Getting the value
# require 'lotus/view'
#
# Lotus::View.configuration.namespace # => Object
#
# @example Setting the value
# require 'lotus/view'
#
# Lotus::View.configure do
# namespace 'MyApp::Views'
# end
def namespace(value = nil)
if value
@namespace = value
else
@namespace
end
end
# Set the root path where to search for templates
#
# If not set, this value defaults to the current directory.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload root(value)
# Sets the given value
# @param value [String,Pathname,#to_pathname] an object that can be
# coerced to Pathname
# @raise [Errno::ENOENT] if the given path doesn't exist
#
# @overload root
# Gets the value
# @return [Pathname]
#
# @since 0.2.0
#
# @see Lotus::View::Dsl#root
# @see http://www.ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
# @see http://rdoc.info/gems/lotus-utils/Lotus/Utils/Kernel#Pathname-class_method
#
# @example Getting the value
# require 'lotus/view'
#
# Lotus::View.configuration.root # => #<Pathname:.>
#
# @example Setting the value
# require 'lotus/view'
#
# Lotus::View.configure do
# root '/path/to/templates'
# end
#
# Lotus::View.configuration.root # => #<Pathname:/path/to/templates>
def root(value = nil)
if value
@root = Utils::Kernel.Pathname(value).realpath
else
@root
end
end
# Set the global layout
#
# If not set, this value defaults to `nil`, while at the rendering time
# it will use `Lotus::View::Rendering::NullLayout`.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload layout(value)
# Sets the given value
# @param value [Symbol] the name of the layout
#
# @overload layout
# Gets the value
# @return [Class]
#
# @since 0.2.0
#
# @see Lotus::View::Dsl#layout
#
# @example Getting the value
# require 'lotus/view'
#
# Lotus::View.configuration.layout # => nil
#
# @example Setting the value
# require 'lotus/view'
#
# Lotus::View.configure do
# layout :application
# end
#
# Lotus::View.configuration.layout # => ApplicationLayout
#
# @example Setting the value in a namespaced app
# require 'lotus/view'
#
# module MyApp
# View = Lotus::View.duplicate(self) do
# layout :application
# end
# end
#
# MyApp::View.configuration.layout # => MyApp::ApplicationLayout
def layout(value = nil)
if value.nil?
Rendering::LayoutFinder.find(@layout, @namespace)
else
@layout = value
end
end
# Default encoding for templates
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
#
# @overload default_encoding(value)
# Sets the given value
# @param value [String,Encoding] a string representation of the encoding,
# or an Encoding constant
#
# @raise [ArgumentError] if the given value isn't a supported encoding
#
# @overload default_encoding
# Gets the value
# @return [Encoding]
#
# @since 0.5.0
#
# @example Set UTF-8 As A String
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding 'utf-8'
# end
#
# @example Set UTF-8 As An Encoding Constant
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding Encoding::UTF_8
# end
#
# @example Raise An Error For Unknown Encoding
# require 'lotus/view'
#
# Lotus::View.configure do
# default_encoding 'foo'
# end
#
# # => ArgumentError
def default_encoding(value = nil)
if value.nil?
@default_encoding
else
@default_encoding = Encoding.find(value)
end
end
# Prepare the views.
#
# The given block will be yielded when `Lotus::View` will be included by
# a view.
#
# This method can be called multiple times.
#
# @param blk [Proc] the code block
#
# @return [void]
#
# @raise [ArgumentError] if called without passing a block
#
# @since 0.3.0
#
# @see Lotus::View.configure
# @see Lotus::View.duplicate
#
# @example Including shared utilities
# require 'lotus/view'
#
# module UrlHelpers
# def comments_path
# '/'
# end
# end
#
# Lotus::View.configure do
# prepare do
# include UrlHelpers
# end
# end
#
# Lotus::View.load!
#
# module Comments
# class New
# # The following include will cause UrlHelpers to be included too.
# # This makes `comments_path` available in the view context
# include Lotus::View
#
# def form
# %(<form action="#{ comments_path }" method="POST"></form>)
# end
# end
# end
#
# @example Preparing multiple times
# require 'lotus/view'
#
# Lotus::View.configure do
# prepare do
# include UrlHelpers
# end
#
# prepare do
# format :json
# end
# end
#
# Lotus::View.configure do
# prepare do
# include FormattingHelpers
# end
# end
#
# Lotus::View.load!
#
# module Articles
# class Index
# # The following include will cause the inclusion of:
# # * UrlHelpers
# # * FormattingHelpers
# #
# # It also sets the view to render only JSON
# include Lotus::View
# end
# end
def prepare(&blk)
if block_given?
@modules.push(blk)
else
raise ArgumentError.new('Please provide a block')
end
end
# Add a view to the registry
#
# @since 0.2.0
# @api private
def add_view(view)
@views.add(view)
end
# Add a layout to the registry
#
# @since 0.2.0
# @api private
def add_layout(layout)
@layouts.add(layout)
end
# Duplicate by copying the settings in a new instance.
#
# @return [Lotus::View::Configuration] a copy of the configuration
#
# @since 0.2.0
# @api private
def duplicate
Configuration.new.tap do |c|
c.namespace = namespace
c.root = root
c.layout = @layout # lazy loading of the class
c.default_encoding = default_encoding
c.load_paths = load_paths.dup
c.modules = modules.dup
end
end
# Load the configuration for the current framework
#
# @since 0.2.0
# @api private
def load!
views.each { |v| v.__send__(:load!) }
layouts.each { |l| l.__send__(:load!) }
load_partials!
freeze
end
# Load partials for each partial template file found under the
# given load paths
#
# @since x.x.x
# @api private
def load_partials!
Lotus::View::Rendering::PartialTemplatesFinder.new(self).find.each do |partial|
add_partial(partial)
end
end
# Load partials for each partial template file found under the
# given load paths
#
# @since x.x.x
# @api private
def find_partial(relative_partial_path, template_name, format)
partials_for_view = partials.has_key?(relative_partial_path) ? partials[relative_partial_path] : partials[template_name]
partials_for_view ? partials_for_view[format.to_sym] : nil
end
# Add a partial to the registry
#
# @since x.x.x
# @api private
def add_partial(partial)
@partials[partial.key][partial.format.to_sym] = partial.template
end
# Reset all the values to the defaults
#
# @since 0.2.0
# @api private
def reset!
root DEFAULT_ROOT
default_encoding DEFAULT_ENCODING
@partials = Hash.new { |h, k| h[k] = Hash.new }
@views = Set.new
@layouts = Set.new
@load_paths = Utils::LoadPaths.new(root)
@layout = nil
@modules = []
end
# Copy the configuration for the given action
#
# @param base [Class] the target action
#
# @return void
#
# @since 0.3.0
# @api private
def copy!(base)
modules.each do |mod|
base.class_eval(&mod)
end
end
alias_method :unload!, :reset!
protected
attr_writer :namespace
attr_writer :root
attr_writer :load_paths
attr_writer :layout
attr_writer :default_encoding
attr_writer :modules
end
end
end