props_template 0.21.0 → 0.21.1

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
  SHA256:
3
- metadata.gz: 23e014144b689057bd6e6cc96a007b17263a2eab2a84890dd822056c2a4e258d
4
- data.tar.gz: bba7578e983913d2eeeb15fc12026bb66b25c6da5a8df52c4d2003e8615a2eba
3
+ metadata.gz: fe0d4afac65f1f86e1adfdcc956cc21d6df96a54e55e1ab26c8fa450c6a2a7be
4
+ data.tar.gz: ea96579bc3d06512867d4c11ee619051a205d64e814e5fe643debd5085d2b56e
5
5
  SHA512:
6
- metadata.gz: d1adee8973b67e46c8c2169a7664ec92c0ee52e21451108b449888b8a8dd89221b46d2918da2ace75e016530a7654ce03a513f4f761cb2f6fa938dd76d4a18ed
7
- data.tar.gz: 2bc87b68c92e21f4755bf4a41c8ea1290bbfcd94f570fa2cf98b86fc2eecaa9ae83eae0f58042c3ae18cfd9c9f0d0c54caa23b5cff4c92814dfd81b3e306baa0
6
+ metadata.gz: 363ed26b474344ec9e17b328521d10a2e226761272e80f49e72b32e6943f47e3e970d7939e0923548984e96a1cb361ff3e74beebfa058dabe86db4a683e48746
7
+ data.tar.gz: d99478adad2b0980ce471f992c32d8a8fb0ee4348c469cb7e25b3752c6e44c0181f4beb31bc14a65efafe4d3250588ad8b0d9713eade62ee48107a3072dafbfa
@@ -17,6 +17,14 @@ module Props
17
17
 
18
18
  class Partialer
19
19
  INVALID_PARTIAL_MESSAGE = "The partial name must be a string, but received (%s)."
20
+ OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " \
21
+ "make sure it starts with lowercase letter, " \
22
+ "and is followed by any combination of letters, numbers and underscores."
23
+ IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " \
24
+ "make sure your partial name starts with underscore."
25
+
26
+ INVALID_PARTIAL_MESSAGE = "The partial name must be a string, but received (%s)."
27
+
20
28
 
21
29
  def initialize(base, context, builder)
22
30
  @context = context
@@ -24,30 +32,28 @@ module Props
24
32
  @base = base
25
33
  end
26
34
 
27
- def extract_details(options) # :doc:
28
- @context.lookup_context.registered_details.each_with_object({}) do |key, details|
35
+ def extract_details(options)
36
+ registered_details.each_with_object({}) do |key, details|
29
37
  value = options[key]
30
38
 
31
39
  details[key] = Array(value) if value
32
40
  end
33
41
  end
34
42
 
43
+ def registered_details
44
+ if ActionView.version.to_s >= "7"
45
+ ActionView::LookupContext.registered_details
46
+ else
47
+ @context.lookup_context.registered_details
48
+ end
49
+ end
50
+
35
51
  def find_and_add_template(all_options)
36
52
  first_opts = all_options[0]
37
53
 
38
54
  if first_opts[:partial]
39
55
  partial_opts = block_opts_to_render_opts(@builder, first_opts)
40
- .merge(formats: [:json])
41
- partial_opts.delete(:handlers)
42
- partial = partial_opts[:partial]
43
-
44
- if !(String === partial)
45
- raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % (partial.inspect))
46
- end
47
-
48
- template_keys = retrieve_template_keys(partial_opts)
49
- details = extract_details(partial_opts)
50
- template = find_template(partial, template_keys, details)
56
+ template = find_template(partial_opts)
51
57
 
52
58
  all_options.map do |opts|
53
59
  opts[:_template] = template
@@ -58,9 +64,13 @@ module Props
58
64
  end
59
65
  end
60
66
 
61
- def find_template(path, locals, details)
62
- prefixes = path.include?(?/) ? [] : @context.lookup_context.prefixes
63
- @context.lookup_context.find_template(path, prefixes, true, locals, details)
67
+ def find_template(partial_opts)
68
+ partial = partial_opts[:partial]
69
+ template_keys = retrieve_template_keys(partial_opts)
70
+ details = extract_details(partial_opts)
71
+
72
+ prefixes = partial.include?(?/) ? [] : @context.lookup_context.prefixes
73
+ @context.lookup_context.find_template(partial, prefixes, true, template_keys, details)
64
74
  end
65
75
 
66
76
  def retrieve_template_keys(options)
@@ -75,24 +85,24 @@ module Props
75
85
  pass_opts[:locals] ||= {}
76
86
  pass_opts[:locals][:json] = @builder
77
87
  pass_opts[:partial] = partial
88
+ pass_opts[:formats] = [:json]
89
+ pass_opts.delete(:handlers)
78
90
 
79
- pass_opts
80
- end
91
+ if !(String === partial)
92
+ raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % (partial.inspect))
93
+ end
81
94
 
82
- def refine_options(options, item = nil)
83
- PartialRenderer.refine_options(options, item)
95
+ pass_opts
84
96
  end
