hanami-view 1.3.1 → 2.0.0.alpha3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/LICENSE +20 -0
- data/README.md +17 -835
- data/hanami-view.gemspec +26 -16
- data/lib/hanami/view/application_configuration.rb +77 -0
- data/lib/hanami/view/application_context.rb +35 -0
- data/lib/hanami/view/application_view.rb +89 -0
- data/lib/hanami/view/context.rb +97 -0
- data/lib/hanami/view/context_helpers/content_helpers.rb +26 -0
- data/lib/hanami/view/decorated_attributes.rb +82 -0
- data/lib/hanami/view/errors.rb +31 -53
- data/lib/hanami/view/exposure.rb +126 -0
- data/lib/hanami/view/exposures.rb +74 -0
- data/lib/hanami/view/part.rb +217 -0
- data/lib/hanami/view/part_builder.rb +140 -0
- data/lib/hanami/view/path.rb +68 -0
- data/lib/hanami/view/render_environment.rb +62 -0
- data/lib/hanami/view/render_environment_missing.rb +44 -0
- data/lib/hanami/view/rendered.rb +55 -0
- data/lib/hanami/view/renderer.rb +79 -0
- data/lib/hanami/view/scope.rb +189 -0
- data/lib/hanami/view/scope_builder.rb +98 -0
- data/lib/hanami/view/standalone_view.rb +400 -0
- data/lib/hanami/view/tilt/erb.rb +26 -0
- data/lib/hanami/view/tilt/erbse.rb +21 -0
- data/lib/hanami/view/tilt/haml.rb +26 -0
- data/lib/hanami/view/tilt.rb +78 -0
- data/lib/hanami/view/version.rb +5 -5
- data/lib/hanami/view.rb +208 -223
- data/lib/hanami-view.rb +3 -1
- metadata +120 -70
- data/LICENSE.md +0 -22
- data/lib/hanami/layout.rb +0 -190
- data/lib/hanami/presenter.rb +0 -98
- data/lib/hanami/view/configuration.rb +0 -504
- data/lib/hanami/view/dsl.rb +0 -347
- data/lib/hanami/view/escape.rb +0 -225
- data/lib/hanami/view/inheritable.rb +0 -54
- data/lib/hanami/view/rendering/layout_finder.rb +0 -128
- data/lib/hanami/view/rendering/layout_registry.rb +0 -69
- data/lib/hanami/view/rendering/layout_scope.rb +0 -274
- data/lib/hanami/view/rendering/null_layout.rb +0 -52
- data/lib/hanami/view/rendering/null_local.rb +0 -82
- data/lib/hanami/view/rendering/null_template.rb +0 -83
- data/lib/hanami/view/rendering/null_view.rb +0 -26
- data/lib/hanami/view/rendering/options.rb +0 -24
- data/lib/hanami/view/rendering/partial.rb +0 -31
- data/lib/hanami/view/rendering/partial_file.rb +0 -29
- data/lib/hanami/view/rendering/partial_finder.rb +0 -75
- data/lib/hanami/view/rendering/partial_templates_finder.rb +0 -73
- data/lib/hanami/view/rendering/registry.rb +0 -134
- data/lib/hanami/view/rendering/scope.rb +0 -108
- data/lib/hanami/view/rendering/subscope.rb +0 -56
- data/lib/hanami/view/rendering/template.rb +0 -69
- data/lib/hanami/view/rendering/template_finder.rb +0 -55
- data/lib/hanami/view/rendering/template_name.rb +0 -50
- data/lib/hanami/view/rendering/templates_finder.rb +0 -144
- data/lib/hanami/view/rendering/view_finder.rb +0 -37
- data/lib/hanami/view/rendering.rb +0 -294
- data/lib/hanami/view/template.rb +0 -57
@@ -1,504 +0,0 @@
|
|
1
|
-
require 'set'
|
2
|
-
require 'hanami/utils/class'
|
3
|
-
require 'hanami/utils/kernel'
|
4
|
-
require 'hanami/utils/string'
|
5
|
-
require 'hanami/utils/load_paths'
|
6
|
-
require 'hanami/view/rendering/layout_finder'
|
7
|
-
require 'hanami/view/rendering/partial_templates_finder'
|
8
|
-
|
9
|
-
module Hanami
|
10
|
-
module View
|
11
|
-
# Configuration for the framework, controllers and actions.
|
12
|
-
#
|
13
|
-
# Hanami::Controller has its own global configuration that can be manipulated
|
14
|
-
# via `Hanami::View.configure`.
|
15
|
-
#
|
16
|
-
# Every time that `Hanami::View` and `Hanami::Layout` are included, that
|
17
|
-
# global configuration is being copied to the recipient. The copy will
|
18
|
-
# inherit all the settings from the original, but all the subsequent changes
|
19
|
-
# aren't reflected from the parent to the children, and viceversa.
|
20
|
-
#
|
21
|
-
# This architecture allows to have a global configuration that capture the
|
22
|
-
# most common cases for an application, and let views and layouts
|
23
|
-
# layouts to specify exceptions.
|
24
|
-
#
|
25
|
-
# @since 0.2.0
|
26
|
-
class Configuration
|
27
|
-
# Default root
|
28
|
-
#
|
29
|
-
# @since 0.2.0
|
30
|
-
# @api private
|
31
|
-
DEFAULT_ROOT = '.'.freeze
|
32
|
-
|
33
|
-
# Default encoding
|
34
|
-
#
|
35
|
-
# @since 0.5.0
|
36
|
-
# @api private
|
37
|
-
DEFAULT_ENCODING = Encoding::UTF_8
|
38
|
-
|
39
|
-
attr_reader :load_paths
|
40
|
-
attr_reader :views
|
41
|
-
attr_reader :layouts
|
42
|
-
attr_reader :modules
|
43
|
-
attr_reader :partials
|
44
|
-
|
45
|
-
# Return the original configuration of the framework instance associated
|
46
|
-
# with the given class.
|
47
|
-
#
|
48
|
-
# When multiple instances of Hanami::View are used in the same application,
|
49
|
-
# we want to make sure that a controller or an action will receive the
|
50
|
-
# expected configuration.
|
51
|
-
#
|
52
|
-
# @param base [Class] a view or a layout
|
53
|
-
#
|
54
|
-
# @return [Hanami::Controller::Configuration] the configuration associated
|
55
|
-
# to the given class.
|
56
|
-
#
|
57
|
-
# @since 0.2.0
|
58
|
-
# @api private
|
59
|
-
#
|
60
|
-
# @example Direct usage of the framework
|
61
|
-
# require 'hanami/view'
|
62
|
-
#
|
63
|
-
# class Show
|
64
|
-
# include Hanami::View
|
65
|
-
# end
|
66
|
-
#
|
67
|
-
# Hanami::View::Configuration.for(Show)
|
68
|
-
# # => will return from Hanami::View
|
69
|
-
#
|
70
|
-
# @example Multiple instances of the framework
|
71
|
-
# require 'hanami/view'
|
72
|
-
#
|
73
|
-
# module MyApp
|
74
|
-
# View = Hanami::View.duplicate(self)
|
75
|
-
#
|
76
|
-
# module Views::Dashboard
|
77
|
-
# class Index
|
78
|
-
# include MyApp::View
|
79
|
-
# end
|
80
|
-
# end
|
81
|
-
# end
|
82
|
-
#
|
83
|
-
# class Show
|
84
|
-
# include Hanami::Action
|
85
|
-
# end
|
86
|
-
#
|
87
|
-
# Hanami::View::Configuration.for(Show)
|
88
|
-
# # => will return from Hanami::View
|
89
|
-
#
|
90
|
-
# Hanami::View::Configuration.for(MyApp::Views::Dashboard::Index)
|
91
|
-
# # => will return from MyApp::View
|
92
|
-
def self.for(base)
|
93
|
-
# TODO this implementation is similar to Hanami::Controller::Configuration consider to extract it into Hanami::Utils
|
94
|
-
namespace = Utils::String.namespace(base)
|
95
|
-
framework = Utils::Class.load("#{namespace}::View") || Utils::Class.load!('Hanami::View')
|
96
|
-
framework.configuration
|
97
|
-
end
|
98
|
-
|
99
|
-
# Initialize a configuration instance
|
100
|
-
#
|
101
|
-
# @return [Hanami::View::Configuration] a new configuration's instance
|
102
|
-
#
|
103
|
-
# @since 0.2.0
|
104
|
-
def initialize
|
105
|
-
@namespace = Object
|
106
|
-
reset!
|
107
|
-
end
|
108
|
-
|
109
|
-
# Set the Ruby namespace where to lookup for views.
|
110
|
-
#
|
111
|
-
# When multiple instances of the framework are used, we want to make sure
|
112
|
-
# that if a `MyApp` wants a `Dashboard::Index` view, we are loading the
|
113
|
-
# right one.
|
114
|
-
#
|
115
|
-
# If not set, this value defaults to `Object`.
|
116
|
-
#
|
117
|
-
# This is part of a DSL, for this reason when this method is called with
|
118
|
-
# an argument, it will set the corresponding instance variable. When
|
119
|
-
# called without, it will return the already set value, or the default.
|
120
|
-
#
|
121
|
-
# @overload namespace(value)
|
122
|
-
# Sets the given value
|
123
|
-
# @param value [Class, Module, String] a valid Ruby namespace identifier
|
124
|
-
#
|
125
|
-
# @overload namespace
|
126
|
-
# Gets the value
|
127
|
-
# @return [Class, Module, String]
|
128
|
-
#
|
129
|
-
# @since 0.2.0
|
130
|
-
#
|
131
|
-
# @example Getting the value
|
132
|
-
# require 'hanami/view'
|
133
|
-
#
|
134
|
-
# Hanami::View.configuration.namespace # => Object
|
135
|
-
#
|
136
|
-
# @example Setting the value
|
137
|
-
# require 'hanami/view'
|
138
|
-
#
|
139
|
-
# Hanami::View.configure do
|
140
|
-
# namespace 'MyApp::Views'
|
141
|
-
# end
|
142
|
-
def namespace(value = nil)
|
143
|
-
if value
|
144
|
-
@namespace = value
|
145
|
-
else
|
146
|
-
@namespace
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
# Set the root path where to search for templates
|
151
|
-
#
|
152
|
-
# If not set, this value defaults to the current directory.
|
153
|
-
#
|
154
|
-
# This is part of a DSL, for this reason when this method is called with
|
155
|
-
# an argument, it will set the corresponding instance variable. When
|
156
|
-
# called without, it will return the already set value, or the default.
|
157
|
-
#
|
158
|
-
# @overload root(value)
|
159
|
-
# Sets the given value
|
160
|
-
# @param value [String,Pathname,#to_pathname] an object that can be
|
161
|
-
# coerced to Pathname
|
162
|
-
# @raise [Errno::ENOENT] if the given path doesn't exist
|
163
|
-
#
|
164
|
-
# @overload root
|
165
|
-
# Gets the value
|
166
|
-
# @return [Pathname]
|
167
|
-
#
|
168
|
-
# @since 0.2.0
|
169
|
-
#
|
170
|
-
# @see Hanami::View::Dsl#root
|
171
|
-
# @see http://www.ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
|
172
|
-
# @see http://rdoc.info/gems/hanami-utils/Hanami/Utils/Kernel#Pathname-class_method
|
173
|
-
#
|
174
|
-
# @example Getting the value
|
175
|
-
# require 'hanami/view'
|
176
|
-
#
|
177
|
-
# Hanami::View.configuration.root # => #<Pathname:.>
|
178
|
-
#
|
179
|
-
# @example Setting the value
|
180
|
-
# require 'hanami/view'
|
181
|
-
#
|
182
|
-
# Hanami::View.configure do
|
183
|
-
# root '/path/to/templates'
|
184
|
-
# end
|
185
|
-
#
|
186
|
-
# Hanami::View.configuration.root # => #<Pathname:/path/to/templates>
|
187
|
-
def root(value = nil)
|
188
|
-
if value
|
189
|
-
@root = Utils::Kernel.Pathname(value).realpath
|
190
|
-
else
|
191
|
-
@root
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
# Set the global layout
|
196
|
-
#
|
197
|
-
# If not set, this value defaults to `nil`, while at the rendering time
|
198
|
-
# it will use `Hanami::View::Rendering::NullLayout`.
|
199
|
-
#
|
200
|
-
# This is part of a DSL, for this reason when this method is called with
|
201
|
-
# an argument, it will set the corresponding instance variable. When
|
202
|
-
# called without, it will return the already set value, or the default.
|
203
|
-
#
|
204
|
-
# @overload layout(value)
|
205
|
-
# Sets the given value
|
206
|
-
# @param value [Symbol] the name of the layout
|
207
|
-
#
|
208
|
-
# @overload layout
|
209
|
-
# Gets the value
|
210
|
-
# @return [Class]
|
211
|
-
#
|
212
|
-
# @since 0.2.0
|
213
|
-
#
|
214
|
-
# @see Hanami::View::Dsl#layout
|
215
|
-
#
|
216
|
-
# @example Getting the value
|
217
|
-
# require 'hanami/view'
|
218
|
-
#
|
219
|
-
# Hanami::View.configuration.layout # => nil
|
220
|
-
#
|
221
|
-
# @example Setting the value
|
222
|
-
# require 'hanami/view'
|
223
|
-
#
|
224
|
-
# Hanami::View.configure do
|
225
|
-
# layout :application
|
226
|
-
# end
|
227
|
-
#
|
228
|
-
# Hanami::View.configuration.layout # => ApplicationLayout
|
229
|
-
#
|
230
|
-
# @example Setting the value in a namespaced app
|
231
|
-
# require 'hanami/view'
|
232
|
-
#
|
233
|
-
# module MyApp
|
234
|
-
# View = Hanami::View.duplicate(self) do
|
235
|
-
# layout :application
|
236
|
-
# end
|
237
|
-
# end
|
238
|
-
#
|
239
|
-
# MyApp::View.configuration.layout # => MyApp::ApplicationLayout
|
240
|
-
def layout(value = nil)
|
241
|
-
if value.nil?
|
242
|
-
Rendering::LayoutFinder.find(@layout, @namespace)
|
243
|
-
else
|
244
|
-
@layout = value
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
# Default encoding for templates
|
249
|
-
#
|
250
|
-
# This is part of a DSL, for this reason when this method is called with
|
251
|
-
# an argument, it will set the corresponding instance variable. When
|
252
|
-
# called without, it will return the already set value, or the default.
|
253
|
-
#
|
254
|
-
# @overload default_encoding(value)
|
255
|
-
# Sets the given value
|
256
|
-
# @param value [String,Encoding] a string representation of the encoding,
|
257
|
-
# or an Encoding constant
|
258
|
-
#
|
259
|
-
# @raise [ArgumentError] if the given value isn't a supported encoding
|
260
|
-
#
|
261
|
-
# @overload default_encoding
|
262
|
-
# Gets the value
|
263
|
-
# @return [Encoding]
|
264
|
-
#
|
265
|
-
# @since 0.5.0
|
266
|
-
#
|
267
|
-
# @example Set UTF-8 As A String
|
268
|
-
# require 'hanami/view'
|
269
|
-
#
|
270
|
-
# Hanami::View.configure do
|
271
|
-
# default_encoding 'utf-8'
|
272
|
-
# end
|
273
|
-
#
|
274
|
-
# @example Set UTF-8 As An Encoding Constant
|
275
|
-
# require 'hanami/view'
|
276
|
-
#
|
277
|
-
# Hanami::View.configure do
|
278
|
-
# default_encoding Encoding::UTF_8
|
279
|
-
# end
|
280
|
-
#
|
281
|
-
# @example Raise An Error For Unknown Encoding
|
282
|
-
# require 'hanami/view'
|
283
|
-
#
|
284
|
-
# Hanami::View.configure do
|
285
|
-
# default_encoding 'foo'
|
286
|
-
# end
|
287
|
-
#
|
288
|
-
# # => ArgumentError
|
289
|
-
def default_encoding(value = nil)
|
290
|
-
if value.nil?
|
291
|
-
@default_encoding
|
292
|
-
else
|
293
|
-
@default_encoding = Encoding.find(value)
|
294
|
-
end
|
295
|
-
end
|
296
|
-
|
297
|
-
# Prepare the views.
|
298
|
-
#
|
299
|
-
# The given block will be yielded when `Hanami::View` will be included by
|
300
|
-
# a view.
|
301
|
-
#
|
302
|
-
# This method can be called multiple times.
|
303
|
-
#
|
304
|
-
# @param blk [Proc] the code block
|
305
|
-
#
|
306
|
-
# @return [void]
|
307
|
-
#
|
308
|
-
# @raise [ArgumentError] if called without passing a block
|
309
|
-
#
|
310
|
-
# @since 0.3.0
|
311
|
-
#
|
312
|
-
# @see Hanami::View.configure
|
313
|
-
# @see Hanami::View.duplicate
|
314
|
-
#
|
315
|
-
# @example Including shared utilities
|
316
|
-
# require 'hanami/view'
|
317
|
-
#
|
318
|
-
# module UrlHelpers
|
319
|
-
# def comments_path
|
320
|
-
# '/'
|
321
|
-
# end
|
322
|
-
# end
|
323
|
-
#
|
324
|
-
# Hanami::View.configure do
|
325
|
-
# prepare do
|
326
|
-
# include UrlHelpers
|
327
|
-
# end
|
328
|
-
# end
|
329
|
-
#
|
330
|
-
# Hanami::View.load!
|
331
|
-
#
|
332
|
-
# module Comments
|
333
|
-
# class New
|
334
|
-
# # The following include will cause UrlHelpers to be included too.
|
335
|
-
# # This makes `comments_path` available in the view context
|
336
|
-
# include Hanami::View
|
337
|
-
#
|
338
|
-
# def form
|
339
|
-
# %(<form action="#{ comments_path }" method="POST"></form>)
|
340
|
-
# end
|
341
|
-
# end
|
342
|
-
# end
|
343
|
-
#
|
344
|
-
# @example Preparing multiple times
|
345
|
-
# require 'hanami/view'
|
346
|
-
#
|
347
|
-
# Hanami::View.configure do
|
348
|
-
# prepare do
|
349
|
-
# include UrlHelpers
|
350
|
-
# end
|
351
|
-
#
|
352
|
-
# prepare do
|
353
|
-
# format :json
|
354
|
-
# end
|
355
|
-
# end
|
356
|
-
#
|
357
|
-
# Hanami::View.configure do
|
358
|
-
# prepare do
|
359
|
-
# include FormattingHelpers
|
360
|
-
# end
|
361
|
-
# end
|
362
|
-
#
|
363
|
-
# Hanami::View.load!
|
364
|
-
#
|
365
|
-
# module Articles
|
366
|
-
# class Index
|
367
|
-
# # The following include will cause the inclusion of:
|
368
|
-
# # * UrlHelpers
|
369
|
-
# # * FormattingHelpers
|
370
|
-
# #
|
371
|
-
# # It also sets the view to render only JSON
|
372
|
-
# include Hanami::View
|
373
|
-
# end
|
374
|
-
# end
|
375
|
-
def prepare(&blk)
|
376
|
-
if block_given?
|
377
|
-
@modules.push(blk)
|
378
|
-
else
|
379
|
-
raise ArgumentError.new('Please provide a block')
|
380
|
-
end
|
381
|
-
end
|
382
|
-
|
383
|
-
# Add a view to the registry
|
384
|
-
#
|
385
|
-
# @since 0.2.0
|
386
|
-
# @api private
|
387
|
-
def add_view(view)
|
388
|
-
@views.add(view)
|
389
|
-
end
|
390
|
-
|
391
|
-
# Add a layout to the registry
|
392
|
-
#
|
393
|
-
# @since 0.2.0
|
394
|
-
# @api private
|
395
|
-
def add_layout(layout)
|
396
|
-
@layouts.add(layout)
|
397
|
-
end
|
398
|
-
|
399
|
-
# Duplicate by copying the settings in a new instance.
|
400
|
-
#
|
401
|
-
# @return [Hanami::View::Configuration] a copy of the configuration
|
402
|
-
#
|
403
|
-
# @since 0.2.0
|
404
|
-
# @api private
|
405
|
-
def duplicate
|
406
|
-
Configuration.new.tap do |c|
|
407
|
-
c.namespace = namespace
|
408
|
-
c.root = root
|
409
|
-
c.layout = @layout # lazy loading of the class
|
410
|
-
c.default_encoding = default_encoding
|
411
|
-
c.load_paths = load_paths.dup
|
412
|
-
c.modules = modules.dup
|
413
|
-
end
|
414
|
-
end
|
415
|
-
|
416
|
-
# Load the configuration for the current framework
|
417
|
-
#
|
418
|
-
# @since 0.2.0
|
419
|
-
# @api private
|
420
|
-
def load!
|
421
|
-
views.each { |v| v.__send__(:load!) }
|
422
|
-
layouts.each { |l| l.__send__(:load!) }
|
423
|
-
load_partials!
|
424
|
-
freeze
|
425
|
-
end
|
426
|
-
|
427
|
-
# Load partials for each partial template file found under the
|
428
|
-
# given load paths
|
429
|
-
#
|
430
|
-
# @since 0.7.0
|
431
|
-
# @api private
|
432
|
-
def load_partials!
|
433
|
-
Hanami::View::Rendering::PartialTemplatesFinder.new(self).find.each do |partial|
|
434
|
-
add_partial(partial)
|
435
|
-
end
|
436
|
-
end
|
437
|
-
|
438
|
-
# Load partials for each partial template file found under the
|
439
|
-
# given load paths
|
440
|
-
#
|
441
|
-
# @since 0.7.0
|
442
|
-
# @api private
|
443
|
-
def find_partial(relative_partial_path, template_name, format)
|
444
|
-
partials_for_view = partials.has_key?(relative_partial_path) ? partials[relative_partial_path] : partials[template_name]
|
445
|
-
partials_for_view ? partials_for_view[format.to_sym] : nil
|
446
|
-
end
|
447
|
-
|
448
|
-
# Add a partial to the registry
|
449
|
-
#
|
450
|
-
# @since 0.7.0
|
451
|
-
# @api private
|
452
|
-
def add_partial(partial)
|
453
|
-
@partials[partial.key][partial.format.to_sym] = partial.template
|
454
|
-
end
|
455
|
-
|
456
|
-
# Reset all the values to the defaults
|
457
|
-
#
|
458
|
-
# @since 0.2.0
|
459
|
-
# @api private
|
460
|
-
def reset!
|
461
|
-
root DEFAULT_ROOT
|
462
|
-
default_encoding DEFAULT_ENCODING
|
463
|
-
|
464
|
-
@partials = Hash.new { |h, k| h[k] = Hash.new }
|
465
|
-
@views = Set.new
|
466
|
-
@layouts = Set.new
|
467
|
-
@load_paths = Utils::LoadPaths.new(root)
|
468
|
-
@layout = nil
|
469
|
-
@modules = []
|
470
|
-
end
|
471
|
-
|
472
|
-
# Copy the configuration for the given action
|
473
|
-
#
|
474
|
-
# @param base [Class] the target action
|
475
|
-
#
|
476
|
-
# @return void
|
477
|
-
#
|
478
|
-
# @since 0.3.0
|
479
|
-
# @api private
|
480
|
-
def copy!(base)
|
481
|
-
modules.each do |mod|
|
482
|
-
base.class_eval(&mod)
|
483
|
-
end
|
484
|
-
end
|
485
|
-
|
486
|
-
# @api private
|
487
|
-
alias_method :unload!, :reset!
|
488
|
-
|
489
|
-
protected
|
490
|
-
# @api private
|
491
|
-
attr_writer :namespace
|
492
|
-
# @api private
|
493
|
-
attr_writer :root
|
494
|
-
# @api private
|
495
|
-
attr_writer :load_paths
|
496
|
-
# @api private
|
497
|
-
attr_writer :layout
|
498
|
-
# @api private
|
499
|
-
attr_writer :default_encoding
|
500
|
-
# @api private
|
501
|
-
attr_writer :modules
|
502
|
-
end
|
503
|
-
end
|
504
|
-
end
|