ultra-pure-box 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/jbuilder-2.15.1/Appraisals +26 -0
  3. data/jbuilder-2.15.1/CONTRIBUTING.md +100 -0
  4. data/jbuilder-2.15.1/Gemfile +9 -0
  5. data/jbuilder-2.15.1/MIT-LICENSE +20 -0
  6. data/jbuilder-2.15.1/README.md +394 -0
  7. data/jbuilder-2.15.1/Rakefile +21 -0
  8. data/jbuilder-2.15.1/bin/release +14 -0
  9. data/jbuilder-2.15.1/bin/test +6 -0
  10. data/jbuilder-2.15.1/gemfiles/rails_7_0.gemfile +11 -0
  11. data/jbuilder-2.15.1/gemfiles/rails_7_1.gemfile +10 -0
  12. data/jbuilder-2.15.1/gemfiles/rails_7_2.gemfile +10 -0
  13. data/jbuilder-2.15.1/gemfiles/rails_8_0.gemfile +10 -0
  14. data/jbuilder-2.15.1/gemfiles/rails_8_1.gemfile +10 -0
  15. data/jbuilder-2.15.1/gemfiles/rails_head.gemfile +10 -0
  16. data/jbuilder-2.15.1/jbuilder.gemspec +35 -0
  17. data/jbuilder-2.15.1/lib/generators/rails/jbuilder_generator.rb +65 -0
  18. data/jbuilder-2.15.1/lib/generators/rails/scaffold_controller_generator.rb +24 -0
  19. data/jbuilder-2.15.1/lib/generators/rails/templates/api_controller.rb +69 -0
  20. data/jbuilder-2.15.1/lib/generators/rails/templates/controller.rb +86 -0
  21. data/jbuilder-2.15.1/lib/generators/rails/templates/index.json.jbuilder +1 -0
  22. data/jbuilder-2.15.1/lib/generators/rails/templates/partial.json.jbuilder +16 -0
  23. data/jbuilder-2.15.1/lib/generators/rails/templates/show.json.jbuilder +1 -0
  24. data/jbuilder-2.15.1/lib/jbuilder/blank.rb +13 -0
  25. data/jbuilder-2.15.1/lib/jbuilder/collection_renderer.rb +58 -0
  26. data/jbuilder-2.15.1/lib/jbuilder/errors.rb +26 -0
  27. data/jbuilder-2.15.1/lib/jbuilder/jbuilder.rb +3 -0
  28. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_dependency_tracker.rb +75 -0
  29. data/jbuilder-2.15.1/lib/jbuilder/jbuilder_template.rb +264 -0
  30. data/jbuilder-2.15.1/lib/jbuilder/key_formatter.rb +32 -0
  31. data/jbuilder-2.15.1/lib/jbuilder/railtie.rb +34 -0
  32. data/jbuilder-2.15.1/lib/jbuilder/version.rb +5 -0
  33. data/jbuilder-2.15.1/lib/jbuilder.rb +384 -0
  34. data/jbuilder-2.15.1/test/jbuilder_dependency_tracker_test.rb +71 -0
  35. data/jbuilder-2.15.1/test/jbuilder_generator_test.rb +68 -0
  36. data/jbuilder-2.15.1/test/jbuilder_template_test.rb +469 -0
  37. data/jbuilder-2.15.1/test/jbuilder_test.rb +954 -0
  38. data/jbuilder-2.15.1/test/scaffold_api_controller_generator_test.rb +83 -0
  39. data/jbuilder-2.15.1/test/scaffold_controller_generator_test.rb +116 -0
  40. data/jbuilder-2.15.1/test/test_helper.rb +47 -0
  41. data/ultra-pure-box.gemspec +12 -0
  42. metadata +81 -0
