dry-view 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +18 -0
  3. data/.travis.yml +15 -10
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +60 -1
  6. data/Gemfile +15 -5
  7. data/README.md +38 -13
  8. data/bin/setup +5 -0
  9. data/bin/setup_helpers.rb +27 -0
  10. data/dry-view.gemspec +8 -9
  11. data/lib/dry-view.rb +3 -1
  12. data/lib/dry/view.rb +503 -2
  13. data/lib/dry/view/context.rb +80 -0
  14. data/lib/dry/view/decorated_attributes.rb +81 -0
  15. data/lib/dry/view/exposure.rb +15 -2
  16. data/lib/dry/view/exposures.rb +15 -5
  17. data/lib/dry/view/part.rb +154 -61
  18. data/lib/dry/view/part_builder.rb +136 -0
  19. data/lib/dry/view/path.rb +22 -5
  20. data/lib/dry/view/render_environment.rb +62 -0
  21. data/lib/dry/view/render_environment_missing.rb +44 -0
  22. data/lib/dry/view/rendered.rb +55 -0
  23. data/lib/dry/view/renderer.rb +22 -19
  24. data/lib/dry/view/scope.rb +146 -14
  25. data/lib/dry/view/scope_builder.rb +98 -0
  26. data/lib/dry/view/tilt.rb +78 -0
  27. data/lib/dry/view/tilt/erb.rb +26 -0
  28. data/lib/dry/view/tilt/erbse.rb +21 -0
  29. data/lib/dry/view/tilt/haml.rb +26 -0
  30. data/lib/dry/view/version.rb +5 -2
  31. metadata +50 -88
  32. data/benchmarks/templates/button.html.erb +0 -1
  33. data/benchmarks/view.rb +0 -24
  34. data/lib/dry/view/controller.rb +0 -159
  35. data/lib/dry/view/decorator.rb +0 -45
  36. data/lib/dry/view/missing_renderer.rb +0 -15
  37. data/spec/fixtures/templates/_hello.html.slim +0 -1
  38. data/spec/fixtures/templates/controller_renderer_options.html.erb +0 -3
  39. data/spec/fixtures/templates/decorated_parts.html.slim +0 -4
  40. data/spec/fixtures/templates/edit.html.slim +0 -11
  41. data/spec/fixtures/templates/empty.html.slim +0 -1
  42. data/spec/fixtures/templates/greeting.html.slim +0 -2
  43. data/spec/fixtures/templates/hello.html.slim +0 -1
  44. data/spec/fixtures/templates/layouts/app.html.slim +0 -6
  45. data/spec/fixtures/templates/layouts/app.txt.erb +0 -3
  46. data/spec/fixtures/templates/parts_with_args.html.slim +0 -3
  47. data/spec/fixtures/templates/parts_with_args/_box.html.slim +0 -3
  48. data/spec/fixtures/templates/shared/_index_table.html.slim +0 -2
  49. data/spec/fixtures/templates/shared/_shared_hello.html.slim +0 -1
  50. data/spec/fixtures/templates/tasks.html.slim +0 -3
  51. data/spec/fixtures/templates/user.html.slim +0 -2
  52. data/spec/fixtures/templates/users.html.slim +0 -5
  53. data/spec/fixtures/templates/users.txt.erb +0 -3
  54. data/spec/fixtures/templates/users/_row.html.slim +0 -2
  55. data/spec/fixtures/templates/users/_tbody.html.slim +0 -5
  56. data/spec/fixtures/templates/users_with_count.html.slim +0 -5
  57. data/spec/fixtures/templates/users_with_count_inherit.html.slim +0 -6
  58. data/spec/fixtures/templates_override/_hello.html.slim +0 -1
  59. data/spec/fixtures/templates_override/users.html.slim +0 -5
  60. data/spec/integration/decorator_spec.rb +0 -80
  61. data/spec/integration/exposures_spec.rb +0 -392
  62. data/spec/integration/part/decorated_attributes_spec.rb +0 -193
  63. data/spec/integration/view_spec.rb +0 -133
  64. data/spec/spec_helper.rb +0 -46
  65. data/spec/unit/controller_spec.rb +0 -83
  66. data/spec/unit/decorator_spec.rb +0 -61
  67. data/spec/unit/exposure_spec.rb +0 -227
  68. data/spec/unit/exposures_spec.rb +0 -103
  69. data/spec/unit/part_spec.rb +0 -104
  70. data/spec/unit/renderer_spec.rb +0 -57
  71. data/spec/unit/scope_spec.rb +0 -53
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/core/cache"
4
+ require "dry/equalizer"
5
+ require_relative "scope"
6
+
7
+ module Dry
8
+ class View
9
+ # Builds scope objects via matching classes
10
+ #
11
+ # @api private
12
+ class ScopeBuilder
13
+ extend Dry::Core::Cache
14
+ include Dry::Equalizer(:namespace)
15
+
16
+ # The view's configured `scope_namespace`
17
+ #
18
+ # @api private
19
+ attr_reader :namespace
20
+
21
+ # @return [RenderEnvironment]
22
+ #
23
+ # @api private
24
+ attr_reader :render_env
25
+
26
+ # Returns a new instance of ScopeBuilder
27
+ #
28
+ # @api private
29
+ def initialize(namespace: nil, render_env: nil)
30
+ @namespace = namespace
31
+ @render_env = render_env
32
+ end
33
+
34
+ # @api private
35
+ def for_render_env(render_env)
36
+ return self if render_env == self.render_env
37
+
38
+ self.class.new(namespace: namespace, render_env: render_env)
39
+ end
40
+
41
+ # Returns a new scope using a class matching the name
42
+ #
43
+ # @param name [Symbol, Class] scope name
44
+ # @param locals [Hash<Symbol, Object>] locals hash
45
+ #
46
+ # @return [Dry::View::Scope]
47
+ #
48
+ # @api private
49
+ def call(name = nil, locals)
50
+ scope_class(name).new(
51
+ name: name,
52
+ locals: locals,
53
+ render_env: render_env,
54
+ )
55
+ end
56
+
57
+ private
58
+
59
+ DEFAULT_SCOPE_CLASS = Scope
60
+
61
+ def scope_class(name = nil)
62
+ if name.nil?
63
+ DEFAULT_SCOPE_CLASS
64
+ elsif name.is_a?(Class)
65
+ name
66
+ else
67
+ fetch_or_store(namespace, name) do
68
+ resolve_scope_class(name: name)
69
+ end
70
+ end
71
+ end
72
+
73
+ def resolve_scope_class(name:)
74
+ name = inflector.camelize(name.to_s)
75
+
76
+ # Give autoloaders a chance to act
77
+ begin
78
+ klass = namespace.const_get(name)
79
+ rescue NameError
80
+ end
81
+
82
+ if !klass && namespace.const_defined?(name, false)
83
+ klass = namespace.const_get(name)
84
+ end
85
+
86
+ if klass && klass < Scope
87
+ klass
88
+ else
89
+ DEFAULT_SCOPE_CLASS
90
+ end
91
+ end
92
+
93
+ def inflector
94
+ render_env.inflector
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/core/cache"
4
+ require "tilt"
5
+
6
+ module Dry
7
+ class View
8
+ # @api private
9
+ module Tilt
10
+ extend Dry::Core::Cache
11
+
12
+ class << self
13
+ def [](path, mapping, **options)
14
+ ext = File.extname(path).sub(%r{^.}, "").to_sym
15
+ activate_adapter ext
16
+
17
+ with_mapping(mapping).new(path, **options)
18
+ end
19
+
20
+ def default_mapping
21
+ ::Tilt.default_mapping
22
+ end
23
+
24
+ def register_adapter(ext, adapter)
25
+ adapters[ext] = adapter
26
+ end
27
+
28
+ def deregister_adapter(ext)
29
+ adapters.delete(ext)
30
+ end
31
+
32
+ private
33
+
34
+ def adapters
35
+ @adapters ||= {}
36
+ end
37
+
38
+ def activate_adapter(ext)
39
+ fetch_or_store(:adapter, ext) {
40
+ adapter = adapters[ext]
41
+ return unless adapter
42
+
43
+ *requires, error_message = adapter.requirements
44
+
45
+ begin
46
+ requires.each(&method(:require))
47
+ rescue LoadError => e
48
+ raise e, "#{e.message}\n\n#{error_message}"
49
+ end
50
+
51
+ adapter.activate
52
+ }
53
+ end
54
+
55
+ def with_mapping(mapping)
56
+ fetch_or_store(:mapping, mapping) {
57
+ if mapping.any?
58
+ build_mapping(mapping)
59
+ else
60
+ default_mapping
61
+ end
62
+ }
63
+ end
64
+
65
+ def build_mapping(mapping)
66
+ default_mapping.dup.tap do |new_mapping|
67
+ mapping.each do |extension, template_class|
68
+ new_mapping.register template_class, extension
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ require_relative "tilt/erb"
78
+ require_relative "tilt/haml"
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class View
5
+ module Tilt
6
+ module ERB
7
+ def self.requirements
8
+ ["dry/view/tilt/erbse", <<~ERROR]
9
+ dry-view requires erbse for full compatibility when rendering .erb templates (e.g. implicitly capturing block content when yielding)
10
+
11
+ To ignore this and use another engine for .erb templates, deregister this adapter before calling your views:
12
+
13
+ Dry::View::Tilt.deregister_adapter(:erb)
14
+ ERROR
15
+ end
16
+
17
+ def self.activate
18
+ Tilt.default_mapping.register ErbseTemplate, 'erb'
19
+ self
20
+ end
21
+ end
22
+
23
+ register_adapter :erb, ERB
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tilt/template"
4
+ require "erbse"
5
+
6
+ module Dry
7
+ class View
8
+ module Tilt
9
+ # Tilt template class copied from cells-erb gem
10
+ class ErbseTemplate < ::Tilt::Template
11
+ def prepare
12
+ @template = ::Erbse::Engine.new
13
+ end
14
+
15
+ def precompiled_template(locals)
16
+ @template.call(data)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ class View
5
+ module Tilt
6
+ module Haml
7
+ def self.requirements
8
+ ["hamlit/block", <<~ERROR]
9
+ dry-view requires hamlit-block for full compatibility when rendering .haml templates (e.g. implicitly capturing block content when yielding)
10
+
11
+ To ignore this and use another engine for .haml templates, dereigster this adapter before calling your views:
12
+
13
+ Dry::View::Tilt.deregister_adatper(:haml)
14
+ ERROR
15
+ end
16
+
17
+ def self.activate
18
+ # Requiring hamlit/block will register the engine with Tilt
19
+ self
20
+ end
21
+ end
22
+
23
+ register_adapter :haml, Haml
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Dry
2
- module View
3
- VERSION = '0.5.3'.freeze
4
+ class View
5
+ # @api private
6
+ VERSION = "0.6.0"
4
7
  end
