turbostreamer 1.3.0 → 1.9.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
  SHA256:
3
- metadata.gz: 81c2119468be8e96eb8e02da3fe72df9996aaa16fc610d4b0bf2b5f902663004
4
- data.tar.gz: 21fc7309b4f17ae08e76b73f70bdc6c9d6a456a37d354aa53c45313b4c5e017d
3
+ metadata.gz: 5560f65eafd9983a08cc6cd0745101c0909d923dbca5cb012b9f5b884446de37
4
+ data.tar.gz: 14697af3049c50e3504e46e66a0209cf694c450ba95ac84fa4b723038126460d
5
5
  SHA512:
6
- metadata.gz: 5353499e01978fa2b6036f2bd26bd4f76a3da28cd07a242aec9be06d67afb9125b49ba179e8d13748945f36c9cfecb276118d88952c600854f88ce4c65e0fb41
7
- data.tar.gz: 907614df89e2f67d3a087570eda988f55b498b85d65d4e2756eccafb2acfdcd7911ff4de726edadb3bec02b4428812b68d58ca86fcb8d884dbf1f4beaf304e2b
6
+ metadata.gz: 894d614c59b3b002130c00519a5b687cc0814c4bd5358c033858f590f3a5e6082dec8b220cba91b59114eb1236129f1f166f27eb276c4ddbfab7bd796e81bacf
7
+ data.tar.gz: f0d5a68cf0e48ce0e73f50b879d69dc21fcdd6ed35110b4d8498255eeb33f35fb1b1326ff13619dee729adaca4a79ed787ced23f471931112bef5cf2657c1c03
data/README.md CHANGED
@@ -299,12 +299,25 @@ TurboStreamer.encode(encoder: TurboStreamer::WankelEncoder)
299
299
  TurboStreamer.encode(encoder: MyEncoder)
300
300
  ```
301
301
 
302
+ # Setting the default encoder and options
302
303
  If you need explicitly set the default:
303
304
 
304
305
  ```ruby
305
306
  TurboStreamer.set_default_encoder(:json, :oj)
