vine 0.3 → 0.4

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/vine.rb +28 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ab5549cbdfb3282b77fcd7bc18cb5035f52909c
4
- data.tar.gz: 24fb8a2532e044029b5f9e8802c73480109fe2cd
3
+ metadata.gz: 1fa94ad02bba2c38a0b59973a228a1b1e2e93e73
4
+ data.tar.gz: 7b46cab730077054f6c60a971b4e83a29ad8f278
5
5
  SHA512:
6
- metadata.gz: dc3085e2433fb2f6730ea39f9ef698e1bebaf717a165dce4e86027e050559c8808f9a430fc9a6e1aec1e7a97a21ad4a96261799080b8dc9bbbd77e087100eff0
7
- data.tar.gz: 7414671d963b2dded3364013c10afea3214cbc66e3a904835dc4261dd0f6eba33d26dd8fe3cde9fda544ad754a01655147814d294fea61e1d179e07208e6a18a
6
+ metadata.gz: a035fd5298a4f365470747797d02ad26752c9989de4965489de0c39a538efe4b74fa32028964f91c49b97fdf0e0f777e3535f8a55553ee7dbc427d7b8f59c2dd
7
+ data.tar.gz: ed2493896073cb7d91b377a9f83b01bc79b4d706d4af69c01ffadb06c3040d05d19068ed95ac64d487f3c64ebb349491ed0f9e8ecc9a1264465030f981a97bc3
@@ -26,6 +26,34 @@ class Hash
26
26
  value
27
27
  end
28
28
 
29
+ def set(path, value)
30
+ keys = []
31
+ path = path.to_s.split('.')
32
+
33
+ [*path, nil].each_cons(2) do |key,nextkey|
34
+ tmp_hash = keys.inject(self, :fetch)
35
+
36
+ if key.to_i.to_s == key
37
+ key = key.to_i
38
+ elsif tmp_hash[key].nil?
39
+ key = key.to_sym
40
+ end
41
+
42
+ if tmp_hash[key].nil?
43
+ tmp_hash[key] = {}
44
+ elsif nextkey.to_i.to_s == nextkey && !tmp_hash[key].is_a?(Array)
45
+ tmp_hash[key] = []
46
+ elsif !nextkey.nil? && !tmp_hash[key].is_a?(Hash)
47
+ tmp_hash[key] = {}
48
+ end
49
+
50
+ keys << key
51
+ end # each_cons
52
+
53
+ last_key = keys.pop
54
+ keys.inject(self, :fetch)[last_key] = value
55
+ end
56
+
29
57
  alias :vine :access
30
58
 
31
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vine
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cheng Guangnan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-21 00:00:00.000000000 Z
11
+ date: 2016-12-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Access nested Hash (JSON) with a string path
14
14
  email: chengguangnan@icloud.com