lotus-view 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dda1cba5c497595dbff0db4c47d5c0728579af21
4
- data.tar.gz: c69552c1116c135f86715a31b084612528e86f49
3
+ metadata.gz: e4af30e053bd980adee58a5c31b34d1a47672a2c
4
+ data.tar.gz: 9215a2d92a5f5c8b76e4a77d13bfae59f996cde2
5
5
  SHA512:
6
- metadata.gz: 0f8e135e3739e623393fb7df975c1680c7299dd91300414277d16a3143b7089a8fe3c8c0d42eda3b02c355cb61652b9234cb16b77f4211d1f747968dfa930bf0
7
- data.tar.gz: 45e78d2c9910b4f3e9bfff52162cb08478fd6f663b094c16aae59ec1148d33389db6a9d9237dc837fd48a0bb9956ecd0267abcbc35b4de7875d0b3ff3b3367be
6
+ metadata.gz: 2933b16f795e1fcd6f62b0739b035ebe9a0798cb4a9debffb40a01b72f5d65d95b16bbf4e72a3e2a64993bc3c32aba09c2200b804a5ad40205b34ac404b9dfc2
7
+ data.tar.gz: b735e3c0352fdcc0e8a685b148caf332543d514a840765c54ff49df52972f38bccc18d2ec110e126dc76706de44dee04c9d97820e1a1804fc7417d2e7c0dc031
@@ -1,6 +1,18 @@
1
1
  # Lotus::View
2
2
  View layer for Lotus
3
3
 
4
+ ## v0.5.0 - 2016-01-12
5
+ ### Added
6
+ - [Luca Guidi] Added `Lotus::View::Configuration#default_encoding` to set the encoding for templates
7
+
8
+ ### Fixed
9
+ - [Luca Guidi] Let exceptions to be raised as they occur in rendering context. This fixes misleading backtraces for exceptions.
10
+ - [Martin Rubi] Raise a `Lotus::View::MissingTemplateError` when rendering a missing partial from a template
11
+ - [Luca Guidi] Fix for `template.erb is not valid US-ASCII (Encoding::InvalidByteSequenceError)` when system encoding is not set
12
+
13
+ ### Changed
14
+ - [Liam Dawson] Introduced `Lotus::View::Error` and let all the framework exceptions to inherit from it.
15
+
4
16
  ## v0.4.4 - 2015-09-30
5
17
  ### Added
6
18
  - [Luca Guidi] Autoescape for layout helpers.
data/LICENSE.md CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Luca Guidi
1
+ Copyright (c) 2014-2016 Luca Guidi
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -573,6 +573,11 @@ Lotus::View.configure do
573
573
  #
574
574
  root '/path/to/root'
575
575
 
576
+ # Default encoding for templates
577
+ # Argument: String, defaults to utf-8
578
+ #
579
+ default_encoding 'koi-8'
580
+
576
581
  # Set the Ruby namespace where to lookup for views
577
582
  # Argument: Class, Module, String, defaults to Object
578
583
  #
