i_hash 0.0.1

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/i_hash.rb +72 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd01d8784280e3f3d7bc99f065a79fb53dad1710
4
+ data.tar.gz: ca60737d48a9eeb8d53b6be27c2bc8790b717354
5
+ SHA512:
6
+ metadata.gz: 221e1cdf183a7ce71248f5175708c9d959a5ca0e4fac11fbbc0c536c6ccc3707375a02e275ec5df3561994485e776ba0ad19808a77a42532f1243f531319ad1f
7
+ data.tar.gz: 3eeca4bda1346250efca7f46f494b8f13882a0a4e4230ab1af1b043480f20952033bc613de4221e9d40703360ed255bd77d7685541ed7d267e27e3fa2a94f6d4
@@ -0,0 +1,72 @@
1
+ class IHash < Hash
2
+ def symbolize_keys
3
+ IHash.symbolize(self)
4
+ end
5
+
6
+ def stringify_keys
7
+ IHash.stringify(self)
8
+ end
9
+
10
+ def compare(other_hash)
11
+ current_hash = IHash.symbolize(self)
12
+ other_hash = IHash.symbolize(other_hash)
13
+ (current_hash.keys | other_hash.keys).each_with_object({}) do |k, diff|
14
+ current_hash_key = current_hash[k]
15
+ other_hash_key = other_hash[k]
16
+ if current_hash_key != other_hash_key
17
+ diff[k] = [current_hash_key, other_hash_key]
18
+ next unless current_hash_key.is_a?(Hash) && other_hash_key.is_a?(Hash)
19
+
20
+ diff[k] = deep_diff(current_hash_key, other_hash_key)
21
+ end
22
+ diff
23
+ end
24
+ end
25
+
26
+ class << self
27
+ def symbolize(obj)
28
+ if obj.is_a? Hash
29
+ return obj.inject({}) do |memo, (k, v)|
30
+ memo.tap { |m| m[k.to_sym] = symbolize(v) }
31
+ end
32
+ elsif obj.is_a? Array
33
+ return obj.map { |memo| symbolize(memo) }
34
+ end
35
+ obj
36
+ end
37
+
38
+ def stringify(obj)
39
+ if obj.is_a? Hash
40
+ return obj.inject({}) do |memo, (k, v)|
41
+ memo.tap { |m| m[k.to_s] = stringify(v) }
42
+ end
43
+ elsif obj.is_a? Array
44
+ return obj.map { |memo| stringify(memo) }
45
+ end
46
+ obj
47
+ end
48
+ end
49
+ end
50
+
51
+ # Modify `Object`
52
+ class Object
53
+ def deep_symbolize_keys
54
+ return self.reduce({}) do |memo, (k, v)|
55
+ memo.tap { |m| m[k.to_sym] = v.deep_symbolize_keys }
56
+ end if self.is_a? Hash
57
+ return self.reduce([]) do |memo, v|
58
+ memo << v.deep_symbolize_keys; memo
59
+ end if self.is_a? Array
60
+ self
61
+ end
62
+
63
+ def deep_stringify_keys
64
+ return self.reduce({}) do |memo, (k, v)|
65
+ memo.tap { |m| m[k.to_s] = v.deep_stringify_keys }
66
+ end if self.is_a? Hash
67
+ return self.reduce([]) do |memo, v|
68
+ memo << v.deep_stringify_keys; memo
69
+ end if self.is_a? Array
70
+ self
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Derek Nguyen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The new Hash for Ruby
14
+ email: derek.nguyen.269@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/i_hash.rb
20
+ homepage: https://rubygems.org/gems/i_hash
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.14
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: i_hash
44
+ test_files: []