85
97
 
86
98
  def handle(options)
87
- pass_opts = block_opts_to_render_opts(@builder, options)
88
- renderer = PartialRenderer.new(@context, pass_opts)
89
- template = options[:_template] || renderer.template
99
+ partial_opts = block_opts_to_render_opts(@builder, options)
100
+ template = options[:_template] || find_template(partial_opts)
90
101
 
91
- renderer.render(template, pass_opts)
102
+ render_partial(template, @context, partial_opts)
92
103
  end
93
104
 
94
- def render(template, options)
95
- view = @context
105
+ def render_partial(template, view, options)
96
106
  instrument(:partial, identifier: template.identifier) do |payload|
97
107
  locals = options[:locals]
98
108
  content = template.render(view, locals)
@@ -101,33 +111,32 @@ module Props
101
111
  build_rendered_template(content, template)
102
112
  end
103
113
  end
104
- end
105
114
 
106
- class PartialRenderer
107
- OPTION_AS_ERROR_MESSAGE = "The value (%s) of the option `as` is not a valid Ruby identifier; " \
108
- "make sure it starts with lowercase letter, " \
109
- "and is followed by any combination of letters, numbers and underscores."
110
- IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " \
111
- "make sure your partial name starts with underscore."
112
-
113
- INVALID_PARTIAL_MESSAGE = "The partial name must be a string, but received (%s)."
115
+ def build_rendered_template(content, template, layout = nil)
116
+ RenderedTemplate.new content, layout, template
117
+ end
114
118
 
119
+ def instrument(name, **options) # :doc:
120
+ ActiveSupport::Notifications.instrument("render_#{name}.action_view", options) do |payload|
121
+ yield payload
122
+ end
123
+ end
115
124
 
116
- def self.raise_invalid_option_as(as)
125
+ def raise_invalid_option_as(as)
117
126
  raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % (as))
118
127
  end
119
128
 
120
- def self.raise_invalid_identifier(path)
129
+ def raise_invalid_identifier(path)
121
130
  raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
122
131
  end
123
132
 
124
- def self.retrieve_variable(path)
133
+ def retrieve_variable(path)
125
134
  base = path[-1] == "/" ? "" : File.basename(path)
126
135
  raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
127
136
  $1.to_sym
128
137
  end
129
138
 
130
- def self.refine_options(options, item = nil)
139
+ def refine_options(options, item = nil)
131
140
  return options if !options[:partial]
132
141
 
133
142
  partial, rest = [*options[:partial]]
@@ -156,78 +165,5 @@ module Props
156
165
 
157
166
  pass_opts
158
167
  end
159
-
160
- attr_reader :template
161
-
162
- def initialize(context, options)
163
- @context = context
164
- @options = options.merge(formats: [:json])
165
- @options.delete(:handlers)
166
- @details = extract_details(@options)
167
-
168
- partial = @options[:partial]
169
-
170
- if !(String === partial)
171
- raise_invalid_partial(partial.inspect)
172
- end
173
-
174
- @path = partial
175
-
176
- template_keys = retrieve_template_keys(@options)
177
- @template = find_template(@path, template_keys)
178
- end
179
-
180
- def render(template, options)
181
- #remove this later
182
-
183
- render_partial(template, @context, @options)
184
- end
185
-
186
- private
187
- def extract_details(options) # :doc:
188
- @context.lookup_context.registered_details.each_with_object({}) do |key, details|
189
- value = options[key]
190
-
191
- details[key] = Array(value) if value
192
- end
193
- end
194
-
195
- def instrument(name, **options) # :doc:
196
- ActiveSupport::Notifications.instrument("render_#{name}.action_view", options) do |payload|
197
- yield payload
198
- end
199
- end
200
-
201
- def render_partial(template, view, options)
202
- template ||= @template
203
- # @variable ||= template.variable
204
-
205
- instrument(:partial, identifier: @template.identifier) do |payload|
206
- locals = options[:locals]
207
- content = template.render(view, locals)
208
-
209
- payload[:cache_hit] = view.view_renderer.cache_hits[template.virtual_path]
210
- build_rendered_template(content, template)
211
- end
212
- end
213
-
214
- def build_rendered_template(content, template, layout = nil)
215
- RenderedTemplate.new content, layout, template
216
- end
217
-
218
- def find_template(path, locals)
219
- prefixes = path.include?(?/) ? [] : @context.lookup_context.prefixes
220
- @context.lookup_context.find_template(path, prefixes, true, locals, @details)
221
- end
222
-
223
- def retrieve_template_keys(options)
224
- template_keys = options[:locals].keys
225
- template_keys << options[:as] if options[:as]
226
- template_keys
227
- end
228
-
229
- def raise_invalid_partial(path)
230
- raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % (path))
231
- end
232
168
  end
233
169
  end
@@ -1,3 +1,3 @@
1
1
  module Props
2
- VERSION = "0.21.0".freeze
2
+ VERSION = "0.21.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: props_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.21.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-04 00:00:00.000000000 Z
11
+ date: 2022-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport