hash-json-path 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/hash_json_path.rb +66 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dc1b3a10130434df62083a9617bb19bb77762f826fbb2009bda855da44b55b78
4
+ data.tar.gz: 6dedf5b04dcb23e06315a28044742c824ade710168ea924f70058e2a428b1123
5
+ SHA512:
6
+ metadata.gz: fd8780b9f0968f9ca20c4fe217991e17c9d92055bb2657acda7dc73221b22711f86b10398efbec8c26a9ca95cbcc4603d955e7026f3144778bf1a16e1a467acb
7
+ data.tar.gz: 5bae1063e81def147f6f5fa82acf57c18d41a8960c8a539bc507d951e567d470589af1b35524ba2f5a72830a5b57e956b98129604b9464d9825ffeff116d04dd
@@ -0,0 +1,66 @@
1
+ class HashJsonPath
2
+ SEPERATOR_REGEX = /[^\[|^\]]+/ # e.g. local_results[0][1] => ["local_results", "0", "1"]
3
+
4
+ def self.on(hash)
5
+ new(hash)
6
+ end
7
+
8
+ def initialize(hash, separator_regex = SEPERATOR_REGEX)
9
+ @hash = hash
10
+ @separator_regex = separator_regex
11
+ end
12
+
13
+ def use_separator_regex(separator_regex)
14
+ @separator_regex = separator_regex
15
+ self
16
+ end
17
+
18
+ def value
19
+ @hash
20
+ end
21
+
22
+ def get(path)
23
+ @hash.dig(*access_keys(path))
24
+ end
25
+
26
+ def safe_get(path)
27
+ get(@hash, path) rescue nil
28
+ end
29
+
30
+ def set(path, value)
31
+ raise "Empty path is not allowed" if path.nil? || path.empty?
32
+
33
+ *ancestors, leaf = access_keys(path)
34
+ tampered_hash = ancestors.empty? ? @hash : @hash.dig(*ancestors)
35
+ tampered_hash[leaf] = value
36
+ self
37
+ end
38
+
39
+ def merge(path, hash_value)
40
+ raise "Empty path is not allowed" if path.nil? || path.empty?
41
+ raise "Value must be a Hash" unless hash_value.is_a?(Hash)
42
+
43
+ *ancestors, leaf = access_keys(path)
44
+ tampered_hash = ancestors.empty? ? @hash : @hash.dig(*ancestors)
45
+ raise "Trying to merge a non hash value" unless tampered_hash[leaf].is_a?(Hash)
46
+ tampered_hash[leaf] = tampered_hash[leaf].merge(hash_value)
47
+ self
48
+ end
49
+
50
+ def prepend(path, hash_value)
51
+ raise "Empty path is not allowed" if path.nil? || path.empty?
52
+ raise "Value must be a Hash" unless hash_value.is_a?(Hash)
53
+
54
+ *ancestors, leaf = access_keys(path)
55
+ tampered_hash = ancestors.empty? ? @hash : @hash.dig(*ancestors)
56
+ raise "Trying to merge with a non hash value" unless tampered_hash[leaf].is_a?(Hash)
57
+ tampered_hash[leaf] = hash_value.merge(tampered_hash[leaf])
58
+ self
59
+ end
60
+
61
+ private
62
+
63
+ def access_keys(path)
64
+ path.scan(@separator_regex).map{|key| Integer(key) rescue key.to_sym }
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-json-path
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Terry Tan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: HashJsonPath is a simple gem to access hash and set hash value using
14
+ json path.
15
+ email:
16
+ - tanyongsheng0805@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/hash_json_path.rb
22
+ homepage: https://github.com/serpapi/hash-json-path
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: HashJsonPath is a simple gem to access hash and set hash value using json
45
+ path.
46
+ test_files: []