props_template 1.1.0 → 1.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3dc53f3b47d33c98b7fc04e82ef1bcc711529a30c5631627ba2bbd55eeb47ded
4
- data.tar.gz: 4c13c7771bfc1ef0d2a2848b0c24d03f029f47751625f18151c23702d0a42376
3
+ metadata.gz: 4b2f7cf3a4eca0bead0971f22d1f75de65d3e365c2684d9055b80c6310b07df3
4
+ data.tar.gz: bd190fb9a3581b9f056eef6512f2ad6c7331b03f033553a4c236dc10effcac76
5
5
  SHA512:
6
- metadata.gz: c08a1f263224c6b4902ac849d7bded7b8e22d4520f3520a82cf7eac231f645ee9109dc8ac95649e2b8f5695c369f71868687482802113712daa3b18cd261bfbe
7
- data.tar.gz: 6b3b68733f7b740be73438aff7602d30919798aa2d7693bc36437b096ff1e88a4414d4e93618ca90a74627897f96bc8ec781f0e84718b79a5ac925096a829900
6
+ metadata.gz: 421bcf7b29a5f7ce4a21ce99a39db7b243f86eac63ae3cdb6d513745cd91f2bd830a72c360826048a7fafd03dff293bcc4024311e053f9a330a5880212c0439e
7
+ data.tar.gz: 4efdc30e4b36fdc382b75936625b7ae233ea065bf7c1f632e2e943730c28ea8f9b4e3b8d58a28a788d67102995948b7fffac61042b9ce4479e068435e952d935
@@ -1,5 +1,6 @@
1
1
  require "oj"
2
2
  require "active_support"
3
+ require "props_template/raw_json"
3
4
 
4
5
  module Props
5
6
  class InvalidScopeForArrayError < StandardError; end
@@ -11,8 +12,10 @@ module Props
11
12
  class Base
12
13
  attr_accessor :item_context
13
14
 
15
+ WRITER_OPTIONS = {mode: :rails, use_raw_json: true}.freeze
16
+
14
17
  def initialize(encoder = nil)
15
- @stream = Oj::StringWriter.new(mode: :rails)
18
+ @stream = Oj::StringWriter.new(WRITER_OPTIONS)
16
19
  @scope = nil
17
20
  @item_context = nil
18
21
  end
@@ -168,6 +171,20 @@ module Props
168
171
  @child_index = child_index
169
172
  end
170
173
 
174
+ def capture_json
175
+ parent_stream = @stream
176
+ @stream = Oj::StringWriter.new(WRITER_OPTIONS)
177
+ yield
178
+ @stream.raw_json.strip
179
+ ensure
180
+ @stream = parent_stream
181
+ end
182
+
183
+ # Adds already-encoded JSON, such as a cached fragment, as the next value.
184
+ def push_cached_json(json)
185
+ @stream.push_value(RawJSON.new(json))
186
+ end
187
+
171
188
  def result!
172
189
  if @scope.nil?
173
190
  @stream.push_object
@@ -1,5 +1,9 @@
1
1
  module Props
2
2
  class ExtensionManager
3
+ # A cached fragment is stored after a line of the deferred and fragment
4
+ # paths inside it, which is this when there are none.
5
+ EMPTY_META_LINE = "[[],[]]\n".freeze
6
+
3
7
  attr_reader :base, :builder, :context, :cache, :partialer
4
8
 
5
9
  delegate :load_cache, to: :cache
@@ -69,36 +73,35 @@ module Props
69
73
  private
70
74
 
71
75
  def handle_cache(options, item)
72
- if options[:cache]
73
- recently_cached = false
74
-
76
+ if options[:cache] && @cache.enabled?
75
77
  key, rest = Cache
76
78
  .normalize_options(options[:cache], item)
79
+
80
+ # A miss renders into a writer of its own, so its JSON is added to
81
+ # the output below like a hit's.
77
82
  state = @cache.cache(key, rest) do
78
- recently_cached = true
79
83
  result = nil
80
- start = base.stream.to_s.length
81
84
  base.scoped_state { |stream, deferred_paths, fragment_paths|
82
- yield
85
+ raw = base.capture_json { yield }
83
86
  meta = Oj.dump([deferred_paths, fragment_paths]).strip
84
- json_in_progress = base.stream.to_s
85
- if json_in_progress[start] == ","
86
- start += 1
87
- end
88
- raw = base.stream.to_s[start..].strip
89
87
  result = "#{meta}\n#{raw}"
90
88
  }
91
89
  result
92
90
  end
93
91
 
