props_template 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,209 @@
1
+ require_relative './support/helper'
2
+
3
+ RSpec.describe 'Searcher' do
4
+ it 'searching for a child node returns the proc, and refined found options' do
5
+ json = Props::Searcher.new(nil, ['outer', 'inner', 'hit'])
6
+ target_proc = Proc.new {}
7
+ target_opts = {some_options: 1}
8
+ json.set!('outer') do
9
+ json.set!('inner') do
10
+ json.set!('hit', target_opts, &target_proc)
11
+ end
12
+ end
13
+
14
+ found_block, found_options = json.found!
15
+ expect(found_block).to eql(target_proc)
16
+ expect(found_options).to eql({
17
+ some_options: 1,
18
+ path_suffix: ['inner', 'hit']
19
+ })
20
+ end
21
+
22
+ it 'searching with an empty path means you found nothing' do
23
+ json = Props::Searcher.new(nil, [])
24
+
25
+ target_proc = Proc.new do
26
+ json.set!('inner') do
27
+ json.set!('hit', {}, &target_proc)
28
+ end
29
+ end
30
+ json.set!('outer', {}, &target_proc)
31
+
32
+ found_block, found_options = json.found!
33
+ expect(found_block).to be_nil
34
+ end
35
+
36
+ it 'searching for a child node with siblings in back' do
37
+ json = Props::Searcher.new(nil, ['outer', 'inner'])
38
+ target_proc = Proc.new do
39
+ json.set!('foo', 32)
40
+ end
41
+
42
+ json.set!('outer') do
43
+ json.set!('inner', {}, &target_proc)
44
+
45
+ json.set!('bad') do
46
+ json.set!('foo', 'should not touch')
47
+ end
48
+ end
49
+
50
+ found_block, found_options = json.found!
51
+ expect(found_block).to eql(target_proc)
52
+ end
53
+
54
+ it 'searching for a child node with siblings in front' do
55
+ json = Props::Searcher.new(nil, ['outer', 'inner'])
56
+ target_proc = Proc.new do
57
+ json.foo 32
58
+ end
59
+
60
+ json.set!('outer') do
61
+ json.set!('bad') do
62
+ json.set!('foo', 'should not touch')
63
+ end
64
+
65
+ json.set!('inner', {}, &target_proc)
66
+ end
67
+
68
+ found_block, found_options = json.found!
69
+ expect(found_block).to eql(target_proc)
70
+ end
71
+
72
+ it 'searching for a subtree' do
73
+ json = Props::Searcher.new(nil, ['outer', 'inner', 'deep'])
74
+
75
+ target_proc = Proc.new do
76
+ json.set!('deeper') do
77
+ json.set!('foo', 32)
78
+ end
79
+ end
80
+
81
+ json.set!('outer') do
82
+ json.set!('inner') do
83
+ json.set!('deep', {}, &target_proc)
84
+ end
85
+ end
86
+
87
+ found_block, found_options = json.found!
88
+ expect(found_block).to eql(target_proc)
89
+ end
90
+
91
+ it 'searching for a leaf node is unsupported' do
92
+ json = Props::Searcher.new(nil, ['outer', 'inner', 'foo'])
93
+ json.set!('outer') do
94
+ json.set!('inner') do
95
+ json.set!('foo', 32)
96
+ end
97
+ end
98
+
99
+ found_block, found_options = json.found!
100
+ expect(found_block).to be_nil
101
+ end
102
+
103
+ it 'searching for a node beyond whats available is equivalent to not finding anything' do
104
+ json = Props::Searcher.new(nil, ['outer', 'inner', 'a', 'b'])
105
+ json.set!('outer') do
106
+ json.set!('inner') do
107
+ json.set!('foo', 32)
108
+ end
109
+ end
110
+
111
+ found_block, found_options = json.found!
112
+ expect(found_block).to be_nil
113
+ end
114
+
115
+ it 'searching for an item inside an array, includes refined found options' do
116
+ json = Props::Searcher.new(nil, ['outer', 1])
117
+ target_opts = {some_options: 1}
118
+ json.set!('outer') do
119
+ json.array!(['hello', 'world'], target_opts) do |item|
120
+ item
121
+ end
122
+ end
123
+
124
+ found_block, found_options = json.found!
125
+ expect(found_options).to eql({some_options: 1, path_suffix: [1]})
126
+ expect(found_block.call).to eql('world')
127
+ end
128
+
129
+ it 'searching for an item inside an array using an id=val keypath' do
130
+ json = Props::Searcher.new(nil, ['outer', 'id=1'])
131
+
132
+ class Collection
133
+ def initialize(ary, rspec)
134
+ @ary = ary
135
+ @rspec = rspec
136
+ end
137
+
138
+ def member_by(key, value)
139
+ @rspec.expect(key).to @rspec.eql('id')
140
+ @rspec.expect(value).to @rspec.eql(1)
141
+ @ary.last
142
+ end
143
+ end
144
+
145
+ collection = Collection.new(
146
+ [{id: 0}, {id: 1}],
147
+ self
148
+ )
149
+
150
+ json.set!('outer') do
151
+ json.array!(collection) do |item|
152
+ item
153
+ end
154
+ end
155
+
156
+ found_block, found_options = json.found!
157
+ expect(found_block.call).to eql({id: 1})
158
+ end
159
+
160
+ it 'searching for an node belonging an array' do
161
+ json = Props::Searcher.new(nil, ['outer', 'inner', 1, 'foo'])
162
+ json.set!('outer') do
163
+ json.set!('inner') do
164
+ json.array! ['hello', 'world'] do |item|
165
+ json.set!('foo') do
166
+ item
167
+ end
168
+ end
169
+ end
170
+ end
171
+ found_block, found_options = json.found!
172
+ expect(found_block.call).to eql('world')
173
+ end
174
+
175
+ it 'searching for an node outside the length of the array, is equivalent to not finding anything' do
176
+ json = Props::Searcher.new(nil, ['outer','inner', 10, 'foo'])
177
+ json.set!('outer') do
178
+ json.set!('inner') do
179
+ json.array! [1, 2] do |item|
180
+ json.set!('foo') do
181
+ json.set!('bar', item)
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ found_block, found_options = json.found!
188
+ expect(found_block).to be_nil
189
+ end
190
+
191
+ it 'searching for object inside a nested array' do
192
+ json = Props::Searcher.new(nil, ['outer', 'inner', 1, 'foo', 0])
193
+ json.set!('outer') do
194
+ json.set!('inner') do
195
+ json.array! [0, 1] do |item|
196
+ json.set!('foo') do
197
+ json.array! ['hello', 'world'] do |inner_item|
198
+ inner_item
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+
205
+ found_block, found_options = json.found!
206
+ expect(found_block.call).to eql('hello')
207
+ end
208
+ end
209
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: props_template
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.13.0
5
+ platform: ruby
6
+ authors:
7
+ - Johny Ho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: oj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
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
+ description: A JSON builder for your React props
70
+ email: jho406@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/props_template.rb
76
+ - lib/props_template/base.rb
77
+ - lib/props_template/base_with_extensions.rb
78
+ - lib/props_template/core_ext.rb
79
+ - lib/props_template/dependency_tracker.rb
80
+ - lib/props_template/extension_manager.rb
81
+ - lib/props_template/extensions/cache.rb
82
+ - lib/props_template/extensions/deferment.rb
83
+ - lib/props_template/extensions/fragment.rb
84
+ - lib/props_template/extensions/partial_renderer.rb
85
+ - lib/props_template/handler.rb
86
+ - lib/props_template/key_formatter.rb
87
+ - lib/props_template/layout_patch.rb
88
+ - lib/props_template/railtie.rb
89
+ - lib/props_template/searcher.rb
90
+ - spec/layout_spec.rb
91
+ - spec/props_template_spec.rb
92
+ - spec/searcher_spec.rb
93
+ homepage: https://github.com/jho406/breezy/
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '2.3'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.0.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: A JSON builder for your React props
116
+ test_files:
117
+ - spec/searcher_spec.rb
118
+ - spec/layout_spec.rb
119
+ - spec/props_template_spec.rb