darthjee-core_ext 1.7.4 → 2.0.0
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/.circleci/config.yml +7 -1
- data/.gitignore +2 -0
- data/.reek.yml +3 -0
- data/ARRAY_README.md +72 -15
- data/CLASS_README.md +154 -0
- data/DATE_README.md +19 -9
- data/Dockerfile +2 -5
- data/ENUMERABLE_README.md +154 -4
- data/HASH_README.md +276 -135
- data/MATH_README.md +14 -10
- data/OBJECT_README.md +18 -35
- data/README.md +8 -4
- data/Rakefile +1 -0
- data/SYMBOL_README.md +13 -18
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.yml +45 -3
- data/core_ext.gemspec +10 -8
- data/docker-compose.yml +15 -7
- data/lib/darthjee/core_ext/array.rb +17 -12
- data/lib/darthjee/core_ext/array/hash_builder.rb +20 -4
- data/lib/darthjee/core_ext/class.rb +28 -10
- data/lib/darthjee/core_ext/date.rb +1 -0
- data/lib/darthjee/core_ext/enumerable.rb +67 -30
- data/lib/darthjee/core_ext/hash.rb +4 -2
- data/lib/darthjee/core_ext/hash/cameliazable.rb +39 -8
- data/lib/darthjee/core_ext/hash/chain_fetcher.rb +17 -1
- data/lib/darthjee/core_ext/hash/changeable.rb +88 -0
- data/lib/darthjee/core_ext/hash/deep_hash_constructor.rb +127 -63
- data/lib/darthjee/core_ext/hash/deep_hash_constructor/setter.rb +112 -0
- data/lib/darthjee/core_ext/hash/key_changeable.rb +126 -54
- data/lib/darthjee/core_ext/hash/key_changer.rb +140 -40
- data/lib/darthjee/core_ext/hash/keys_sorter.rb +42 -6
- data/lib/darthjee/core_ext/hash/squasher.rb +131 -18
- data/lib/darthjee/core_ext/hash/transformable.rb +133 -41
- data/lib/darthjee/core_ext/hash/transposeable.rb +37 -8
- data/lib/darthjee/core_ext/hash/value_changer.rb +76 -36
- data/lib/darthjee/core_ext/math.rb +37 -6
- data/lib/darthjee/core_ext/numeric.rb +10 -0
- data/lib/darthjee/core_ext/object.rb +29 -0
- data/lib/darthjee/core_ext/symbol.rb +22 -2
- data/lib/darthjee/core_ext/version.rb +1 -1
- data/scripts/check_readme.sh +6 -0
- data/scripts/rubycritic.sh +10 -0
- data/spec/integration/readme/array_spec.rb +96 -0
- data/spec/integration/readme/class_spec.rb +123 -0
- data/spec/integration/readme/date_spec.rb +20 -0
- data/spec/integration/readme/enumerable_spec.rb +87 -0
- data/spec/integration/readme/hash_spec.rb +391 -0
- data/spec/integration/readme/math_spec.rb +31 -0
- data/spec/integration/readme/object_spec.rb +49 -0
- data/spec/integration/readme/symbol_spec.rb +29 -0
- data/spec/integration/yard/darthjee/core_ext/enumerable_spec.rb +24 -4
- data/spec/integration/yard/darthjee/core_ext/hash/cameliazable_spec.rb +11 -0
- data/spec/integration/yard/darthjee/core_ext/hash/changeable_spec.rb +68 -0
- data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +34 -0
- data/spec/integration/yard/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +65 -0
- data/spec/integration/yard/darthjee/core_ext/hash/key_changeable_spec.rb +8 -0
- data/spec/integration/yard/darthjee/core_ext/hash/key_changer_spec.rb +59 -0
- data/spec/integration/yard/darthjee/core_ext/hash/keys_sorter_spec.rb +25 -0
- data/spec/integration/yard/darthjee/core_ext/hash/squasher_spec.rb +60 -0
- data/spec/integration/yard/darthjee/core_ext/hash/transformable_spec.rb +66 -36
- data/spec/integration/yard/darthjee/core_ext/hash/transposeable_spec.rb +35 -0
- data/spec/integration/yard/darthjee/core_ext/hash_spec.rb +13 -2
- data/spec/integration/yard/darthjee/core_ext/math_spec.rb +28 -0
- data/spec/integration/yard/darthjee/core_ext/numeric_spec.rb +11 -0
- data/spec/integration/yard/darthjee/core_ext/object_spec.rb +47 -0
- data/spec/integration/yard/darthjee/core_ext/symbol_spec.rb +17 -0
- data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor/setter_spec.rb +64 -0
- data/spec/lib/darthjee/core_ext/hash/deep_hash_constructor_spec.rb +16 -156
- data/spec/lib/darthjee/core_ext/hash/keys_sorter_spec.rb +5 -4
- data/spec/lib/darthjee/core_ext/hash/squasher_spec.rb +8 -2
- data/spec/lib/darthjee/core_ext/hash/value_changer_spec.rb +4 -12
- data/spec/lib/hash_spec.rb +92 -146
- data/spec/lib/object_spec.rb +32 -0
- data/spec/support/models/client.rb +22 -0
- data/spec/support/shared_examples/hash/deep_hash.rb +166 -0
- data/spec/support/shared_examples/hash/hash_squasher.rb +54 -9
- data/spec/support/shared_examples/hash/keys_sorter.rb +266 -6
- metadata +70 -8
- data/lib/darthjee/core_ext/hash/to_hash_mapper.rb +0 -25
- data/spec/lib/darthjee/core_ext/hash/to_hash_mapper_spec.rb +0 -11
@@ -3,90 +3,154 @@
|
|
3
3
|
module Darthjee
|
4
4
|
module CoreExt
|
5
5
|
module Hash
|
6
|
+
# @api private
|
7
|
+
#
|
8
|
+
# @author Darthjee
|
9
|
+
#
|
10
|
+
# Class responsible for creating a Hash deep hash
|
11
|
+
#
|
12
|
+
# Deep hash construction happens when a hash of one layer
|
13
|
+
# (no sub hashes) has keys that, once explitted, can be
|
14
|
+
# assembled in a hash with many layers
|
15
|
+
#
|
16
|
+
# @see Transformable#squash
|
17
|
+
# @see Transformable#to_deep_hash
|
18
|
+
#
|
19
|
+
# @example (see Transformable#to_deep_hash)
|
20
|
+
# @example General Usage
|
21
|
+
# hash = {
|
22
|
+
# 'account.person.name[0]' => 'John',
|
23
|
+
# 'account.person.name[1]' => 'Wick',
|
24
|
+
# 'account.person.age' => 20,
|
25
|
+
# 'account.number' => '102030',
|
26
|
+
# :'house.number' => 67,
|
27
|
+
# :'house.zip' => 12_345
|
28
|
+
# }
|
29
|
+
#
|
30
|
+
# constructor = Darthjee::CoreExt::Hash::DeepHashConstructor.new('.')
|
31
|
+
#
|
32
|
+
# constructor.deep_hash(hash) # returns {
|
33
|
+
# # 'account' => {
|
34
|
+
# # 'person' => {
|
35
|
+
# # 'name' => ['John', 'Wick'],
|
36
|
+
# # 'age' => 20
|
37
|
+
# # },
|
38
|
+
# # 'number' => '102030',
|
39
|
+
# # },
|
40
|
+
# # 'house' => {
|
41
|
+
# # 'number' => 67,
|
42
|
+
# # 'zip' => 12345
|
43
|
+
# # }
|
44
|
+
# # }
|
6
45
|
class DeepHashConstructor
|
7
|
-
|
46
|
+
autoload :Setter, "#{PATH}/hash/deep_hash_constructor/setter"
|
8
47
|
|
9
|
-
|
48
|
+
# @param separator [::String] keys splitter
|
49
|
+
def initialize(separator = '.')
|
10
50
|
@separator = separator
|
11
51
|
end
|
12
52
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
53
|
+
# Performs deep hash transformation
|
54
|
+
#
|
55
|
+
# @param hash [::Hash] On layered hash
|
56
|
+
#
|
57
|
+
# @return [::Hash] Many layered hash
|
58
|
+
#
|
59
|
+
# @example (see DeepHashConstructor)
|
60
|
+
def deep_hash(hash)
|
61
|
+
break_keys(hash).tap do
|
62
|
+
hash.each do |key, value|
|
63
|
+
hash[key] = deep_hash_value(value)
|
64
|
+
end
|
20
65
|
end
|
21
66
|
end
|
22
67
|
|
23
68
|
private
|
24
69
|
|
25
|
-
|
26
|
-
|
27
|
-
value.is_a?(Hash) ? deep_hash(value) : value
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def hash_deep_hash(hash)
|
32
|
-
{}.tap do |new_hash|
|
33
|
-
hash.each do |k, v|
|
34
|
-
base_key, child_key = split_key(k, separator)
|
35
|
-
set_deep_hash_positioned_value(new_hash, base_key, v, child_key)
|
36
|
-
end
|
70
|
+
# @private
|
71
|
+
attr_reader :separator
|
37
72
|
|
38
|
-
|
39
|
-
|
40
|
-
|
73
|
+
# @private
|
74
|
+
# break the keys creating sub-hashes
|
75
|
+
#
|
76
|
+
# @param hash [::Hash] hash to be broken
|
77
|
+
#
|
78
|
+
# @example Breaking many level keys
|
79
|
+
# hash = {
|
80
|
+
# 'account.person.name[0]' => 'John',
|
81
|
+
# 'account.person.name[1]' => 'Wick',
|
82
|
+
# 'account.person.age' => 20,
|
83
|
+
# 'account.number' => '102030',
|
84
|
+
# :'house.number' => 67,
|
85
|
+
# :'house.zip' => 12_345
|
86
|
+
# }
|
87
|
+
#
|
88
|
+
# constructor = Darthjee::CoreExt::Hash::DeepHashConstructor.new('.')
|
89
|
+
#
|
90
|
+
# constructor.send(:break_keys, hash)
|
91
|
+
#
|
92
|
+
# # Returns {
|
93
|
+
# # 'account' => {
|
94
|
+
# # %w[person name[0]] => 'John',
|
95
|
+
# # %w[person name[1]] => 'Wick',
|
96
|
+
# # %w[person age] => 20,
|
97
|
+
# # %w[number] => '102030'
|
98
|
+
# # },
|
99
|
+
# # 'house' => {
|
100
|
+
# # %w[number] => 67,
|
101
|
+
# # %w[zip] => 12_345
|
102
|
+
# # }
|
103
|
+
# # }
|
104
|
+
#
|
105
|
+
# @return [Hash]
|
106
|
+
def break_keys(hash)
|
107
|
+
hash.keys.each do |key|
|
108
|
+
value = hash.delete(key)
|
109
|
+
base_key, child_key = split_key(key, separator)
|
110
|
+
Setter.new(hash, base_key).set(child_key, value)
|
41
111
|
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def split_key(key, separator)
|
45
|
-
separator_rxp = separator == '.' ? "\\#{separator}" : separator
|
46
|
-
skipper = "[^#{separator}]"
|
47
|
-
regexp = Regexp.new("^(#{skipper}*)#{separator_rxp}(.*)")
|
48
|
-
match = key.match(regexp)
|
49
112
|
|
50
|
-
|
113
|
+
hash
|
51
114
|
end
|
52
115
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
116
|
+
# @private
|
117
|
+
#
|
118
|
+
# Recursively proccess a value calling deep hash on it
|
119
|
+
#
|
120
|
+
# @return [::Object]
|
121
|
+
def deep_hash_value(object)
|
122
|
+
return array_deep_hash(object) if object.is_a? Array
|
123
|
+
return deep_hash(object) if object.is_a? Hash
|
124
|
+
object
|
63
125
|
end
|
64
126
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
127
|
+
# @private
|
128
|
+
#
|
129
|
+
# Map array performing deep hash on its Hash elements
|
130
|
+
#
|
131
|
+
# @param array [::Array] array to be mapped
|
132
|
+
#
|
133
|
+
# @return [::Array]
|
134
|
+
def array_deep_hash(array)
|
135
|
+
array.map do |value|
|
136
|
+
deep_hash_value(value)
|
75
137
|
end
|
76
138
|
end
|
77
139
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
140
|
+
# @private
|
141
|
+
#
|
142
|
+
# Split key into array of keys
|
143
|
+
#
|
144
|
+
# @param key [::String,::Symbol] key to be splitted
|
145
|
+
# @param separator [::String] string of key splitting
|
146
|
+
#
|
147
|
+
# @return [::Array<::String>,::String]
|
148
|
+
def split_key(key, separator)
|
149
|
+
keys = key.is_a?(Array) ? key : key.to_s.split(separator)
|
82
150
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
else
|
87
|
-
hash[base_key] ||= {}
|
88
|
-
hash[base_key][key] = value
|
89
|
-
end
|
151
|
+
return keys unless keys.second
|
152
|
+
|
153
|
+
[keys.shift, keys]
|
90
154
|
end
|
91
155
|
end
|
92
156
|
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Darthjee
|
4
|
+
module CoreExt
|
5
|
+
module Hash
|
6
|
+
class DeepHashConstructor
|
7
|
+
# @api private
|
8
|
+
#
|
9
|
+
# Class responsible for setting value localized inside hash
|
10
|
+
#
|
11
|
+
# @example Simple usage
|
12
|
+
# hash = {}
|
13
|
+
# base = 'person'
|
14
|
+
# setter = Darthjee::CoreExt::Hash::DeepHashConstructor::Setter.new(
|
15
|
+
# hash, base
|
16
|
+
# )
|
17
|
+
#
|
18
|
+
# setter.set('age', 21)
|
19
|
+
#
|
20
|
+
# hash # changed to {
|
21
|
+
# # 'person' => {
|
22
|
+
# # 'age' => 21,
|
23
|
+
# # }
|
24
|
+
# # }
|
25
|
+
class Setter
|
26
|
+
# @param hash [Hash] hash to be changed
|
27
|
+
# @param base_key [::String] base key of hash where
|
28
|
+
# subhash will be created
|
29
|
+
def initialize(hash, base_key)
|
30
|
+
@hash = hash
|
31
|
+
@base_key = base_key
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sets a value in the correct key inside the hash
|
35
|
+
#
|
36
|
+
# @param key [::String,::NilClass] key where value will live
|
37
|
+
# @param value [::Object] value to be set
|
38
|
+
#
|
39
|
+
# @return [::Object] value
|
40
|
+
#
|
41
|
+
# @example (see DeepHashConstructor)
|
42
|
+
#
|
43
|
+
# @example With Array index
|
44
|
+
# hash = {}
|
45
|
+
# base = 'person[0]'
|
46
|
+
# setter = Darthjee::CoreExt::Hash::DeepHashConstructor::Setter.new(
|
47
|
+
# hash, base
|
48
|
+
# )
|
49
|
+
#
|
50
|
+
# setter.set('age', 21)
|
51
|
+
#
|
52
|
+
# hash # changed to {
|
53
|
+
# # 'person' => [{
|
54
|
+
# # 'age' => 21,
|
55
|
+
# # }]
|
56
|
+
# # }
|
57
|
+
def set(key, value)
|
58
|
+
return hash[base_key] = value unless key || index
|
59
|
+
return array[index] = value unless key
|
60
|
+
|
61
|
+
sub_hash[key] = value
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
# @private
|
67
|
+
attr_reader :hash, :base_key
|
68
|
+
|
69
|
+
# @private
|
70
|
+
# Extract index of array from base_key
|
71
|
+
#
|
72
|
+
# @return [::NilClass,::Integer]
|
73
|
+
def index
|
74
|
+
return @index if instance_variable_defined?('@index')
|
75
|
+
|
76
|
+
match = base_key.match(/\[([^)]+)\]/)
|
77
|
+
return @index = nil unless match
|
78
|
+
|
79
|
+
@index = match[1].to_i
|
80
|
+
end
|
81
|
+
|
82
|
+
# @private
|
83
|
+
# Returns array that will receive value
|
84
|
+
#
|
85
|
+
# This array is only created / used when base_key
|
86
|
+
# contains index
|
87
|
+
#
|
88
|
+
# @return [::Array]
|
89
|
+
def array
|
90
|
+
return @array if instance_variable_defined?('@array')
|
91
|
+
|
92
|
+
key_without_index = base_key.tr("[#{index}]", '')
|
93
|
+
|
94
|
+
@array = hash[key_without_index] ||= []
|
95
|
+
end
|
96
|
+
|
97
|
+
# @private
|
98
|
+
# Returns sub hash that will receive the value
|
99
|
+
#
|
100
|
+
# This sub_hash is only build when key is not nil
|
101
|
+
#
|
102
|
+
# @return [::Hash]
|
103
|
+
def sub_hash
|
104
|
+
return array[index] ||= {} if index
|
105
|
+
|
106
|
+
hash[base_key] ||= {}
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -7,14 +7,26 @@ module Darthjee
|
|
7
7
|
# changing / transforming keys of a Hash
|
8
8
|
#
|
9
9
|
# @api public
|
10
|
+
#
|
11
|
+
# @author Darthjee
|
12
|
+
#
|
13
|
+
# @see KeyChanger
|
14
|
+
# @see KeysSorter
|
10
15
|
module KeyChangeable
|
11
|
-
|
12
|
-
#
|
16
|
+
##########################################
|
17
|
+
# Key change methods
|
18
|
+
##########################################
|
19
|
+
|
20
|
+
# Change all keys without changing the original hash
|
21
|
+
#
|
22
|
+
# It changes all keysby publically sending methods
|
23
|
+
# to the keys
|
13
24
|
#
|
14
25
|
# @return [::Hash] New hash with the resulting keys
|
15
|
-
# @param [::Array<Symbol>] calls methods to be
|
26
|
+
# @param [::Array<Symbol>] calls methods to be
|
27
|
+
# called form the key`
|
16
28
|
#
|
17
|
-
# @see #
|
29
|
+
# @see #chain_change_keys!
|
18
30
|
#
|
19
31
|
# @example
|
20
32
|
# hash = { first: 1, second: 2 }
|
@@ -24,13 +36,15 @@ module Darthjee
|
|
24
36
|
deep_dup.chain_change_keys!(*calls)
|
25
37
|
end
|
26
38
|
|
27
|
-
# Change all keys
|
28
|
-
#
|
39
|
+
# Change all keys changing the original hash
|
40
|
+
#
|
41
|
+
# It changes all keys by publically sending methods
|
42
|
+
# to the keys
|
29
43
|
#
|
30
44
|
# @return [::Hash] New hash with the resulting keys
|
31
45
|
# @param [::Array<Symbol>] calls methods to be called form the key`
|
32
46
|
#
|
33
|
-
# @see #
|
47
|
+
# @see #change_keys
|
34
48
|
#
|
35
49
|
# @example (see #chain_change_keys)
|
36
50
|
def chain_change_keys!(*calls)
|
@@ -45,10 +59,13 @@ module Darthjee
|
|
45
59
|
#
|
46
60
|
# @return new Hash with modified keys
|
47
61
|
# @param [::Hash] options options to passed to KeyChanger
|
48
|
-
# @option options [
|
62
|
+
# @option options [::TrueClass,::FalseClass]
|
63
|
+
# recursive (true) flag defining the
|
49
64
|
# change to happen also
|
50
65
|
# on inner hashes (defaults to: true)
|
51
66
|
#
|
67
|
+
# @yield (key) changing key block
|
68
|
+
#
|
52
69
|
# @see Hash::KeyChanger#change_keys
|
53
70
|
#
|
54
71
|
# @example
|
@@ -71,10 +88,13 @@ module Darthjee
|
|
71
88
|
#
|
72
89
|
# @return self
|
73
90
|
# @param [::Hash] options options to passed to KeyChanger
|
74
|
-
# @option options [
|
91
|
+
# @option options [::TrueClass,::FalseClass]
|
92
|
+
# recursive: (true) flag defining the
|
75
93
|
# change to happen also
|
76
94
|
# on inner hashes (defaults to: true)
|
77
95
|
#
|
96
|
+
# @yield (key) changing key block
|
97
|
+
#
|
78
98
|
# @see Hash::KeyChanger#change_keys
|
79
99
|
#
|
80
100
|
# @example (see #change_keys)
|
@@ -83,58 +103,93 @@ module Darthjee
|
|
83
103
|
end
|
84
104
|
|
85
105
|
# prepend a string to all keys
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
106
|
+
#
|
107
|
+
# @param options [::Hash]
|
108
|
+
# @option options [::TrueClass,::FalseClass]
|
109
|
+
# recursive (true)
|
110
|
+
# flag indicating transformation should be recursive
|
111
|
+
# @option options [::Symbol] type (:keep)
|
112
|
+
# type of the final key
|
113
|
+
# - keep : cast the result to the same type of the
|
114
|
+
# original key
|
115
|
+
# - string : cast the result to be {::String}
|
116
|
+
# - symbol cast the result to be {::Symbol}
|
117
|
+
#
|
118
|
+
# @return [::Hash]
|
119
|
+
#
|
120
|
+
# @see KeyChanger#change_keys
|
121
|
+
#
|
122
|
+
# @example
|
123
|
+
# hash = { :a => 1, "b"=> 2 }
|
124
|
+
#
|
125
|
+
# hash.prepend_to_keys("foo_") # returns {
|
126
|
+
# # :foo_a => 1,
|
127
|
+
# # "foo_b"=> 2
|
128
|
+
# # }
|
92
129
|
def prepend_to_keys(str, options = {})
|
93
130
|
change_key_text(options) do |key|
|
94
131
|
"#{str}#{key}"
|
95
132
|
end
|
96
133
|
end
|
97
134
|
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
135
|
+
# Append a string to all keys
|
136
|
+
#
|
137
|
+
# @param options [::Hash]
|
138
|
+
# @option options [::TrueClass,::FalseClass]
|
139
|
+
# recursive (true)
|
140
|
+
# flag indicating transformation should be recursive
|
141
|
+
# @option options [::Symbol] type (:keep)
|
142
|
+
# type of the final key
|
143
|
+
# - keep : cast the result to the same type of the
|
144
|
+
# original key
|
145
|
+
# - string : cast the result to be {::String}
|
146
|
+
# - symbol cast the result to be {::Symbol}
|
147
|
+
#
|
148
|
+
# @return [::Hash]
|
149
|
+
#
|
150
|
+
# @see KeyChanger#change_keys
|
151
|
+
#
|
152
|
+
# @example (see #prepend_to_keys)
|
105
153
|
def append_to_keys(str, options = {})
|
106
154
|
change_key_text(options) do |key|
|
107
155
|
"#{key}#{str}"
|
108
156
|
end
|
109
157
|
end
|
110
158
|
|
111
|
-
#
|
112
|
-
#
|
113
|
-
#
|
114
|
-
|
159
|
+
# Sorts keys for hash changing the original
|
160
|
+
#
|
161
|
+
# @param options [::Hash]
|
162
|
+
# @option options [::TrueClass,::FalseClass]
|
163
|
+
# recursive (true) flag indicating recursive sorting
|
164
|
+
#
|
165
|
+
# @return [::Hash]
|
166
|
+
#
|
167
|
+
# @see KeySorter#sort
|
168
|
+
#
|
169
|
+
# @example
|
170
|
+
# hash = { b: 1, a: 2 }
|
171
|
+
#
|
172
|
+
# hash.sort_keys # changes hash to { a: 2, b: 1 }
|
173
|
+
def sort_keys!(options = {})
|
115
174
|
Hash::KeysSorter.new(self, **options).sort
|
116
175
|
end
|
117
176
|
|
118
|
-
|
119
|
-
#
|
120
|
-
|
121
|
-
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
128
|
-
#
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
def
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
def change_values!(options = {}, &block)
|
137
|
-
Hash::ValueChanger.new(options, &block).change(self)
|
177
|
+
# Sorts keys for hash without changing the original
|
178
|
+
#
|
179
|
+
# @param options [::Hash]
|
180
|
+
# @option options [::TrueClass,::FalseClass]
|
181
|
+
# recursive (true) flag indicating recursive sorting
|
182
|
+
#
|
183
|
+
# @return [::Hash]
|
184
|
+
#
|
185
|
+
# @see KeySorter#sort
|
186
|
+
#
|
187
|
+
# @example
|
188
|
+
# hash = { b: 1, a: 2 }
|
189
|
+
#
|
190
|
+
# hash.sort_keys # returns { a: 2, b: 1 }
|
191
|
+
def sort_keys(options = {})
|
192
|
+
Hash::KeysSorter.new(deep_dup, **options).sort
|
138
193
|
end
|
139
194
|
|
140
195
|
# Changes the key of the hash without changing it
|
@@ -143,7 +198,10 @@ module Darthjee
|
|
143
198
|
#
|
144
199
|
# @example
|
145
200
|
# hash = { a: 1, b: 2 }
|
146
|
-
# hash.remap_keys(a: :b, b: :c) # returns {
|
201
|
+
# hash.remap_keys(a: :b, b: :c) # returns {
|
202
|
+
# # b: 1,
|
203
|
+
# # c: 2
|
204
|
+
# # }
|
147
205
|
def remap_keys(remap)
|
148
206
|
dup.remap_keys!(remap)
|
149
207
|
end
|
@@ -159,13 +217,27 @@ module Darthjee
|
|
159
217
|
|
160
218
|
private
|
161
219
|
|
162
|
-
#
|
163
|
-
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
#
|
167
|
-
#
|
168
|
-
#
|
220
|
+
# @private
|
221
|
+
# @api private
|
222
|
+
#
|
223
|
+
# Changes the text of the keys
|
224
|
+
#
|
225
|
+
# @param options [::Hash]
|
226
|
+
# @option options [::TrueClass,::FalseClass]
|
227
|
+
# recursive (true)
|
228
|
+
# flag indicating transformation should be recursive
|
229
|
+
# @option options [::Symbol] type (:keep)
|
230
|
+
# type of the final key
|
231
|
+
# - keep : cast the result to the same type of the
|
232
|
+
# original key
|
233
|
+
# - string : cast the result to be {::String}
|
234
|
+
# - symbol cast the result to be {::Symbol}
|
235
|
+
#
|
236
|
+
# @yield (key) changing key block
|
237
|
+
#
|
238
|
+
# @return [::Hash]
|
239
|
+
#
|
240
|
+
# @see KeyChanger
|
169
241
|
def change_key_text(options = {}, &block)
|
170
242
|
Hash::KeyChanger.new(self).change_text(options, &block)
|
171
243
|
end
|