94
- meta, raw_json = state.split("\n", 2)
95
- next_deferred, next_fragments = Oj.load(meta)
96
- deferred.push(*next_deferred)
97
- fragments.push(*next_fragments)
98
-
99
- if !recently_cached
100
- base.stream.push_json(raw_json)
92
+ # Nearly every fragment has no deferred or fragment paths, so the
93
+ # empty line is skipped without being split off or parsed.
94
+ if state.start_with?(EMPTY_META_LINE)
95
+ raw_json = state.byteslice(EMPTY_META_LINE.bytesize..)
96
+ else
97
+ separator = state.byteindex("\n")
98
+ next_deferred, next_fragments = Oj.load(state.byteslice(0, separator))
99
+ deferred.push(*next_deferred)
100
+ fragments.push(*next_fragments)
101
+ raw_json = state.byteslice((separator + 1)..)
101
102
  end
103
+
104
+ base.stream.push_value(RawJSON.new(raw_json))
102
105
  else
103
106
  yield
104
107
  end
@@ -56,15 +56,21 @@ module Props
56
56
  result
57
57
  end
58
58
 
59
+ def enabled?
60
+ controller.perform_caching
61
+ end
62
+
59
63
  def load_cache(keys, options = {})
60
- @results = results.merge multi_fetch(keys, options)
64
+ if enabled?
65
+ @results = results.merge multi_fetch(keys, options)
66
+ end
61
67
  end
62
68
 
63
69
  # The below was copied from the wonderful jbuilder library Its also MIT
64
70
  # licensed, so no issues there. Thanks to the jbuilder authors!
65
71
 
66
72
  def cache(key = nil, options = {})
67
- if controller.perform_caching
73
+ if enabled?
68
74
  cache_fragment_for(key, options) do
69
75
  yield
70
76
  end
@@ -0,0 +1,15 @@
1
+ module Props
2
+ # Already-encoded JSON, such as a cached fragment, for Oj to insert as is.
3
+ # Oj calls raw_json on values when its use_raw_json option is on, and
4
+ # takes the length from the String rather than scanning for it as
5
+ # push_json does.
6
+ class RawJSON
7
+ def initialize(json)
8
+ @json = json
9
+ end
10
+
11
+ def raw_json(depth, indent)
12
+ @json
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Props
2
- VERSION = "1.1.0".freeze
2
+ VERSION = "1.2.0".freeze
3
3
  end
@@ -17,6 +17,12 @@ require "active_support"
17
17
 
18
18
  module Props
19
19
  class Template
20
+ GENERATED_KEY_METHODS = Module.new
21
+ include GENERATED_KEY_METHODS
22
+
23
+ GENERATABLE_KEY_REGEXP = /\A[a-zA-Z_]\w*[!?]?\z/
24
+ GENERATED_KEY_METHODS_LOCK = Mutex.new
25
+
20
26
  class << self
21
27
  attr_accessor :template_lookup_options
22
28
  end
@@ -93,8 +99,30 @@ module Props
93
99
  @builder
94
100
  end
95
101
 
96
- alias_method :method_missing, :set!
97
- private :method_missing
102
+ def self.generate_key_method(key)
103
+ GENERATED_KEY_METHODS_LOCK.synchronize do
104
+ name = key.name
105
+ already_implemented = method_defined?(key) || private_method_defined?(key)
106
+
107
+ if !already_implemented && GENERATABLE_KEY_REGEXP.match?(name)
108
+ GENERATED_KEY_METHODS.module_eval <<~RUBY, __FILE__, __LINE__ + 1
109
+ def #{name}(options = {}, &block)
110
+ set!(#{key.inspect}, options, &block)
111
+ end
112
+ RUBY
113
+ end
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ # No respond_to_missing?: a builder accepts any key, but claiming to
120
+ # respond to everything would answer yes to probes like to_ary or to_hash.
121
+ # Generated methods are real methods, so respond_to? sees those already.
122
+ def method_missing(key, ...) # standard:disable Style/MissingRespondToMissing
123
+ self.class.generate_key_method(key)
124
+ set!(key, ...)
125
+ end
98
126
  end
99
127
  end
100
128
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: props_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
@@ -92,6 +92,7 @@ files:
92
92
  - lib/props_template/options.rb
93
93
  - lib/props_template/partial_patch.rb
94
94
  - lib/props_template/railtie.rb
95
+ - lib/props_template/raw_json.rb
95
96
  - lib/props_template/searcher.rb
96
97
  - lib/props_template/version.rb
97
98
  homepage: https://github.com/thoughtbot/props_template/