5
8
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
- - Piotr Solnica
8
7
  - Tim Riley
8
+ - Piotr Solnica
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-10-21 00:00:00.000000000 Z
12
+ date: 2019-01-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tilt
@@ -18,6 +18,9 @@ dependencies:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
20
  version: '2.0'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.0.6
21
24
  type: :runtime
22
25
  prerelease: false
23
26
  version_requirements: !ruby/object:Gem::Requirement
@@ -25,6 +28,9 @@ dependencies:
25
28
  - - "~>"
26
29
  - !ruby/object:Gem::Version
27
30
  version: '2.0'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.6
28
34
  - !ruby/object:Gem::Dependency
29
35
  name: dry-core
30
36
  requirement: !ruby/object:Gem::Requirement
@@ -68,19 +74,33 @@ dependencies:
68
74
  - !ruby/object:Gem::Version
69
75
  version: '0.2'
70
76
  - !ruby/object:Gem::Dependency
71
- name: bundler
77
+ name: dry-inflector
72
78
  requirement: !ruby/object:Gem::Requirement
73
79
  requirements:
74
80
  - - "~>"
75
81
  - !ruby/object:Gem::Version
76
- version: '1.7'
77
- type: :development
82
+ version: '0.1'
83
+ type: :runtime
78
84
  prerelease: false
