jsonpath 0.9.3 → 0.9.4

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
- SHA256:
3
- metadata.gz: '048116fe74d7b281fbcf58d8f5d1aa44c359267bf179e58376527032e58c0094'
4
- data.tar.gz: 4d91c8953e2750322d486f372b165507c4f383a42c11d07987cc49630abea5b6
2
+ SHA1:
3
+ metadata.gz: e2473ca28bed27ee404a8ad46a73e4752f848f29
4
+ data.tar.gz: 647e15073ba118cd82ebae0d21a4d7fb056600f8
5
5
  SHA512:
6
- metadata.gz: fb899c515175b16b0f9c1001ae7525f14d086b4bf844779cef819c9e634511445c0ee1aa033c7e51c9419aa1de1e22ae8e4670e7fbaa162b27d5c819ebef0846
7
- data.tar.gz: d303bb0188617e52133250571c43c0262b02cd1f586edb2d05683f30e6e26e17dfaeb81a3a62e40c4b838374e9b9354fbf012d03591fa7fb465549ff9148ddd8
6
+ metadata.gz: ce6cde76b1d9ccb25bbd83d08d28ff8be128555cde22c1cb64a36d5f296d3dd9b6196ea0fd359f7f6ca94b11ddde46728291752c6a733123677dae128fdd694e
7
+ data.tar.gz: 88052cdd548064d68e4cc6a384214dc1712b8661ae509782b2f38b9cbda61b37753938db50b055022629d77821a0669f5e39759138409da61c544fb365be22b2
data/lib/jsonpath.rb CHANGED
@@ -10,7 +10,7 @@ require 'jsonpath/parser'
10
10
  # JsonPath: initializes the class with a given JsonPath and parses that path
11
11
  # into a token array.
12
12
  class JsonPath
13
- PATH_ALL = '$..*'
13
+ PATH_ALL = '$..*'.freeze
14
14
 
15
15
  attr_accessor :path
16
16
 
@@ -77,8 +77,6 @@ class JsonPath
77
77
  when Array
78
78
  node.size.times do |index|
79
79
  @_current_node = node[index]
80
- # exps = sub_path[1, sub_path.size - 1]
81
- # if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]")
82
80
  if process_function_or_literal(sub_path[1, sub_path.size - 1])
83
81
  each(@_current_node, nil, pos + 1, &blk)
84
82
  end
@@ -104,6 +102,8 @@ class JsonPath
104
102
  when :delete
105
103
  if key
106
104
  key.is_a?(Integer) ? context.delete_at(key) : context.delete(key)
105
+ else
106
+ context.replace({})
107
107
  end
108
108
  when :substitute
109
109
  if key
@@ -46,9 +46,19 @@ class JsonPath
46
46
 
47
47
  def _delete(obj, path)
48
48
  JsonPath.new(path)[obj, :delete].each
49
+ obj = _remove(obj)
49
50
  Proxy.new(obj)
50
51
  end
51
52
 
53
+ def _remove(obj)
54
+ obj.each do |o|
55
+ if o.is_a?(Hash) || o.is_a?(Array)
56
+ _remove(o)
57
+ o.delete({})
58
+ end
59
+ end
60
+ end
61
+
52
62
  def _compact(obj, path)
53
63
  JsonPath.new(path)[obj, :compact].each
54
64
  Proxy.new(obj)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JsonPath
4
- VERSION = '0.9.3'
4
+ VERSION = '0.9.4'.freeze
5
5
  end
@@ -173,6 +173,89 @@ class TestJsonpath < MiniTest::Unit::TestCase
173
173
  assert_equal({ 'you' => nil }, h)
174
174
  end
175
175
 
176
+ def test_delete_2
177
+ json = { 'store' => {
178
+ 'book' => [
179
+ { 'category' => 'reference',
180
+ 'author' => 'Nigel Rees',
181
+ 'title' => 'Sayings of the Century',
182
+ 'price' => 9,
183
+ 'tags' => %w[asdf asdf2] },
184
+ { 'category' => 'fiction',
185
+ 'author' => 'Evelyn Waugh',
186
+ 'title' => 'Sword of Honour',
187
+ 'price' => 13 },
188
+ { 'category' => 'fiction',
189
+ 'author' => 'Aasdf',
190
+ 'title' => 'Aaasdf2',
191
+ 'price' => 1 }
192
+ ]
193
+ } }
194
+ json_deleted = { 'store' => {
195
+ 'book' => [
196
+ { 'category' => 'fiction',
197
+ 'author' => 'Evelyn Waugh',
198
+ 'title' => 'Sword of Honour',
199
+ 'price' => 13 },
200
+ { 'category' => 'fiction',
201
+ 'author' => 'Aasdf',
202
+ 'title' => 'Aaasdf2',
203
+ 'price' => 1 }
204
+ ]
205
+ } }
206
+ assert_equal(json_deleted, JsonPath.for(json).delete("$..store.book[?(@.category == 'reference')]").obj)
207
+ end
208
+
209
+ def test_delete_3
210
+ json = { 'store' => {
211
+ 'book' => [
212
+ { 'category' => 'reference',
213
+ 'author' => 'Nigel Rees',
214
+ 'title' => 'Sayings of the Century',
215
+ 'price' => 9,
216
+ 'tags' => %w[asdf asdf2],
217
+ 'this' => {
218
+ 'delete_me' => [
219
+ 'no' => 'do not'
220
+ ]
221
+ }
222
+ },
223
+ { 'category' => 'fiction',
224
+ 'author' => 'Evelyn Waugh',
225
+ 'title' => 'Sword of Honour',
226
+ 'price' => 13
227
+ },
228
+ { 'category' => 'fiction',
229
+ 'author' => 'Aasdf',
230
+ 'title' => 'Aaasdf2',
231
+ 'price' => 1
232
+ }
233
+ ]
234
+ } }
235
+ json_deleted = { 'store' => {
236
+ 'book' => [
237
+ { 'category' => 'reference',
238
+ 'author' => 'Nigel Rees',
239
+ 'title' => 'Sayings of the Century',
240
+ 'price' => 9,
241
+ 'tags' => %w[asdf asdf2],
242
+ 'this' => {}
243
+ },
244
+ { 'category' => 'fiction',
245
+ 'author' => 'Evelyn Waugh',
246
+ 'title' => 'Sword of Honour',
247
+ 'price' => 13
248
+ },
249
+ { 'category' => 'fiction',
250
+ 'author' => 'Aasdf',
251
+ 'title' => 'Aaasdf2',
252
+ 'price' => 1
253
+ }
254
+ ]
255
+ } }
256
+ assert_equal(json_deleted, JsonPath.for(json).delete('$..store.book..delete_me').obj)
257
+ end
258
+
176
259
  def test_delete_for_array
177
260
  before = JsonPath.on(@object, '$..store.book[1]')
178
261
  JsonPath.for(@object).delete!('$..store.book[0]')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ version: 0.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-07-08 00:00:00.000000000 Z
12
+ date: 2018-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  requirements: []
161
161
  rubyforge_project: jsonpath
162
- rubygems_version: 2.7.3
162
+ rubygems_version: 2.6.13
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Ruby implementation of http://goessner.net/articles/JsonPath/