turbostreamer 1.5.0 → 1.7.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: 41cf2cabe5c4084620fea525158ec975f1d5d7785b2416aff205c6e97c6ff77f
4
- data.tar.gz: 112322a9faa00a0e70e250222dbbdc3ec756879c886b4f448493802ddbc46d88
3
+ metadata.gz: f88c7e8d509e97aa6d2d2604f4a36c1e2d4cc5bb6b15393390681b93d570a893
4
+ data.tar.gz: 454ca8f3167263308d397fc66ffaafc3858e4078fed5109a325932c8c890c664
5
5
  SHA512:
6
- metadata.gz: b51fcf37f22a606e5a7dc072706e9e32468c3e92b5f1b9a2d6adf2102644a680ace31baef8486ac850675ff20182db4cd40e07c92cebaded6faaab2ac98a7bae
7
- data.tar.gz: 81706cdc8ec9808cc573d6bb3d871977e0edc22744daa15b1d1c3811c7819b0181971e5553cc7ed416a2052b7bb92267ff1f093a79d9a5e07e69c0873a7486be
6
+ metadata.gz: 225cfb941767f0f3c45d0b1458aaabd9815f121af909e35534a40f3a71502297ea0dd1b72ca943f0f8d7fb11944ccea64283668d86b3e34c977958e1170edb46
7
+ data.tar.gz: 22ec94ce3cd8e5e1b81c7373d021297e66bedaadbc7fb780c765cf0c7ff752447b9e13e7de84ec5e114620de3af598d225331a7303ec06d31dc78191971c2295
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
 
@@ -2,7 +2,8 @@ module ActionView
2
2
  class StreamingTemplateRenderer < TemplateRenderer
3
3
 
4
4
  def render_template(template, layout_name = nil, locals = {}) #:nodoc:
5
- return [super] unless layout_name && template.supports_streaming?
5
+ template_supports_streaming = (layout_name && template.supports_streaming?) || template.handler == TurboStreamer::Handler
6
+ return [super] unless template_supports_streaming
6
7
 
7
8
  locals ||= {}
8
9
  layout = layout_name && find_layout(layout_name, locals.keys, [formats.first])
@@ -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)
@@ -113,29 +113,51 @@ class TurboStreamer::Template < TurboStreamer
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)
@@ -1,3 +1,3 @@
1
1
  class TurboStreamer
2
- VERSION = '1.5.0'
2
+ VERSION = '1.7.0'
3
3
  end
data/lib/turbostreamer.rb CHANGED
@@ -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)
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.5.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-18 00:00:00.000000000 Z
11
+ date: 2019-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -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