79
85
  version_requirements: !ruby/object:Gem::Requirement
80
86
  requirements:
81
87
  - - "~>"
82
88
  - !ruby/object:Gem::Version
83
- version: '1.7'
89
+ version: '0.1'
90
+ - !ruby/object:Gem::Dependency
91
+ name: bundler
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
84
104
  - !ruby/object:Gem::Dependency
85
105
  name: rake
86
106
  requirement: !ruby/object:Gem::Requirement
@@ -109,75 +129,51 @@ dependencies:
109
129
  - - "~>"
110
130
  - !ruby/object:Gem::Version
111
131
  version: '3.1'
112
- description: Functional view rendering system
132
+ description: A complete, standalone view rendering system that gives you everything
133
+ you need to write well-factored view code
113
134
  email:
114
- - piotr.solnica@gmail.com
115
135
  - tim@icelab.com.au
136
+ - piotr.solnica@gmail.com
116
137
  executables: []
117
138
  extensions: []
118
139
  extra_rdoc_files: []
119
140
  files:
141
+ - ".codeclimate.yml"
120
142
  - ".gitignore"
121
143
  - ".rspec"
122
144
  - ".travis.yml"
145
+ - ".yardopts"
123
146
  - CHANGELOG.md
124
147
  - CONTRIBUTING.md
