props_template 0.15.0 → 0.16.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: abee2de45e7fc076a1edad8c9d3edd9f13b3335e0946d6d227fcc4e5370a9e60
|
4
|
+
data.tar.gz: 20bb87744d490b3cae705a1514a965b07ec04cfdc99c5f7deafb627c00fb7484
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84b19f30e105c344ea37f0db00524804f998ba01ab84e0cd2f5c47e7c9324061096fd9b089df310a9cfc7a25980b72b01cb2c397649d3ce6ba24e9ca1fc7fd94
|
7
|
+
data.tar.gz: 07257d4609fb4ebbce6621dd804f87abe1b28ab336a41ea1d092611d973d3ea905134b5ad0d690ed9496c62758318825034b1f99b3668b09b3aa7046f49fc9c3
|
data/README.md
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
# PropsTemplate
|
2
2
|
|
3
|
-
PropsTemplate is a
|
3
|
+
PropsTemplate is a direct-to-Oj, JBuilder-like DSL for building JSON. It has support for Russian-Doll caching, layouts, and of course, its most unique feature: your templates are queryable.
|
4
|
+
|
5
|
+
PropsTemplate is fast!
|
6
|
+
|
7
|
+
Most libraries would build a hash before feeding it to your serializer of choice, typically Oj. PropsTemplate writes directly to Oj using `Oj::StringWriter` as its rendering your template and skips the need for an intermediate data structure.
|
8
|
+
|
9
|
+
PropsTemplate also improves caching. While other libraries spend time unmarshaling, merging, and then serializing to JSON; PropsTemplate simply takes the cached string and [push_json](http://www.ohler.com/oj/doc/Oj/StringWriter.html#push_json-instance_method).
|
10
|
+
|
4
11
|
|
5
12
|
Example:
|
6
13
|
|
@@ -54,24 +61,10 @@ gem 'props_template'
|
|
54
61
|
|
55
62
|
and run `bundle`
|
56
63
|
|
57
|
-
Then add the following to a initializer:
|
58
|
-
|
59
|
-
```
|
60
|
-
Props.reset_encoder!
|
61
|
-
```
|
62
|
-
|
63
|
-
PropsTemplate uses a single instance of `Oj::StringWriter` for a process. If you're using a forking server like puma, be sure to add this to your `config/puma.rb`.
|
64
|
-
|
65
|
-
```
|
66
|
-
on_worker_boot do
|
67
|
-
Props.reset_encoder!
|
68
|
-
end
|
69
|
-
```
|
70
|
-
|
71
64
|
## API
|
72
65
|
|
73
66
|
### json.set! or json.<your key here>
|
74
|
-
Defines the attribute or stucture. All keys are automatically camelized.
|
67
|
+
Defines the attribute or stucture. All keys are automatically camelized lower.
|
75
68
|
|
76
69
|
```ruby
|
77
70
|
json.set! :author_details, {..options...} do
|
data/lib/props_template/base.rb
CHANGED
@@ -6,20 +6,10 @@ module Props
|
|
6
6
|
class InvalidScopeForArrayError < StandardError; end
|
7
7
|
class InvalidScopeForObjError < StandardError; end
|
8
8
|
|
9
|
-
def self.reset_encoder!
|
10
|
-
@@encoder = Oj::StringWriter.new(mode: :rails)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.encoder
|
14
|
-
@@encoder
|
15
|
-
end
|
16
|
-
|
17
9
|
class Base
|
18
10
|
def initialize(encoder = nil)
|
19
|
-
@stream =
|
20
|
-
@stream.reset
|
11
|
+
@stream = Oj::StringWriter.new(mode: :rails)
|
21
12
|
@scope = nil
|
22
|
-
@key_cache = {}
|
23
13
|
end
|
24
14
|
|
25
15
|
def set_block_content!(options = {})
|
@@ -38,9 +28,12 @@ module Props
|
|
38
28
|
end
|
39
29
|
end
|
40
30
|
|
31
|
+
def format_key(key)
|
32
|
+
key.to_s
|
33
|
+
end
|
34
|
+
|
41
35
|
def set!(key, value = nil)
|
42
|
-
|
43
|
-
key = @key_cache[key]
|
36
|
+
key = format_key(key)
|
44
37
|
|
45
38
|
if @scope == :array
|
46
39
|
raise InvalidScopeForObjError.new('Attempted to set! on an array! scope')
|
@@ -120,10 +113,9 @@ module Props
|
|
120
113
|
@stream.pop
|
121
114
|
end
|
122
115
|
|
123
|
-
json = @stream.
|
116
|
+
json = @stream.raw_json
|
124
117
|
@scope = nil
|
125
118
|
@key_cache = {}
|
126
|
-
@stream.reset
|
127
119
|
json
|
128
120
|
end
|
129
121
|
end
|
@@ -3,8 +3,8 @@ require 'props_template/extensions/partial_renderer'
|
|
3
3
|
require 'props_template/extensions/cache'
|
4
4
|
require 'props_template/extensions/deferment'
|
5
5
|
require 'props_template/extension_manager'
|
6
|
-
require 'props_template/key_formatter'
|
7
6
|
require 'active_support/core_ext/string/output_safety'
|
7
|
+
require 'active_support/core_ext/array'
|
8
8
|
|
9
9
|
module Props
|
10
10
|
class BaseWithExtensions < Base
|
@@ -16,7 +16,7 @@ module Props
|
|
16
16
|
#todo: refactor so deferred can be its own class
|
17
17
|
@em = ExtensionManager.new(self)
|
18
18
|
@traveled_path = []
|
19
|
-
@
|
19
|
+
@key_cache = {}
|
20
20
|
super()
|
21
21
|
end
|
22
22
|
|
@@ -52,9 +52,12 @@ module Props
|
|
52
52
|
@em = ExtensionManager.new(self, prev_state[1], prev_state[2])
|
53
53
|
end
|
54
54
|
|
55
|
-
def
|
56
|
-
key
|
55
|
+
def format_key(key)
|
56
|
+
@key_cache[key] ||= key.to_s.camelize(:lower)
|
57
|
+
@key_cache[key].dup
|
58
|
+
end
|
57
59
|
|
60
|
+
def set!(key, options = {}, &block)
|
58
61
|
if block_given?
|
59
62
|
options = @em.refine_options(options)
|
60
63
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
require "concurrent/map"
|
2
1
|
require 'action_view'
|
3
2
|
|
4
3
|
module Props
|
@@ -81,7 +80,6 @@ module Props
|
|
81
80
|
raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path))
|
82
81
|
end
|
83
82
|
|
84
|
-
|
85
83
|
def self.retrieve_variable(path)
|
86
84
|
base = path[-1] == "/" ? "" : File.basename(path)
|
87
85
|
raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
|
data/spec/props_template_spec.rb
CHANGED
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.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johny Ho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.9'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: concurrent-ruby
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '1.1'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '1.1'
|
69
55
|
description: A JSON builder for your React props
|
70
56
|
email: jho406@gmail.com
|
71
57
|
executables: []
|
@@ -85,7 +71,6 @@ files:
|
|
85
71
|
- lib/props_template/extensions/fragment.rb
|
86
72
|
- lib/props_template/extensions/partial_renderer.rb
|
87
73
|
- lib/props_template/handler.rb
|
88
|
-
- lib/props_template/key_formatter.rb
|
89
74
|
- lib/props_template/layout_patch.rb
|
90
75
|
- lib/props_template/railtie.rb
|
91
76
|
- lib/props_template/searcher.rb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/array'
|
2
|
-
|
3
|
-
module Props
|
4
|
-
class KeyFormatter
|
5
|
-
def initialize(*args)
|
6
|
-
@format = {}
|
7
|
-
@cache = {}
|
8
|
-
|
9
|
-
options = args.extract_options!
|
10
|
-
args.each do |name|
|
11
|
-
@format[name] = []
|
12
|
-
end
|
13
|
-
options.each do |name, parameters|
|
14
|
-
@format[name] = parameters
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def initialize_copy(original)
|
19
|
-
@cache = {}
|
20
|
-
end
|
21
|
-
|
22
|
-
def format(key)
|
23
|
-
@cache[key] ||= @format.inject(key.to_s) do |result, args|
|
24
|
-
func, args = args
|
25
|
-
if ::Proc === func
|
26
|
-
func.call result, *args
|
27
|
-
else
|
28
|
-
result.send func, *args
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|