306
307
  ```
307
308
 
309
+ You can also set default options to pass to the encoder if needed:
310
+
311
+ ```ruby
312
+ TurboStreamer.set_default_encoder(:json, :oj, buffer_size: 1_024)
313
+ ```
314
+
315
+ You may also just set the default options for an encoder:
316
+
317
+ ```ruby
318
+ TurboStreamer.set_default_encoder_options(:oj, buffer_size: 2_048)
319
+ ```
320
+
308
321
  The idea was to also support [MessagePack](http://msgpack.org/), hence requring
309
322
  the mime type when setting a default encoder.
310
323
 
@@ -1,17 +1,18 @@
1
1
  module ActionView
2
2
  class StreamingTemplateRenderer < TemplateRenderer
3
3
 
4
- def render_template(template, layout_name = nil, locals = {}) #:nodoc:
5
- return [super] unless layout_name && template.supports_streaming?
4
+ def render_template(view, template, layout_name = nil, locals = {}) #:nodoc:
5
+ template_supports_streaming = (layout_name && template.supports_streaming?) || template.handler == TurboStreamer::Handler
6
+ return [super.body] unless layout_name && template_supports_streaming
6
7
 
7
8
  locals ||= {}
8
9
  layout = layout_name && find_layout(layout_name, locals.keys, [formats.first])
9
10
 
10
11
  Body.new do |buffer|
11
12
  if template.handler == TurboStreamer::Handler
12
- delayed_render_json(buffer, template, layout, @view, locals)
13
+ delayed_render_json(buffer, template, layout, view, locals)
13
14
  else
14
- delayed_render(buffer, template, layout, @view, locals)
15
+ delayed_render(buffer, template, layout, view, locals)
15
16
  end
16
17
  end
17
18
  end
@@ -6,13 +6,13 @@ class TurboStreamer
6
6
 
7
7
  BLANK = ::Object.new
8
8
 
9
-
10
9
  ENCODERS = {
11
10
  json: {oj: 'Oj', wankel: 'Wankel'},
12
11
  msgpack: {msgpack: 'MessagePack'}
13
12
  }
14
13
 
15
14
  @@default_encoders = {}
15
+ @@encoder_options = Hash.new { |h, k| h[k] = {} }
16
16
  @@key_formatter = nil
17
17
 
18
18
  undef_method :==
@@ -24,15 +24,22 @@ class TurboStreamer
24
24
 
25
25
  def initialize(options = {})
26
26
  @output_buffer = options[:output_buffer] || ::StringIO.new
27
- @encoder = if options[:encoder].is_a?(Symbol)
28
- TurboStreamer.get_encoder(options[:mime] || :json, options[:encoder])
27
+ if options[:encoder].is_a?(Symbol)
28
+ @encoder = TurboStreamer.get_encoder(options[:mime] || :json, options[:encoder])
29
+ @encoder_options = @@encoder_options[options[:encoder]]
29
30
  elsif options[:encoder].nil?
30
- TurboStreamer.default_encoder_for(options[:mime] || :json)
31
+ @encoder = TurboStreamer.default_encoder_for(options[:mime] || :json)
32
+ if encoder_symbol = ENCODERS[options[:mime] || :json].find { |k, v| v == @encoder.name.delete_prefix('TurboStreamer::').delete_suffix('Encoder') }&.first
33
+ @encoder_options = @@encoder_options[encoder_symbol]
34
+ else
35
+ @encoder_options = {}
36
+ end
31
37
  else
32
- options[:encoder]
38
+ @encoder = options[:encoder]
39
+ @encoder_options = {}
33
40
  end
34
- @encoder = @encoder.new(@output_buffer)
35
41
 
42
+ @encoder = @encoder.new(@output_buffer, @encoder_options)
36
43
  @key_formatter = options.fetch(:key_formatter){ @@key_formatter ? @@key_formatter.clone : nil }
37
44
 
38
45
  yield self if ::Kernel.block_given?
@@ -221,12 +228,17 @@ class TurboStreamer
221
228
  @@key_formatter = formatter
222
229
  end
223
230
 
224
- def self.set_default_encoder(mime, encoder)
231
+ def self.set_default_encoder(mime, encoder, default_options={})
225
232
  if encoder.is_a?(Symbol)
226
233
  @@default_encoders[mime] = get_encoder(mime, encoder)
227
234
  else
228
235
  @@default_encoders[mime] = encoder
229
236
  end
237
+ @@encoder_options[encoder] = default_options
238
+ end
239
+
240
+ def self.set_default_encoder_options(encoder, options)
241
+ @@encoder_options[encoder] = options
230
242
  end
231
243
 
232
244
  def self.get_encoder(mime, key)
@@ -5,11 +5,13 @@ class TurboStreamer
5
5
 
6
6
  attr_reader :output
7
7
 
8
+ BUFFER_SIZE = 4096
9
+
8
10
  def initialize(io, options={})
9
11
  @stack = []
10
12
  @indexes = []
11
13
 
12
- @options = {mode: :json}.merge(options)
14
+ @options = {mode: :json, buffer_size: BUFFER_SIZE}.merge(options)
13
15
 
14
16
  @output = io
15
17
  @stream_writer = ::Oj::StreamWriter.new(io, @options)
@@ -12,9 +12,13 @@ class TurboStreamer
12
12
  true
13
13
  end
14
14
 
15
- def self.call(template)
15
+ # TODO: setting source=nil is for rails 5.x compatability, once unsppored
16
+ # source can be a required param and
17
+ # `source = template.source if source.nil?` can be removed
18
+ def self.call(template, source=nil)
19
+ source = template.source if source.nil?
16
20
  # this juggling is required to keep line numbers right in the error
17
- %{__already_defined = defined?(json); json||=TurboStreamer::Template.new(self, output_buffer: output_buffer || ActionView::OutputBuffer.new); #{template.source}
21
+ %{__already_defined = defined?(json); json||=TurboStreamer::Template.new(self, output_buffer: output_buffer || ActionView::OutputBuffer.new); #{source}
18
22
  json.target! unless (__already_defined && __already_defined != "method")}
19
23
  end
20
24
 
@@ -106,36 +106,58 @@ class TurboStreamer::Template < TurboStreamer
106
106
  # json.cache_if! !admin?, @person, expires_in: 10.minutes do
107
107
  # json.extract! @person, :name, :age
108
108
  # end
109
- def cache_if!(condition, *args)
110
- condition ? cache!(*args, &::Proc.new) : yield
109
+ def cache_if!(condition, *args, &block)
110
+ condition ? cache!(*args, &block) : yield
111
111
  end
112
112
 
113
113
  private
114
114
 
115
115
  def _render_partial_with_options(options)
116
- options.reverse_merge! locals: {}
116
+
117
117
  options.reverse_merge! ::TurboStreamer::Template.template_lookup_options
118
- as = options[:as]
118
+ as = options[:as]&.to_sym
119
+ options[:locals] ||= {}
120
+ options[:locals][:json] = self
119
121
 
120
122
  if as && options.key?(:collection)
121
- as = as.to_sym
122
- collection = options.delete(:collection)
123
- locals = options.delete(:locals)
124
- array! collection do |member|
125
- member_locals = locals.clone
126
- member_locals.merge! collection: collection
127
- member_locals.merge! as => member
128
- _render_partial options.merge(locals: member_locals)
129
- end
123
+ # Option 1, nice simple, fast, calls find_template once
124
+ array! { @context.render(options) }
125
+
126
+ # Option 2, the jBuilder way, slow because find_template for every item
127
+ # in the collection (a method which is known as one of the heaviest parts
128
+ # of Action View)
129
+ # as = as.to_sym
130
+ # collection = options.delete(:collection)
131
+ # locals = options.delete(:locals)
132
+ # array! collection do |member|
133
+ # member_locals = locals.clone
134
+ # member_locals.merge! collection: collection
135
+ # member_locals.merge! as => member
136
+ # _render_partial options.merge(locals: member_locals)
137
+ # end
138
+
139
+ # Option 3, the fastest, haven't looked into precisely why, but would need
140
+ # to customeize to the rails version
141
+ # lookup_context = @context.view_renderer.lookup_context
142
+ # options[:locals][:json] = self
143
+ # options[:locals][:collection] = options[:collection]
144
+ #
145
+ # pr = ActionView::PartialRenderer.new(lookup_context)
146
+ # pr.send(:setup, @context, options, as, nil)
147
+ # path = pr.instance_variable_get(:@path)
148
+ # a, b, c = pr.send(:retrieve_variable, path, as)
149
+ # template_keys = pr.send(:retrieve_template_keys, a).compact
150
+ # # + [:"#{a}__counter", :"#{a}_iteration"]
151
+ # template = pr.send(:find_partial, path, template_keys)
152
+ # locals = options[:locals]
153
+ # array! options[:collection] do |member|
154
+ # locals[as] = member
155
+ # template.render(@context, locals)
156
+ # end
130
157
  else
131
- _render_partial options
158
+ @context.render(options)
132
159
  end
133
160
  end
134
-
135
- def _render_partial(options)
136
- options[:locals].merge! json: self
137
- @context.render options
138
- end
139
161
 
140
162
  def _keys_to_collection_map(collection, options)
141
163
  key = options.delete(:key)
@@ -177,7 +199,10 @@ class TurboStreamer::Template < TurboStreamer
177
199
  name_options = options.slice(:skip_digest, :virtual_path)
178
200
  key = _fragment_name_with_digest(key, name_options)
179
201
 
180
- if @context.respond_to?(:fragment_cache_key)
202
+ if @context.respond_to?(:combined_fragment_cache_key)
203
+ key = @context.combined_fragment_cache_key(key)
204
+ elsif @context.respond_to?(:fragment_cache_key)
205
+ # TODO: remove after droping rails 5.1 support
181
206
  key = @context.fragment_cache_key(key)
182
207
  elsif ::Hash === key
183
208
  key = url_for(key).split('://', 2).last
@@ -199,16 +224,6 @@ class TurboStreamer::Template < TurboStreamer
199
224
  end
200
225
  end
201
226
 
202
- def _fragment_name_with_digest(key, options)
203
- if @context.respond_to?(:cache_fragment_name)
204
- # Current compatibility, fragment_name_with_digest is private again and cache_fragment_name
205
- # should be used instead.
206
- @context.cache_fragment_name(key, options)
207
- else
208
- key
209
- end
210
- end
211
-
212
227
  def _partial_options?(options)
213
228
  ::Hash === options && options.key?(:as) && options.key?(:partial)
214
229
  end
@@ -1,3 +1,3 @@
1
1
  class TurboStreamer
2
- VERSION = '1.3.0'
2
+ VERSION = '1.9.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbostreamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-22 00:00:00.000000000 Z
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: 5.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '2.0'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '2.0'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: mocha
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -192,7 +192,7 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
- description:
195
+ description:
196
196
  email:
197
197
  - jonbracy@gmail.com
198
198
  executables: []
@@ -217,7 +217,7 @@ homepage: https://github.com/malomalo/turbostreamer
217
217
  licenses:
218
218
  - MIT
219
219
  metadata: {}
220
- post_install_message:
220
+ post_install_message:
221
221
  rdoc_options:
222
222
  - "--main"
223
223
  - README.md
@@ -227,15 +227,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
227
227
  requirements:
228
228
  - - ">="
229
229
  - !ruby/object:Gem::Version
230
- version: '0'
230
+ version: '2.5'
231
231
  required_rubygems_version: !ruby/object:Gem::Requirement
232
232
  requirements:
233
233
  - - ">="
234
234
  - !ruby/object:Gem::Version
235
235
  version: '0'
236
236
  requirements: []
237
- rubygems_version: 3.0.1
238
- signing_key:
237
+ rubygems_version: 3.1.2
238
+ signing_key:
239
239
  specification_version: 4
240
240
  summary: Stream JSON via a Builder-style DSL
241
241
  test_files: []