nice_hash 1.14.0 → 1.15.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/.yardopts +6 -1
- data/README.md +25 -0
- data/lib/nice/hash/add_to_ruby.rb +18 -0
- data/lib/nice_hash.rb +75 -2
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5f7ac29e16ee8ddcaed4f42d365f58c322a0345781ba9579be3b3cb57b067d2e
|
|
4
|
+
data.tar.gz: b1c38c87b0699cdbc167d8e6acc490475bdd085e5fae02c8b8f716c7e843ac59
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0608b70a2dcc0964aa80399d1b03ed40568ad67936ba0260c1bda08a8c6f4d0d90680e1141df051edc5bb60da18353ff5d64b68e285f009e1234e4683aff7409'
|
|
7
|
+
data.tar.gz: f6b2ce5f4108a92f550b6caad39a0d6e37a3e0c8354ebe6e8464a69db8da1e6290d9d7170ccfcbc0fe465fc000e0c26b30ee3358a36866dcc073138c2951d7e4
|
data/.yardopts
CHANGED
|
@@ -2,4 +2,9 @@
|
|
|
2
2
|
--title 'nice_hash - NiceHash creates hashes following certain patterns so your testing will be much easier. You can easily generates all the hashes you want following the criteria you specify.'
|
|
3
3
|
--charset utf-8
|
|
4
4
|
--markup markdown
|
|
5
|
-
|
|
5
|
+
-
|
|
6
|
+
lib/**/*.rb
|
|
7
|
+
lib/nice_hash.rb
|
|
8
|
+
lib/nice/hash/add_to_ruby.rb
|
|
9
|
+
*.md
|
|
10
|
+
LICENSE
|
data/README.md
CHANGED
|
@@ -175,6 +175,8 @@ new_hash = my_hash.generate
|
|
|
175
175
|
puts new_hash.get_values(:address) #> {:address=>"21 Doom Av"}
|
|
176
176
|
puts new_hash.get_values(:address, :zip) #> {:zip=>{:default=>"00000", :correct=>"42782"}, :address=>"21 Doom Av"}
|
|
177
177
|
puts new_hash.get_values(:drawId) #> {:drawId=>["84914", "21158"]}
|
|
178
|
+
#using nested keys
|
|
179
|
+
puts new_hash.get_values(:'draws.drawId') #> {:'draws.drawId'=>["84914", "21158"]}
|
|
178
180
|
```
|
|
179
181
|
|
|
180
182
|
In case of an array of hashes, you will be able also to access the different keys, for example:
|
|
@@ -276,6 +278,29 @@ On this example new_hash will contain:
|
|
|
276
278
|
}
|
|
277
279
|
```
|
|
278
280
|
|
|
281
|
+
Also you can filter the hash you want and return only the speficied keys by using the `nice_filter` method
|
|
282
|
+
|
|
283
|
+
```ruby
|
|
284
|
+
my_hash = { user: {
|
|
285
|
+
address: {
|
|
286
|
+
city: 'Madrid',
|
|
287
|
+
country: 'Spain'
|
|
288
|
+
},
|
|
289
|
+
name: 'Peter',
|
|
290
|
+
age: 33,
|
|
291
|
+
customers: [{name: 'Peter', currency: 'Euro'}, {name:'John', currency: 'Euro'}]
|
|
292
|
+
},
|
|
293
|
+
customer: true
|
|
294
|
+
}
|
|
295
|
+
pp my_hash.nice_filter([:'user.address.city', :'customer', :'user.customers.name'])
|
|
296
|
+
#> {:user=>
|
|
297
|
+
#> {:address=>{:city=>"Madrid"},
|
|
298
|
+
#> :customers=>[{:name=>"Peter"}, {:name=>"John"}]
|
|
299
|
+
#> },
|
|
300
|
+
#> :customer=>true
|
|
301
|
+
#> }
|
|
302
|
+
```
|
|
303
|
+
|
|
279
304
|
### How to generate the hash with the criteria we want
|
|
280
305
|
|
|
281
306
|
You can use the 'generate' method and everytime will be generated a different hash with different values.
|
|
@@ -98,6 +98,16 @@ class Array
|
|
|
98
98
|
json_string = "[#{join(",")}]"
|
|
99
99
|
json_string.json(*keys)
|
|
100
100
|
end
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
###########################################################################
|
|
104
|
+
# Filter the array of hashes and returns only the specified keys
|
|
105
|
+
# More info: NiceHash.nice_filter
|
|
106
|
+
###########################################################################
|
|
107
|
+
def nice_filter(keys)
|
|
108
|
+
NiceHash.nice_filter(self, keys)
|
|
109
|
+
end
|
|
110
|
+
|
|
101
111
|
end
|
|
102
112
|
|
|
103
113
|
require "date"
|
|
@@ -263,6 +273,14 @@ class Hash
|
|
|
263
273
|
!!keys.detect{ |key| key =~ search }
|
|
264
274
|
end
|
|
265
275
|
|
|
276
|
+
###########################################################################
|
|
277
|
+
# Filter the hash and returns only the specified keys
|
|
278
|
+
# More info: NiceHash.nice_filter
|
|
279
|
+
###########################################################################
|
|
280
|
+
def nice_filter(keys)
|
|
281
|
+
NiceHash.nice_filter(self, keys)
|
|
282
|
+
end
|
|
283
|
+
|
|
266
284
|
alias gen generate
|
|
267
285
|
alias val validate
|
|
268
286
|
alias patterns pattern_fields
|
data/lib/nice_hash.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
SP_ADD_TO_RUBY = true if !defined?(SP_ADD_TO_RUBY)
|
|
2
|
+
#todo: add SP_USE_NESTED_KEYS = true
|
|
3
|
+
|
|
2
4
|
require_relative "nice/hash/add_to_ruby" if SP_ADD_TO_RUBY
|
|
3
5
|
|
|
4
6
|
require "string_pattern"
|
|
@@ -772,19 +774,26 @@ class NiceHash
|
|
|
772
774
|
# {"idt"=>3145,"name"=>"Special ticket"}
|
|
773
775
|
# ]
|
|
774
776
|
# }
|
|
775
|
-
# keys: one key (string) or an array of keys
|
|
777
|
+
# keys: one key (string) or an array of keys. key can be a nested key
|
|
776
778
|
# output:
|
|
777
779
|
# a Hash of Arrays with all values found.
|
|
778
780
|
# Example of output with example.get_values("id","name")
|
|
779
781
|
# {"id"=>[334],"name"=>["Peter North"]}
|
|
780
782
|
# Example of output with example.get_values("idt")
|
|
781
783
|
# {"idt"=>[345,3123,3145]}
|
|
784
|
+
# Example of output with example.get_values(:'tickets.idt')
|
|
785
|
+
# {:"tickets.idt"=>[345,3123,3145]}
|
|
782
786
|
#
|
|
783
787
|
####################################################
|
|
784
788
|
def NiceHash.get_values(hashv, keys)
|
|
785
789
|
if keys.kind_of?(String) or keys.kind_of?(Symbol)
|
|
786
790
|
keys = [keys]
|
|
787
791
|
end
|
|
792
|
+
if (keys.grep(/\./)).empty?
|
|
793
|
+
nested = false
|
|
794
|
+
else
|
|
795
|
+
nested = true
|
|
796
|
+
end
|
|
788
797
|
result = Hash.new()
|
|
789
798
|
number_of_results = Hash.new()
|
|
790
799
|
keys.each { |key|
|
|
@@ -823,6 +832,7 @@ class NiceHash
|
|
|
823
832
|
#if keys.include?(key) then
|
|
824
833
|
#added to be able to access the keys with symbols to strings and opposite
|
|
825
834
|
if keys.include?(key) or keys.include?(key.to_s) or keys.include?(key.to_sym)
|
|
835
|
+
#todo: check on next ruby versions since it will be not necessary to do it
|
|
826
836
|
#added to be able to access the keys with symbols to strings and opposite
|
|
827
837
|
key = key.to_s() if keys.include?(key.to_s)
|
|
828
838
|
key = key.to_sym() if keys.include?(key.to_sym)
|
|
@@ -847,7 +857,19 @@ class NiceHash
|
|
|
847
857
|
number_of_results[key] += 1
|
|
848
858
|
end
|
|
849
859
|
if value.kind_of?(Array) or value.kind_of?(Hash)
|
|
850
|
-
|
|
860
|
+
if nested
|
|
861
|
+
keys_nested = []
|
|
862
|
+
keys.grep(/^#{key}\./).each do |k|
|
|
863
|
+
keys_nested << k.to_s.gsub(/^#{key}\./,'').to_sym
|
|
864
|
+
end
|
|
865
|
+
n_result_tmp = get_values(value, keys_nested)
|
|
866
|
+
n_result = {}
|
|
867
|
+
n_result_tmp.each do |k,v|
|
|
868
|
+
n_result["#{key}.#{k}".to_sym] = v
|
|
869
|
+
end
|
|
870
|
+
else
|
|
871
|
+
n_result = get_values(value, keys)
|
|
872
|
+
end
|
|
851
873
|
if n_result != :error
|
|
852
874
|
n_result.each { |n_key, n_value|
|
|
853
875
|
if result.has_key?(n_key)
|
|
@@ -1090,4 +1112,55 @@ class NiceHash
|
|
|
1090
1112
|
end
|
|
1091
1113
|
return hash
|
|
1092
1114
|
end
|
|
1115
|
+
|
|
1116
|
+
##################################################
|
|
1117
|
+
# Filter the hash supplied and returns only the specified keys
|
|
1118
|
+
#
|
|
1119
|
+
# @param hash [Hash] The hash we want to filter
|
|
1120
|
+
# @param keys [Array] [Symbol] Array of symbols or symbol. Nested keys can be used: 'uno.dos.tres'
|
|
1121
|
+
#
|
|
1122
|
+
# @return [Hash]
|
|
1123
|
+
#
|
|
1124
|
+
# @example
|
|
1125
|
+
# my_hash = { user: {
|
|
1126
|
+
# address: {
|
|
1127
|
+
# city: 'Madrid',
|
|
1128
|
+
# country: 'Spain'
|
|
1129
|
+
# },
|
|
1130
|
+
# name: 'Peter',
|
|
1131
|
+
# age: 33,
|
|
1132
|
+
# customers: [{name: 'Peter', currency: 'Euro'}, {name:'John', currency: 'Euro'}]
|
|
1133
|
+
# },
|
|
1134
|
+
# customer: true
|
|
1135
|
+
# }
|
|
1136
|
+
# NiceHash.nice_filter(my_hash, [:'user.address.city', :'customer', :'user.customers.name'])
|
|
1137
|
+
# #> {:user => {:address => {:city => "Madrid"}, :customers => [{:name => "Peter"}, {:name => "John"}]}, :customer => true}
|
|
1138
|
+
##################################################
|
|
1139
|
+
def self.nice_filter(hash, keys)
|
|
1140
|
+
result = {}
|
|
1141
|
+
keys = [keys] unless keys.is_a?(Array)
|
|
1142
|
+
keys.each do |k|
|
|
1143
|
+
kn = k.to_s.split('.')
|
|
1144
|
+
if hash.is_a?(Hash) and hash.key?(k)
|
|
1145
|
+
if hash[k].is_a?(Hash)
|
|
1146
|
+
result[k] = {} unless result.key?(k)
|
|
1147
|
+
else
|
|
1148
|
+
result[k] = hash[k]
|
|
1149
|
+
end
|
|
1150
|
+
elsif hash.is_a?(Hash) and hash.key?(kn.first.to_sym)
|
|
1151
|
+
keys_nested = []
|
|
1152
|
+
keys.grep(/^#{kn.first}\./).each do |k2|
|
|
1153
|
+
keys_nested << k2.to_s.gsub(/^#{kn.first}\./,'').to_sym
|
|
1154
|
+
end
|
|
1155
|
+
result[kn.first.to_sym] = nice_filter(hash[kn.first.to_sym], keys_nested)
|
|
1156
|
+
elsif hash.is_a?(Array)
|
|
1157
|
+
result = []
|
|
1158
|
+
hash.each do |a|
|
|
1159
|
+
res = nice_filter(a, keys)
|
|
1160
|
+
result << res unless res.empty?
|
|
1161
|
+
end
|
|
1162
|
+
end
|
|
1163
|
+
end
|
|
1164
|
+
return result
|
|
1165
|
+
end
|
|
1093
1166
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: nice_hash
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.15.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mario Ruiz
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2019-
|
|
11
|
+
date: 2019-09-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: string_pattern
|