darthjee-core_ext 1.6.1 → 1.6.2
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +0 -4
- data/core_ext.gemspec +1 -1
- data/lib/darthjee/core_ext/hash.rb +15 -170
- data/lib/darthjee/core_ext/hash/key_changeable.rb +139 -0
- data/lib/darthjee/core_ext/hash/transformable.rb +25 -0
- data/lib/darthjee/core_ext/hash/transposeable.rb +22 -0
- data/lib/darthjee/core_ext/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 738212a365d4f2b6182a1f71519d482998f71f7f
|
4
|
+
data.tar.gz: a5028fd0f7b6f62b5d7ba0510204c21fbafb3a52
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6386535f1ed9bed1fc21e6f157d4117322454cc0413f6bb01461f130fd8da860d6b02a5aaa634f60116e0e5e2703fc4515f4dc4fce0dfc08af86fc6b0d77489f
|
7
|
+
data.tar.gz: ff41c39af8f135b64f84f1cc5f21d51536490ada52003570c66add9c3887282b812a35249b3c6a43eb7ec628b1f43543757dbe5fc40aa3a02800986776dde1ae
|
data/.rubocop.yml
CHANGED
data/core_ext.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
|
|
20
20
|
|
21
21
|
gem.add_runtime_dependency 'activesupport', '>= 5.2.x'
|
22
22
|
|
23
|
-
gem.add_development_dependency 'bundler', '~> 1.
|
23
|
+
gem.add_development_dependency 'bundler', '~> 1.15.4'
|
24
24
|
gem.add_development_dependency 'pry-nav', '~> 0.2.4'
|
25
25
|
gem.add_development_dependency 'rake', '>= 12.3.1'
|
26
26
|
gem.add_development_dependency 'rspec', '>= 3.8'
|
@@ -1,182 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'darthjee/core_ext/hash/key_changeable'
|
4
|
+
require 'darthjee/core_ext/hash/transposeable'
|
5
|
+
require 'darthjee/core_ext/hash/transformable'
|
6
|
+
|
3
7
|
class Hash
|
4
|
-
autoload :
|
8
|
+
autoload :ChainFetcher, 'darthjee/core_ext/hash/chain_fetcher'
|
5
9
|
autoload :DeepHashConstructor, 'darthjee/core_ext/hash/deep_hash_constructor'
|
6
10
|
autoload :KeyChanger, 'darthjee/core_ext/hash/key_changer'
|
7
|
-
autoload :
|
11
|
+
autoload :KeysSorter, 'darthjee/core_ext/hash/keys_sorter'
|
8
12
|
autoload :Squasher, 'darthjee/core_ext/hash/squasher'
|
13
|
+
autoload :ValueChanger, 'darthjee/core_ext/hash/value_changer'
|
9
14
|
autoload :ToHashMapper, 'darthjee/core_ext/hash/to_hash_mapper'
|
10
|
-
autoload :KeysSorter, 'darthjee/core_ext/hash/keys_sorter'
|
11
|
-
|
12
|
-
def chain_fetch(*keys, &block)
|
13
|
-
ChainFetcher.new(self, *keys, &block).fetch
|
14
|
-
end
|
15
|
-
|
16
|
-
def squash
|
17
|
-
Squasher.squash(self)
|
18
|
-
end
|
19
|
-
|
20
|
-
def map_to_hash(&block)
|
21
|
-
ToHashMapper.new(self).map(&block)
|
22
|
-
end
|
23
|
-
|
24
|
-
def remap_keys(remap)
|
25
|
-
dup.remap_keys!(remap)
|
26
|
-
end
|
27
|
-
|
28
|
-
def remap_keys!(keys_map)
|
29
|
-
KeyChanger.new(self).remap(keys_map)
|
30
|
-
end
|
31
|
-
|
32
|
-
def lower_camelize_keys(options = {})
|
33
|
-
dup.lower_camelize_keys!(options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def lower_camelize_keys!(options = {})
|
37
|
-
options = options.merge(uppercase_first_letter: false)
|
38
|
-
|
39
|
-
camelize_keys!(options)
|
40
|
-
end
|
41
|
-
|
42
|
-
def camelize_keys(options = {})
|
43
|
-
dup.camelize_keys!(options)
|
44
|
-
end
|
45
|
-
|
46
|
-
def camelize_keys!(options = {})
|
47
|
-
Hash::KeyChanger.new(self).camelize_keys(options)
|
48
|
-
end
|
49
|
-
|
50
|
-
def underscore_keys(options = {})
|
51
|
-
dup.underscore_keys!(options)
|
52
|
-
end
|
53
|
-
|
54
|
-
def underscore_keys!(options = {})
|
55
|
-
Hash::KeyChanger.new(self).underscore_keys(options)
|
56
|
-
end
|
57
|
-
|
58
|
-
def exclusive_merge(hash)
|
59
|
-
dup.exclusive_merge!(hash)
|
60
|
-
end
|
61
|
-
|
62
|
-
def exclusive_merge!(hash)
|
63
|
-
merge!(hash.slice(*keys))
|
64
|
-
end
|
65
|
-
|
66
|
-
# change all keys returning the new map
|
67
|
-
# options: { recursive: true }
|
68
|
-
# ex: { "a" =>1 }.change_keys{ |key| key.upcase } == { "A" => 1 }
|
69
|
-
def change_keys(options = {}, &block)
|
70
|
-
deep_dup.change_keys!(options, &block)
|
71
|
-
end
|
72
|
-
|
73
|
-
# change all keys returning the new map
|
74
|
-
# options: { recursive: true }
|
75
|
-
# ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
|
76
|
-
def change_keys!(options = {}, &block)
|
77
|
-
Hash::KeyChanger.new(self).change_keys(options, &block)
|
78
|
-
end
|
79
|
-
|
80
|
-
# change all publicaly sending method calls
|
81
|
-
# options: { recursive: true }
|
82
|
-
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
83
|
-
def chain_change_keys(*calls)
|
84
|
-
deep_dup.chain_change_keys!(*calls)
|
85
|
-
end
|
86
|
-
|
87
|
-
# change all publicaly sending method calls
|
88
|
-
# options: { recursive: true }
|
89
|
-
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
90
|
-
def chain_change_keys!(*calls)
|
91
|
-
options = calls.extract_options!
|
92
15
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
# prepend a string to all keys
|
99
|
-
# options {
|
100
|
-
# recursive: true,
|
101
|
-
# type: :keep [keep, string, symbol] (key type to be returned)
|
102
|
-
# }
|
103
|
-
# ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
|
104
|
-
# # returns { :foo_a => 1, "foo_b"=> 2 }
|
105
|
-
def prepend_to_keys(str, options = {})
|
106
|
-
change_key_text(options) do |key|
|
107
|
-
"#{str}#{key}"
|
108
|
-
end
|
109
|
-
end
|
16
|
+
include Hash::KeyChangeable
|
17
|
+
include Hash::Transposeable
|
18
|
+
include Hash::Transformable
|
110
19
|
|
111
|
-
|
112
|
-
#
|
113
|
-
|
114
|
-
# type: :keep [keep, string, symbol] (key type to be returned)
|
115
|
-
# }
|
116
|
-
# ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
|
117
|
-
# # returns { :a_bar => 1, "b_bar"=> 2 }
|
118
|
-
def append_to_keys(str, options = {})
|
119
|
-
change_key_text(options) do |key|
|
120
|
-
"#{key}#{str}"
|
121
|
-
end
|
122
|
-
end
|
20
|
+
########################################
|
21
|
+
# Fetching methods
|
22
|
+
#########################################
|
123
23
|
|
124
|
-
|
125
|
-
|
126
|
-
# ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
|
127
|
-
def sort_keys(options = {})
|
128
|
-
Hash::KeysSorter.new(self, **options).sort
|
129
|
-
end
|
130
|
-
|
131
|
-
# creates a new hash with changes in its values
|
132
|
-
# options: {
|
133
|
-
# recursive: true,
|
134
|
-
# skip_hash:true
|
135
|
-
# }
|
136
|
-
# ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
|
137
|
-
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
|
138
|
-
# # returns { a:"1", b:"{ c=>1 }
|
139
|
-
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s }
|
140
|
-
# # returns { a:"1", b:{ c=>"1" } }
|
141
|
-
def change_values(options = {}, &block)
|
142
|
-
deep_dup.change_values!(options, &block)
|
143
|
-
end
|
144
|
-
|
145
|
-
def change_values!(options = {}, &block)
|
146
|
-
Hash::ValueChanger.new(options, &block).change(self)
|
147
|
-
end
|
148
|
-
|
149
|
-
def to_deep_hash(separator = '.')
|
150
|
-
Hash::DeepHashConstructor.new(separator).deep_hash(self)
|
151
|
-
end
|
152
|
-
|
153
|
-
def transpose!
|
154
|
-
aux = dup
|
155
|
-
keys.each { |k| delete(k) }
|
156
|
-
aux.each do |k, v|
|
157
|
-
self[v] = k
|
158
|
-
end
|
159
|
-
self
|
160
|
-
end
|
161
|
-
|
162
|
-
def transpose
|
163
|
-
{}.tap do |new_hash|
|
164
|
-
each do |k, v|
|
165
|
-
new_hash[v] = k
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
private
|
171
|
-
|
172
|
-
# changes the text of the keys
|
173
|
-
# options {
|
174
|
-
# recursive: true,
|
175
|
-
# type: :keep [keep, string, symbol] (key type to be returned)
|
176
|
-
# }
|
177
|
-
# ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
|
178
|
-
# # returns { :A => 1, "B"=> 2 }
|
179
|
-
def change_key_text(options = {}, &block)
|
180
|
-
Hash::KeyChanger.new(self).change_text(options, &block)
|
24
|
+
def chain_fetch(*keys, &block)
|
25
|
+
ChainFetcher.new(self, *keys, &block).fetch
|
181
26
|
end
|
182
27
|
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
module KeyChangeable
|
5
|
+
def remap_keys(remap)
|
6
|
+
dup.remap_keys!(remap)
|
7
|
+
end
|
8
|
+
|
9
|
+
def remap_keys!(keys_map)
|
10
|
+
KeyChanger.new(self).remap(keys_map)
|
11
|
+
end
|
12
|
+
|
13
|
+
def lower_camelize_keys(options = {})
|
14
|
+
dup.lower_camelize_keys!(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def lower_camelize_keys!(options = {})
|
18
|
+
options = options.merge(uppercase_first_letter: false)
|
19
|
+
|
20
|
+
camelize_keys!(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def camelize_keys(options = {})
|
24
|
+
dup.camelize_keys!(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def camelize_keys!(options = {})
|
28
|
+
Hash::KeyChanger.new(self).camelize_keys(options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def underscore_keys(options = {})
|
32
|
+
dup.underscore_keys!(options)
|
33
|
+
end
|
34
|
+
|
35
|
+
def underscore_keys!(options = {})
|
36
|
+
Hash::KeyChanger.new(self).underscore_keys(options)
|
37
|
+
end
|
38
|
+
|
39
|
+
# change all keys returning the new map
|
40
|
+
# options: { recursive: true }
|
41
|
+
# ex: { "a" =>1 }.change_keys{ |key| key.upcase } == { "A" => 1 }
|
42
|
+
def change_keys(options = {}, &block)
|
43
|
+
deep_dup.change_keys!(options, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
# change all keys returning the new map
|
47
|
+
# options: { recursive: true }
|
48
|
+
# ex: { "a":1 }.change_keys{ |key| key.upcase } == { "A":1 }
|
49
|
+
def change_keys!(options = {}, &block)
|
50
|
+
Hash::KeyChanger.new(self).change_keys(options, &block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# change all publicaly sending method calls
|
54
|
+
# options: { recursive: true }
|
55
|
+
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
56
|
+
def chain_change_keys(*calls)
|
57
|
+
deep_dup.chain_change_keys!(*calls)
|
58
|
+
end
|
59
|
+
|
60
|
+
# change all publicaly sending method calls
|
61
|
+
# options: { recursive: true }
|
62
|
+
# ex: { a: 1 }.chain_change_keys(:to_s, :upcase) == { "A" =>1 }
|
63
|
+
def chain_change_keys!(*calls)
|
64
|
+
options = calls.extract_options!
|
65
|
+
|
66
|
+
calls.inject(self) do |h, m|
|
67
|
+
h.change_keys!(options, &m)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# prepend a string to all keys
|
72
|
+
# options {
|
73
|
+
# recursive: true,
|
74
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
75
|
+
# }
|
76
|
+
# ex: { :a => 1, "b"=> 2 }.prepend_to_keys("foo_")
|
77
|
+
# # returns { :foo_a => 1, "foo_b"=> 2 }
|
78
|
+
def prepend_to_keys(str, options = {})
|
79
|
+
change_key_text(options) do |key|
|
80
|
+
"#{str}#{key}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# append a string to all keys
|
85
|
+
# options {
|
86
|
+
# recursive: true,
|
87
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
88
|
+
# }
|
89
|
+
# ex: { :a => 1, "b"=> 2 }.append_to_keys("_bar")
|
90
|
+
# # returns { :a_bar => 1, "b_bar"=> 2 }
|
91
|
+
def append_to_keys(str, options = {})
|
92
|
+
change_key_text(options) do |key|
|
93
|
+
"#{key}#{str}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# sorts keys for hash
|
98
|
+
# options: { recursive: true }
|
99
|
+
# ex: { b:1, a:2 }.sort_keys == { a:2, b:1 }
|
100
|
+
def sort_keys(options = {})
|
101
|
+
Hash::KeysSorter.new(self, **options).sort
|
102
|
+
end
|
103
|
+
|
104
|
+
##########################################
|
105
|
+
# Value change methods
|
106
|
+
##########################################
|
107
|
+
|
108
|
+
# creates a new hash with changes in its values
|
109
|
+
# options: {
|
110
|
+
# recursive: true,
|
111
|
+
# skip_hash:true
|
112
|
+
# }
|
113
|
+
# ex: { a:1, b:2 }.change_values{ |v| v+1 } == { a:2, b:3 }
|
114
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:false) { |v| v.to_s }
|
115
|
+
# # returns { a:"1", b:"{ c=>1 }
|
116
|
+
# ex: { a:1, b:{ c:1 } }.change_values(skip_hash:true) { |v| v.to_s }
|
117
|
+
# # returns { a:"1", b:{ c=>"1" } }
|
118
|
+
def change_values(options = {}, &block)
|
119
|
+
deep_dup.change_values!(options, &block)
|
120
|
+
end
|
121
|
+
|
122
|
+
def change_values!(options = {}, &block)
|
123
|
+
Hash::ValueChanger.new(options, &block).change(self)
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
# changes the text of the keys
|
129
|
+
# options {
|
130
|
+
# recursive: true,
|
131
|
+
# type: :keep [keep, string, symbol] (key type to be returned)
|
132
|
+
# }
|
133
|
+
# ex: { :a => 1, "b"=> 2 }.change_key_text{ |key| key.upcase }
|
134
|
+
# # returns { :A => 1, "B"=> 2 }
|
135
|
+
def change_key_text(options = {}, &block)
|
136
|
+
Hash::KeyChanger.new(self).change_text(options, &block)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
module Transformable
|
5
|
+
def squash
|
6
|
+
Squasher.squash(self)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_deep_hash(separator = '.')
|
10
|
+
Hash::DeepHashConstructor.new(separator).deep_hash(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def map_to_hash(&block)
|
14
|
+
ToHashMapper.new(self).map(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def exclusive_merge(hash)
|
18
|
+
dup.exclusive_merge!(hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def exclusive_merge!(hash)
|
22
|
+
merge!(hash.slice(*keys))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
module Transposeable
|
5
|
+
def transpose!
|
6
|
+
aux = dup
|
7
|
+
keys.each { |k| delete(k) }
|
8
|
+
aux.each do |k, v|
|
9
|
+
self[v] = k
|
10
|
+
end
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def transpose
|
15
|
+
{}.tap do |new_hash|
|
16
|
+
each do |k, v|
|
17
|
+
new_hash[v] = k
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: darthjee-core_ext
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.15.4
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.15.4
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: pry-nav
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -156,10 +156,13 @@ files:
|
|
156
156
|
- lib/darthjee/core_ext/hash.rb
|
157
157
|
- lib/darthjee/core_ext/hash/chain_fetcher.rb
|
158
158
|
- lib/darthjee/core_ext/hash/deep_hash_constructor.rb
|
159
|
+
- lib/darthjee/core_ext/hash/key_changeable.rb
|
159
160
|
- lib/darthjee/core_ext/hash/key_changer.rb
|
160
161
|
- lib/darthjee/core_ext/hash/keys_sorter.rb
|
161
162
|
- lib/darthjee/core_ext/hash/squasher.rb
|
162
163
|
- lib/darthjee/core_ext/hash/to_hash_mapper.rb
|
164
|
+
- lib/darthjee/core_ext/hash/transformable.rb
|
165
|
+
- lib/darthjee/core_ext/hash/transposeable.rb
|
163
166
|
- lib/darthjee/core_ext/hash/value_changer.rb
|
164
167
|
- lib/darthjee/core_ext/math.rb
|
165
168
|
- lib/darthjee/core_ext/numeric.rb
|