125
148
  - Gemfile
126
149
  - LICENSE.md
127
150
  - README.md
128
151
  - Rakefile
129
- - benchmarks/templates/button.html.erb
130
- - benchmarks/view.rb
131
152
  - bin/console
153
+ - bin/setup
154
+ - bin/setup_helpers.rb
132
155
  - dry-view.gemspec
133
156
  - lib/dry-view.rb
134
157
  - lib/dry/view.rb
135
- - lib/dry/view/controller.rb
136
- - lib/dry/view/decorator.rb
158
+ - lib/dry/view/context.rb
159
+ - lib/dry/view/decorated_attributes.rb
137
160
  - lib/dry/view/exposure.rb
138
161
  - lib/dry/view/exposures.rb
139
- - lib/dry/view/missing_renderer.rb
140
162
  - lib/dry/view/part.rb
163
+ - lib/dry/view/part_builder.rb
141
164
  - lib/dry/view/path.rb
165
+ - lib/dry/view/render_environment.rb
166
+ - lib/dry/view/render_environment_missing.rb
167
+ - lib/dry/view/rendered.rb
142
168
  - lib/dry/view/renderer.rb
143
169
  - lib/dry/view/scope.rb
170
+ - lib/dry/view/scope_builder.rb
171
+ - lib/dry/view/tilt.rb
172
+ - lib/dry/view/tilt/erb.rb
173
+ - lib/dry/view/tilt/erbse.rb
174
+ - lib/dry/view/tilt/haml.rb
144
175
  - lib/dry/view/version.rb