@@ -0,0 +1,264 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jbuilder/jbuilder'
4
+ require 'jbuilder/collection_renderer'
5
+ require 'action_dispatch/http/mime_type'
6
+ require 'active_support/cache'
7
+
8
+ class JbuilderTemplate < Jbuilder
9
+ class << self
10
+ attr_accessor :template_lookup_options
11
+ end
12
+
13
+ self.template_lookup_options = { handlers: [:jbuilder] }
14
+
15
+ def initialize(context, options = nil)
16
+ @context = context
17
+ @cached_root = nil
18
+
19
+ options.nil? ? super() : super(**options)
20
+ end
21
+
22
+ # Generates JSON using the template specified with the `:partial` option. For example, the code below will render
23
+ # the file `views/comments/_comments.json.jbuilder`, and set a local variable comments with all this message's
24
+ # comments, which can be used inside the partial.
25
+ #
26
+ # Example:
27
+ #
28
+ # json.partial! 'comments/comments', comments: @message.comments
29
+ #
30
+ # There are multiple ways to generate a collection of elements as JSON, as illustrated below:
31
+ #
32
+ # Example:
33
+ #
34
+ # json.array! @posts, partial: 'posts/post', as: :post
35
+ #
36
+ # # or:
37
+ # json.partial! 'posts/post', collection: @posts, as: :post
38
+ #
39
+ # # or:
40
+ # json.partial! partial: 'posts/post', collection: @posts, as: :post
41
+ #
42
+ # # or:
43
+ # json.comments @post.comments, partial: 'comments/comment', as: :comment
44
+ #
45
+ # Aside from that, the `:cached` options is available on Rails >= 6.0. This will cache the rendered results
46
+ # effectively using the multi fetch feature.
47
+ #
48
+ # Example:
49
+ #
50
+ # json.array! @posts, partial: "posts/post", as: :post, cached: true
51
+ #
52
+ # json.comments @post.comments, partial: "comments/comment", as: :comment, cached: true
53
+ #
54
+ def partial!(*args)
55
+ if args.one? && _is_active_model?(args.first)
56
+ _render_active_model_partial args.first
57
+ else
58
+ options = args.extract_options!.dup
59
+ options[:partial] = args.first if args.present?
60
+ _render_partial_with_options options
61
+ end
62
+ end
63
+
64
+ # Caches the json constructed within the block passed. Has the same signature as the `cache` helper
65
+ # method in `ActionView::Helpers::CacheHelper` and so can be used in the same way.
66
+ #
67
+ # Example:
68
+ #
69
+ # json.cache! ['v1', @person], expires_in: 10.minutes do
70
+ # json.extract! @person, :name, :age
71
+ # end
72
+ def cache!(key=nil, options={})
73
+ if @context.controller.perform_caching
74
+ value = _cache_fragment_for(key, options) do
75
+ _scope { yield self }
76
+ end
77
+
78
+ merge! value
79
+ else
80
+ yield
81
+ end
82
+ end
83
+
84
+ # Caches the json structure at the root using a string rather than the hash structure. This is considerably
85
+ # faster, but the drawback is that it only works, as the name hints, at the root. So you cannot
86
+ # use this approach to cache deeper inside the hierarchy, like in partials or such. Continue to use #cache! there.
87
+ #
88
+ # Example:
89
+ #
90
+ # json.cache_root! @person do
91
+ # json.extract! @person, :name, :age
92
+ # end
93
+ #
94
+ # # json.extra 'This will not work either, the root must be exclusive'
95
+ def cache_root!(key=nil, options={})
96
+ if @context.controller.perform_caching
97
+ ::Kernel.raise "cache_root! can't be used after JSON structures have been defined" if @attributes.present?
98
+
99
+ @cached_root = _cache_fragment_for([ :root, key ], options) { yield; target! }
100
+ else
101
+ yield
102
+ end
103
+ end
104
+
105
+ # Conditionally caches the json depending in the condition given as first parameter. Has the same
106
+ # signature as the `cache` helper method in `ActionView::Helpers::CacheHelper` and so can be used in
107
+ # the same way.
108
+ #
109
+ # Example:
110
+ #
111
+ # json.cache_if! !admin?, @person, expires_in: 10.minutes do
112
+ # json.extract! @person, :name, :age
113
+ # end
114
+ def cache_if!(condition, *args, &block)
115
+ condition ? cache!(*args, &block) : yield
116
+ end
117
+
118
+ def target!
119
+ @cached_root || super
120
+ end
121
+
122
+ def array!(collection = EMPTY_ARRAY, *args, &block)
123
+ options = args.first
124
+
125
+ if _partial_options?(options)
126
+ options = options.dup
127
+ options[:collection] = collection
128
+ _render_partial_with_options options
129
+ else
130
+ _array collection, args, &block
131
+ end
132
+ end
133
+
134
+ def set!(name, object = BLANK, *args, &block)
135
+ options = args.first
136
+
137
+ if _partial_options?(options)
138
+ _set_inline_partial name, object, options.dup
139
+ else
140
+ _set name, object, args, &block
141
+ end
142
+ end
143
+
144
+ alias_method :method_missing, :set!
145
+ private :method_missing
146
+
147
+ private
148
+
149
+ def _render_partial_with_options(options)
150
+ options[:locals] ||= options.except(:partial, :as, :collection, :cached)
151
+ options[:handlers] ||= ::JbuilderTemplate.template_lookup_options[:handlers]
152
+ as = options[:as]
153
+
154
+ if as && options.key?(:collection)
155
+ collection = options.delete(:collection) || EMPTY_ARRAY
156
+ partial = options.delete(:partial)
157
+ options[:locals][:json] = self
158
+ collection = EnumerableCompat.new(collection) if collection.respond_to?(:count) && !collection.respond_to?(:size)
159
+
160
+ if options.has_key?(:layout)
161
+ ::Kernel.raise ::NotImplementedError, "The `:layout' option is not supported in collection rendering."
162
+ end
163
+
164
+ if options.has_key?(:spacer_template)
165
+ ::Kernel.raise ::NotImplementedError, "The `:spacer_template' option is not supported in collection rendering."
166
+ end
167
+
168
+ if collection.present?
169
+ results = CollectionRenderer
170
+ .new(@context.lookup_context, options) { |&block| _scope(&block) }
171
+ .render_collection_with_partial(collection, partial, @context, nil)
172
+
173
+ _array if results.respond_to?(:body) && results.body.nil?
174
+ else
175
+ _array
176
+ end
177
+ else
178
+ options[:locals][:json] = self
179
+ @context.render options
180
+ end
181
+ end
182
+
183
+ def _cache_fragment_for(key, options, &block)
184
+ key = _cache_key(key, options)
185
+ _read_fragment_cache(key, options) || _write_fragment_cache(key, options, &block)
186
+ end
187
+
188
+ def _read_fragment_cache(key, options = nil)
189
+ @context.controller.instrument_fragment_cache :read_fragment, key do
190
+ ::Rails.cache.read(key, options)
191
+ end
192
+ end
193
+
194
+ def _write_fragment_cache(key, options = nil)
195
+ @context.controller.instrument_fragment_cache :write_fragment, key do
196
+ yield.tap do |value|
197
+ ::Rails.cache.write(key, value, options)
198
+ end
199
+ end
200
+ end
201
+
202
+ def _cache_key(key, options)
203
+ name_options = options.slice(:skip_digest, :virtual_path)
204
+ key = _fragment_name_with_digest(key, name_options)
205
+
206
+ if @context.respond_to?(:combined_fragment_cache_key)
207
+ key = @context.combined_fragment_cache_key(key)
208
+ else
209
+ key = url_for(key).split('://', 2).last if ::Hash === key
210
+ end
211
+
212
+ ::ActiveSupport::Cache.expand_cache_key(key, :jbuilder)
213
+ end
214
+
215
+ def _fragment_name_with_digest(key, options)
216
+ if @context.respond_to?(:cache_fragment_name)
217
+ @context.cache_fragment_name(key, **options)
218
+ else
219
+ key
220
+ end
221
+ end
222
+
223
+ def _partial_options?(options)
224
+ ::Hash === options && options.key?(:as) && options.key?(:partial)
225
+ end
226
+
227
+ def _is_active_model?(object)
228
+ object.class.respond_to?(:model_name) && object.respond_to?(:to_partial_path)
229
+ end
230
+
231
+ def _set_inline_partial(name, object, options)
232
+ value = if object.nil?
233
+ EMPTY_ARRAY
234
+ elsif _is_collection?(object)
235
+ _scope do
236
+ options[:collection] = object
237
+ _render_partial_with_options options
238
+ end
239
+ else
240
+ _scope do
241
+ options[options[:as]] = object
242
+ _render_partial_with_options options
243
+ end
244
+ end
245
+
246
+ _set_value name, value
247
+ end
248
+
249
+ def _render_active_model_partial(object)
250
+ @context.render object, json: self
251
+ end
252
+ end
253
+
254
+ class JbuilderHandler
255
+ cattr_accessor :default_format
256
+ self.default_format = :json
257
+
258
+ def self.call(template, source = nil)
259
+ source ||= template.source
260
+ # this juggling is required to keep line numbers right in the error
261
+ %{__already_defined = defined?(json); json||=JbuilderTemplate.new(self); #{source};
262
+ json.target! unless (__already_defined && __already_defined != "method")}
263
+ end
264
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jbuilder/jbuilder'
4
+
5
+ class Jbuilder
6
+ class KeyFormatter
7
+ def initialize(*formats, **formats_with_options)
8
+ @mutex = Mutex.new
9
+ @formats = formats
10
+ @formats_with_options = formats_with_options
11
+ @cache = {}
12
+ end
13
+
14
+ def format(key)
15
+ @cache[key] || @mutex.synchronize do
16
+ @cache[key] ||= begin
17
+ value = key.is_a?(Symbol) ? key.name : key.to_s
18
+
19
+ @formats.each do |func|
20
+ value = func.is_a?(Proc) ? func.call(value) : value.send(func)
21
+ end
22
+
23
+ @formats_with_options.each do |func, params|
24
+ value = func.is_a?(Proc) ? func.call(value, *params) : value.send(func, *params)
25
+ end
26
+
27
+ value
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+ require 'jbuilder/jbuilder_template'
5
+
6
+ class Jbuilder
7
+ class Railtie < ::Rails::Railtie
8
+ initializer :jbuilder do
9
+ ActiveSupport.on_load :action_view do
10
+ ActionView::Template.register_template_handler :jbuilder, JbuilderHandler
11
+ require 'jbuilder/jbuilder_dependency_tracker'
12
+ end
13
+
14
+ module ::ActionController
15
+ module ApiRendering
16
+ include ActionView::Rendering
17
+ end
18
+ end
19
+
20
+ ActiveSupport.on_load :action_controller_api do
21
+ include ActionController::Helpers
22
+ include ActionController::ImplicitRender
23
+ helper_method :combined_fragment_cache_key
24
+ helper_method :view_cache_dependencies
25
+ end
26
+ end
27
+
28
+ generators do |app|
29
+ Rails::Generators.configure! app.config.generators
30
+ Rails::Generators.hidden_namespaces.uniq!
31
+ require 'generators/rails/scaffold_controller_generator'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Jbuilder < BasicObject
4
+ VERSION = "2.15.1"
5
+ end