@@ -845,4 +850,4 @@ __Lotus::View__ uses [Semantic Versioning 2.0.0](http://semver.org)
845
850
 
846
851
  ## Copyright
847
852
 
848
- Copyright 2014-2015 Luca Guidi – Released under MIT License
853
+ Copyright 2014-2016 Luca Guidi – Released under MIT License
@@ -78,7 +78,7 @@ module Lotus
78
78
  #
79
79
  # ApplicationLayout.template # => 'application'
80
80
  def template
81
- super.gsub(suffix, '')
81
+ super.sub(suffix, '')
82
82
  end
83
83
 
84
84
  # Template name suffix
@@ -7,6 +7,7 @@ require 'lotus/view/inheritable'
7
7
  require 'lotus/view/rendering'
8
8
  require 'lotus/view/escape'
9
9
  require 'lotus/view/dsl'
10
+ require 'lotus/view/errors'
10
11
  require 'lotus/layout'
11
12
  require 'lotus/presenter'
12
13
 
@@ -15,37 +16,7 @@ module Lotus
15
16
  #
16
17
  # @since 0.1.0
17
18
  module View
18
- # Missing template error
19
- #
20
- # This is raised at the runtime when Lotus::View cannot find a template for
21
- # the requested format.
22
- #
23
- # We can't raise this error during the loading phase, because at that time
24
- # we don't know if a view implements its own rendering policy.
25
- # A view is allowed to override `#render`, and this scenario can make the
26
- # presence of a template useless. One typical example is the usage of a
27
- # serializer that returns the output string, without rendering a template.
28
- #
29
- # @since 0.1.0
30
- class MissingTemplateError < ::StandardError
31
- def initialize(template, format)
32
- super("Can't find template '#{ template }' for '#{ format }' format.")
33
- end
34
- end
35
-
36
- # Missing format error
37
- #
38
- # This is raised at the runtime when rendering context lacks of the :format
39
- # key.
40
- #
41
- # @since 0.1.0
42
- #
43
- # @see Lotus::View::Rendering#render
44
- class MissingFormatError < ::StandardError
45
- end
46
-
47
19
  include Utils::ClassAttribute
48
-
49
20
  # Framework configuration
50
21
  #
51
22
  # @since 0.2.0
@@ -268,10 +239,10 @@ module Lotus
268
239
  conf.add_view(base)
269
240
 
270
241
  base.class_eval do
271
- extend Inheritable.dup
272
- extend Dsl.dup
273
- extend Rendering.dup
274
- extend Escape.dup
242
+ extend Inheritable
243
+ extend Dsl
244
+ extend Rendering
245
+ extend Escape
275
246
 
276
247
  include Utils::ClassAttribute
277
248
  class_attribute :configuration
@@ -29,6 +29,12 @@ module Lotus
29
29
  # @api private
30
30
  DEFAULT_ROOT = '.'.freeze
31
31
 
32
+ # Default encoding
33
+ #
34
+ # @since 0.5.0
35
+ # @api private
36
+ DEFAULT_ENCODING = Encoding::UTF_8
37
+
32
38
  attr_reader :load_paths
33
39
  attr_reader :views
34
40
  attr_reader :layouts
@@ -237,6 +243,55 @@ module Lotus
237
243
  end
238
244
  end
239
245
 
246
+ # Default encoding for templates
247
+ #
248
+ # This is part of a DSL, for this reason when this method is called with
249
+ # an argument, it will set the corresponding instance variable. When
250
+ # called without, it will return the already set value, or the default.
251
+ #
252
+ # @overload default_encoding(value)
253
+ # Sets the given value
254
+ # @param value [String,Encoding] a string representation of the encoding,
255
+ # or an Encoding constant
256
+ #
257
+ # @raise [ArgumentError] if the given value isn't a supported encoding
258
+ #
259
+ # @overload default_encoding
260
+ # Gets the value
261
+ # @return [Encoding]
262
+ #
263
+ # @since 0.5.0
264
+ #
265
+ # @example Set UTF-8 As A String
266
+ # require 'lotus/view'
267
+ #
268
+ # Lotus::View.configure do
269
+ # default_encoding 'utf-8'
270
+ # end
271
+ #
272
+ # @example Set UTF-8 As An Encoding Constant
273
+ # require 'lotus/view'
274
+ #
275
+ # Lotus::View.configure do
276
+ # default_encoding Encoding::UTF_8
277
+ # end
278
+ #
279
+ # @example Raise An Error For Unknown Encoding
280
+ # require 'lotus/view'
281
+ #
282
+ # Lotus::View.configure do
283
+ # default_encoding 'foo'
284
+ # end
285
+ #
286
+ # # => ArgumentError
287
+ def default_encoding(value = nil)
288
+ if value.nil?
289
+ @default_encoding
290
+ else
291
+ @default_encoding = Encoding.find(value)
292
+ end
293
+ end
294
+
240
295
  # Prepare the views.
241
296
  #
242
297
  # The given block will be yielded when `Lotus::View` will be included by
@@ -347,11 +402,12 @@ module Lotus
347
402
  # @api private
348
403
  def duplicate
349
404
  Configuration.new.tap do |c|
350
- c.namespace = namespace
351
- c.root = root
352
- c.layout = @layout # lazy loading of the class
353
- c.load_paths = load_paths.dup
354
- c.modules = modules.dup
405
+ c.namespace = namespace
406
+ c.root = root
407
+ c.layout = @layout # lazy loading of the class
408
+ c.default_encoding = default_encoding
409
+ c.load_paths = load_paths.dup
410
+ c.modules = modules.dup
355
411
  end
356
412
  end
357
413
 
@@ -370,7 +426,8 @@ module Lotus
370
426
  # @since 0.2.0
371
427
  # @api private
372
428
  def reset!
373
- root(DEFAULT_ROOT)
429
+ root DEFAULT_ROOT
430
+ default_encoding DEFAULT_ENCODING
374
431
 
375
432
  @views = Set.new
376
433
  @layouts = Set.new
@@ -400,6 +457,7 @@ module Lotus
400
457
  attr_writer :root
401
458
  attr_writer :load_paths
402
459
  attr_writer :layout
460
+ attr_writer :default_encoding
403
461
  attr_writer :modules
404
462
  end
405
463
  end
@@ -205,9 +205,9 @@ module Lotus
205
205
  # Bookshelf::Api::Views::Books::Index.template # => 'books/index'
206
206
  def template(value = nil)
207
207
  if value.nil?
208
- @@template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
208
+ @template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
209
209
  else
210
- @@template = value
210
+ @template = value
211
211
  end
212
212
  end
213
213
 
@@ -0,0 +1,47 @@
1
+ module Lotus
2
+ module View
3
+ # @since 0.5.0
4
+ class Error < ::StandardError
5
+ end
6
+
7
+ # Missing template error
8
+ #
9
+ # This is raised at the runtime when Lotus::View cannot find a template for
10
+ # the requested format.
11
+ #
12
+ # We can't raise this error during the loading phase, because at that time
13
+ # we don't know if a view implements its own rendering policy.
14
+ # A view is allowed to override `#render`, and this scenario can make the
15
+ # presence of a template useless. One typical example is the usage of a
16
+ # serializer that returns the output string, without rendering a template.
17
+ #
18
+ # @since 0.1.0
19
+ class MissingTemplateError < Error
20
+ def initialize(template, format)
21
+ super("Can't find template '#{ template }' for '#{ format }' format.")
22
+ end
23
+ end
24
+
25
+ # Missing format error
26
+ #
27
+ # This is raised at the runtime when rendering context lacks of the :format
28
+ # key.
29
+ #
30
+ # @since 0.1.0
31
+ #
32
+ # @see Lotus::View::Rendering#render
33
+ class MissingFormatError < Error
34
+ end
35
+
36
+ # Missing template layout error
37
+ #
38
+ # This is raised at the runtime when Lotus::Layout cannot find it's template.
39
+ #
40
+ # @since 0.5.0
41
+ class MissingTemplateLayoutError < Error
42
+ def initialize(template)
43
+ super("Can't find layout template '#{ template }'")
44
+ end
45
+ end
46
+ end
47
+ end
@@ -27,7 +27,7 @@ module Lotus
27
27
  # @api private
28
28
  # @since 0.1.0
29
29
  def subclasses
30
- @@subclasses ||= Set.new
30
+ @subclasses ||= Set.new
31
31
  end
32
32
 
33
33
  protected
@@ -47,7 +47,7 @@ module Lotus
47
47
  # @api private
48
48
  # @since 0.1.0
49
49
  def views
50
- @@views ||= [ self ] + subclasses.to_a
50
+ @views ||= [ self ] + subclasses.to_a
51
51
  end
52
52
  end
53
53
  end
@@ -258,7 +258,7 @@ module Lotus
258
258
  #
259
259
  # @see Lotus::View::Rendering::Registry
260
260
  def registry
261
- @@registry ||= Registry.new(self)
261
+ @registry ||= Registry.new(self)
262
262
  end
263
263
  end
264
264
  end
@@ -4,16 +4,6 @@ require 'lotus/view/rendering/templates_finder'
4
4
  module Lotus
5
5
  module View
6
6
  module Rendering
7
- # Missing template layout error
8
- #
9
- # This is raised at the runtime when Lotus::Layout cannot find it's template.
10
- #
11
- # @since 0.3.0
12
- class MissingTemplateLayoutError < ::StandardError
13
- def initialize(template)
14
- super("Can't find layout template '#{ template }'")
15
- end
16
- end
17
7
  # Holds the references of all the registered layouts.
18
8
  # As now the registry is unique at the level of the framework.
19
9
  #
@@ -210,8 +210,6 @@ module Lotus
210
210
  else
211
211
  ::Lotus::View::Escape.html(super)
212
212
  end
213
- rescue ::NameError
214
- ::Kernel.raise ::NoMethodError.new("undefined method `#{ m }' for #{ self.inspect }", m)
215
213
  end
216
214
 
217
215
  def renderer(options)
@@ -35,7 +35,7 @@ module Lotus
35
35
  # @api private
36
36
  def find
37
37
  if path = partial_template_under_view_path
38
- View::Template.new path
38
+ View::Template.new(path, @view.configuration.default_encoding)
39
39
  else
40
40
  super
41
41
  end
@@ -36,10 +36,12 @@ module Lotus
36
36
  #
37
37
  # @return [String] the output of the rendering process.
38
38
  #
39
+ # @raise [Lotus::View::MissingTemplateError] if template can't be found
40
+ #
39
41
  # @api private
40
42
  # @since 0.1.0
41
43
  def render
42
- template.render(scope)
44
+ (template or raise_missing_template_error).render(scope)
43
45
  end
44
46
 
45
47
  protected
@@ -50,6 +52,15 @@ module Lotus
50
52
  def scope
51
53
  Scope.new(@view, @options[:locals])
52
54
  end
55
+
56
+ # @since 0.5.0
57
+ # @api private
58
+ def raise_missing_template_error
59
+ raise MissingTemplateError.new(
60
+ @options.fetch(:template) { @options.fetch(:partial, nil) },
61
+ @options[:format]
62
+ )
63
+ end
53
64
  end
54
65
  end
55
66
  end
@@ -70,7 +70,7 @@ module Lotus
70
70
  # # => [#<Lotus::View::Template:0x007f8a0a86a970 ... @file="/path/to/templates/articles/show.html.erb">]
71
71
  def find
72
72
  _find.map do |template|
73
- View::Template.new(template)
73
+ View::Template.new(template, @view.configuration.default_encoding)
74
74
  end
75
75
  end
76
76
 
@@ -6,8 +6,8 @@ module Lotus
6
6
  #
7
7
  # @since 0.1.0
8
8
  class Template
9
- def initialize(template)
10
- @_template = Tilt.new(template)
9
+ def initialize(template, encoding = Encoding::UTF_8)
10
+ @_template = Tilt.new(template, nil, default_encoding: encoding)
11
11
  end
12
12
 
13
13
  # Returns the format that the template handles.
@@ -3,6 +3,6 @@ module Lotus
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.4.4'.freeze
6
+ VERSION = '0.5.0'.freeze
7
7
  end
8
8
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.required_ruby_version = '>= 2.0.0'
21
21
 
22
22
  spec.add_runtime_dependency 'tilt', '~> 2.0', '>= 2.0.1'
23
- spec.add_runtime_dependency 'lotus-utils', '~> 0.5'
23
+ spec.add_runtime_dependency 'lotus-utils', '~> 0.6'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.5'
26
26
  spec.add_development_dependency 'minitest', '~> 5'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-09-30 00:00:00.000000000 Z
13
+ date: 2016-01-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: tilt
@@ -38,14 +38,14 @@ dependencies:
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0.5'
41
+ version: '0.6'
42
42
  type: :runtime
43
43
  prerelease: false
44
44
  version_requirements: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.5'
48
+ version: '0.6'
49
49
  - !ruby/object:Gem::Dependency
50
50
  name: bundler
51
51
  requirement: !ruby/object:Gem::Requirement
@@ -106,6 +106,7 @@ files:
106
106
  - lib/lotus/view.rb
107
107
  - lib/lotus/view/configuration.rb
108
108
  - lib/lotus/view/dsl.rb
109
+ - lib/lotus/view/errors.rb
109
110
  - lib/lotus/view/escape.rb
110
111
  - lib/lotus/view/inheritable.rb
111
112
  - lib/lotus/view/rendering.rb
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  requirements: []
148
149
  rubyforge_project:
149
- rubygems_version: 2.4.5.1
150
+ rubygems_version: 2.5.1
150
151
  signing_key:
151
152
  specification_version: 4
152
153
  summary: View layer for Lotus, with a separation between views and templates