145
- - spec/fixtures/templates/_hello.html.slim
146
- - spec/fixtures/templates/controller_renderer_options.html.erb
147
- - spec/fixtures/templates/decorated_parts.html.slim
148
- - spec/fixtures/templates/edit.html.slim
149
- - spec/fixtures/templates/empty.html.slim
150
- - spec/fixtures/templates/greeting.html.slim
151
- - spec/fixtures/templates/hello.html.slim
152
- - spec/fixtures/templates/layouts/app.html.slim
153
- - spec/fixtures/templates/layouts/app.txt.erb
154
- - spec/fixtures/templates/parts_with_args.html.slim
155
- - spec/fixtures/templates/parts_with_args/_box.html.slim
156
- - spec/fixtures/templates/shared/_index_table.html.slim
157
- - spec/fixtures/templates/shared/_shared_hello.html.slim
158
- - spec/fixtures/templates/tasks.html.slim
159
- - spec/fixtures/templates/user.html.slim
160
- - spec/fixtures/templates/users.html.slim
161
- - spec/fixtures/templates/users.txt.erb
162
- - spec/fixtures/templates/users/_row.html.slim
163
- - spec/fixtures/templates/users/_tbody.html.slim
164
- - spec/fixtures/templates/users_with_count.html.slim
165
- - spec/fixtures/templates/users_with_count_inherit.html.slim
166
- - spec/fixtures/templates_override/_hello.html.slim
167
- - spec/fixtures/templates_override/users.html.slim
168
- - spec/integration/decorator_spec.rb
169
- - spec/integration/exposures_spec.rb
170
- - spec/integration/part/decorated_attributes_spec.rb
171
- - spec/integration/view_spec.rb
172
- - spec/spec_helper.rb
173
- - spec/unit/controller_spec.rb
174
- - spec/unit/decorator_spec.rb
175
- - spec/unit/exposure_spec.rb
176
- - spec/unit/exposures_spec.rb
177
- - spec/unit/part_spec.rb
178
- - spec/unit/renderer_spec.rb
179
- - spec/unit/scope_spec.rb
180
- homepage: https://github.com/dry-rb/dry-view
176
+ homepage: https://dry-rb.org/gems/dry-view
181
177
  licenses:
182
178
  - MIT
183
179
  metadata: {}
@@ -197,43 +193,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
193
  version: '0'
198
194
  requirements: []
199
195
  rubyforge_project:
200
- rubygems_version: 2.7.7
196
+ rubygems_version: 2.7.3
201
197
  signing_key:
202
198
  specification_version: 4
203
- summary: Functional view rendering system
204
- test_files:
205
- - spec/fixtures/templates/_hello.html.slim
206
- - spec/fixtures/templates/controller_renderer_options.html.erb
207
- - spec/fixtures/templates/decorated_parts.html.slim
208
- - spec/fixtures/templates/edit.html.slim
209
- - spec/fixtures/templates/empty.html.slim
210
- - spec/fixtures/templates/greeting.html.slim
211
- - spec/fixtures/templates/hello.html.slim
212
- - spec/fixtures/templates/layouts/app.html.slim
213
- - spec/fixtures/templates/layouts/app.txt.erb
214
- - spec/fixtures/templates/parts_with_args.html.slim
215
- - spec/fixtures/templates/parts_with_args/_box.html.slim
216
- - spec/fixtures/templates/shared/_index_table.html.slim
217
- - spec/fixtures/templates/shared/_shared_hello.html.slim
218
- - spec/fixtures/templates/tasks.html.slim
219
- - spec/fixtures/templates/user.html.slim
220
- - spec/fixtures/templates/users.html.slim
221
- - spec/fixtures/templates/users.txt.erb
222
- - spec/fixtures/templates/users/_row.html.slim
223
- - spec/fixtures/templates/users/_tbody.html.slim
224
- - spec/fixtures/templates/users_with_count.html.slim
225
- - spec/fixtures/templates/users_with_count_inherit.html.slim
226
- - spec/fixtures/templates_override/_hello.html.slim
227
- - spec/fixtures/templates_override/users.html.slim
228
- - spec/integration/decorator_spec.rb
229
- - spec/integration/exposures_spec.rb
230
- - spec/integration/part/decorated_attributes_spec.rb
231
- - spec/integration/view_spec.rb
232
- - spec/spec_helper.rb
233
- - spec/unit/controller_spec.rb
234
- - spec/unit/decorator_spec.rb
235
- - spec/unit/exposure_spec.rb
236
- - spec/unit/exposures_spec.rb
237
- - spec/unit/part_spec.rb
238
- - spec/unit/renderer_spec.rb
239
- - spec/unit/scope_spec.rb
199
+ summary: A complete, standalone view rendering system that gives you everything you
200
+ need to write well-factored view code
201
+ test_files: []