nice_hash 1.15.6 → 1.17.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -8
- data/lib/nice/hash/add_to_ruby.rb +13 -5
- data/lib/nice/hash/compare_structure.rb +4 -1
- data/lib/nice/hash/generate.rb +15 -4
- data/lib/nice/hash/validate.rb +29 -8
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8319335fc75dd3ea181a84bdc8a67adf979b42e2fde45b4b2c1686927b877886
|
4
|
+
data.tar.gz: f0ca1147eb97015392f846a8449dac01c4df5f223336fc69aa7c83b8dd5dbc01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac548edf37ae74f11a21368c8b5f02d061b33a6fe2749f39f88cb10371e93701fe0ce53e548cf7c67ac968e2fced504fa709716597c0e498e26db0bc29863ee4
|
7
|
+
data.tar.gz: 7fe62a823ffe2b59048225847d83d684792c0097801d72707f4a74e89f138c6a7d61b6181a919964671463754ac7ac990646a8cb3d01f9a2aaaff3317bf8d9ae
|
data/README.md
CHANGED
@@ -116,7 +116,8 @@ my_hash={
|
|
116
116
|
country: {default: 'Spain', correct: ["Spain", "Iceland", "Serbia", "Denmark", "Canada", "Italy", "Austria"].join("|")}, #one of these values
|
117
117
|
mobilePhone: {default: '(987)654321', correct: ['(', :'3:N', ')', :'6-8:N']},
|
118
118
|
sex: :"male|female|other", #any of these values
|
119
|
-
display: true
|
119
|
+
display: true,
|
120
|
+
user_names: [ :'3-10:L' ]
|
120
121
|
}
|
121
122
|
```
|
122
123
|
|
@@ -138,6 +139,7 @@ Explanations of the different fields:
|
|
138
139
|
country: correct: one of these values "Spain", "Iceland", "Serbia", "Denmark", "Canada", "Italy", "Austria"
|
139
140
|
mobilePhone: correct: a sting pattern with one of the next: "(nnn) nnnnnn", "(nnn) nnnnnnn", "(nnn) nnnnnnnn"
|
140
141
|
sex: 'male' or 'female' or 'other'
|
142
|
+
user_names: array of strings from 3 to 10 letters
|
141
143
|
|
142
144
|
So in case you want to assign to a key a string pattern value like for example in loginame, you need to specify the string pattern as a symbol :"5-10:/xn/"
|
143
145
|
|
@@ -303,7 +305,8 @@ On this example new_hash will contain:
|
|
303
305
|
country: ["Spain", "Iceland", "Serbia", "Denmark", "Canada", "Italy", "Austria"].join("|"), #one of these values
|
304
306
|
mobilePhone: ['(', :'3:N', ')', :'6-8:N'],
|
305
307
|
sex: :"male|female|other", #any of these values
|
306
|
-
display: true
|
308
|
+
display: true,
|
309
|
+
user_names: [ :'3-10:L' ]
|
307
310
|
}
|
308
311
|
```
|
309
312
|
|
@@ -383,7 +386,9 @@ In case of filtering by :correct new_hash would have a value like this for examp
|
|
383
386
|
:country=>"Denmark",
|
384
387
|
:mobilePhone=>"(707)8782080",
|
385
388
|
:sex=>"male",
|
386
|
-
:display=>true
|
389
|
+
:display=>true,
|
390
|
+
:user_names=>["FFrriNdw", "ACc"]
|
391
|
+
}
|
387
392
|
```
|
388
393
|
|
389
394
|
In case no filtering you will get all the values for all keys
|
@@ -438,7 +443,9 @@ On this example wrong_min_length will contain something like:
|
|
438
443
|
:country=>"Spai",
|
439
444
|
:mobilePhone=>"(237)17640431",
|
440
445
|
:sex=>"mal",
|
441
|
-
:display=>true
|
446
|
+
:display=>true,
|
447
|
+
:user_names=>["FF"]
|
448
|
+
}
|
442
449
|
```
|
443
450
|
|
444
451
|
### Return the select_fields or the pattern_fields
|
@@ -488,7 +495,8 @@ all_pattern_fields contains:
|
|
488
495
|
[:draws, 1, :drawName],
|
489
496
|
[:draws, 1, :owner, :correct],
|
490
497
|
[:zip, :correct],
|
491
|
-
[:mobilePhone, :correct]]
|
498
|
+
[:mobilePhone, :correct]],
|
499
|
+
[:user_names]
|
492
500
|
```
|
493
501
|
|
494
502
|
pattern_fields_on_correct contains:
|
@@ -504,7 +512,8 @@ pattern_fields_on_correct contains:
|
|
504
512
|
[:draws, 1, :drawName],
|
505
513
|
[:draws, 1, :owner],
|
506
514
|
[:zip],
|
507
|
-
[:mobilePhone]]
|
515
|
+
[:mobilePhone]],
|
516
|
+
[:user_names]
|
508
517
|
```
|
509
518
|
|
510
519
|
|
@@ -582,7 +591,9 @@ After using the bury method default_values will contain:
|
|
582
591
|
:country=>"Spain",
|
583
592
|
:mobilePhone=>"(987)654321",
|
584
593
|
:sex=>"male",
|
585
|
-
:display=>true
|
594
|
+
:display=>true,
|
595
|
+
:user_names=>["FFrriNdw", "ACc"]
|
596
|
+
}
|
586
597
|
```
|
587
598
|
|
588
599
|
### Validating hashes
|
@@ -620,7 +631,9 @@ values = {
|
|
620
631
|
:country=>"Iceland",
|
621
632
|
:mobilePhone=>"(441)97037845",
|
622
633
|
:sex=>"male",
|
623
|
-
:display=>true
|
634
|
+
:display=>true,
|
635
|
+
:user_names=>['ddfdsa']
|
636
|
+
}
|
624
637
|
```
|
625
638
|
|
626
639
|
To validate those values against the patterns defined on my_hash:
|
@@ -801,6 +814,8 @@ The last parameter (patterns) allow you to add verification of data values follo
|
|
801
814
|
Valid patterns:
|
802
815
|
- a regular expression
|
803
816
|
- any string_pattern, more info: string_pattern project: https://github.com/MarioRuiz/string_pattern
|
817
|
+
- array of a string_pattern:
|
818
|
+
- [ :'10-20:L' ]
|
804
819
|
- Boolean: specifying Boolean will check if the value is TrueClass or FalseClass
|
805
820
|
- ranges: Any kind of numeric ranges, for example:
|
806
821
|
- 10..400
|
@@ -885,6 +900,17 @@ value.is_a?(Boolean) #> true
|
|
885
900
|
text.is_a?(Boolean) #> false
|
886
901
|
|
887
902
|
```
|
903
|
+
|
904
|
+
#### in?(array)
|
905
|
+
Added method in? to all objects that accepts array as a parameter.
|
906
|
+
|
907
|
+
```ruby
|
908
|
+
'uno'.in?(['uno','dos']) #> true
|
909
|
+
:uno.in? [:uno, :dos] #> true
|
910
|
+
5.in? [1,2,3,4,6] #> false
|
911
|
+
|
912
|
+
```
|
913
|
+
|
888
914
|
### Other tools integration
|
889
915
|
|
890
916
|
#### Tabulo
|
@@ -292,12 +292,12 @@ class Hash
|
|
292
292
|
alias patterns pattern_fields
|
293
293
|
end
|
294
294
|
|
295
|
-
###########################################################################
|
296
|
-
# symbolize hash of arrays and array of hashes
|
297
|
-
# Taken from gist https://gist.github.com/Integralist/9503099
|
298
|
-
# Thanks to @integralist
|
299
|
-
###########################################################################
|
300
295
|
class Object
|
296
|
+
###########################################################################
|
297
|
+
# symbolize hash of arrays and array of hashes
|
298
|
+
# Taken from gist https://gist.github.com/Integralist/9503099
|
299
|
+
# Thanks to @integralist
|
300
|
+
###########################################################################
|
301
301
|
def deep_symbolize_keys
|
302
302
|
if is_a? Hash
|
303
303
|
return reduce({}) do |memo, (k, v)|
|
@@ -312,6 +312,14 @@ class Object
|
|
312
312
|
end
|
313
313
|
self
|
314
314
|
end
|
315
|
+
|
316
|
+
###########################################################################
|
317
|
+
# include? but the opposite. Check if the object is included on the array
|
318
|
+
###########################################################################
|
319
|
+
def in?(array)
|
320
|
+
array.include?(self)
|
321
|
+
end
|
322
|
+
|
315
323
|
end
|
316
324
|
|
317
325
|
class Array
|
@@ -23,6 +23,7 @@ class NiceHash
|
|
23
23
|
# #>true
|
24
24
|
##################################################
|
25
25
|
def NiceHash.compare_structure(structure, replica, compare_only_if_exist_key = false, patterns = {})
|
26
|
+
patterns = {} if patterns.nil?
|
26
27
|
unless structure.class == replica.class or
|
27
28
|
((structure.is_a?(TrueClass) or structure.is_a?(FalseClass)) and (replica.is_a?(TrueClass) or replica.is_a?(FalseClass)))
|
28
29
|
puts "NiceHash.compare_structure: different object type #{structure.class} is not #{replica.class}. expected: #{structure.inspect}. found: #{replica.inspect}."
|
@@ -30,16 +31,18 @@ class NiceHash
|
|
30
31
|
end
|
31
32
|
success = true
|
32
33
|
if structure.is_a?(Hash)
|
34
|
+
patterns = {} unless patterns.is_a?(Hash)
|
33
35
|
structure.each do |key, value|
|
34
36
|
if patterns.key?(key) and replica.key?(key)
|
35
37
|
unless (patterns[key].is_a?(Array) and replica[key].is_a?(Array) and replica[key].empty?) or
|
38
|
+
(compare_only_if_exist_key and replica.key?(key) and replica[key].nil?) or
|
36
39
|
{key => patterns[key]}.validate({key => replica[key]}).empty?
|
37
40
|
puts "NiceHash.compare_structure: key :#{key} not following pattern #{patterns[key]}. value: #{replica[key]}"
|
38
41
|
success = false
|
39
42
|
end
|
40
43
|
end
|
41
44
|
|
42
|
-
if compare_only_if_exist_key and replica.key?(key)
|
45
|
+
if compare_only_if_exist_key and replica.key?(key) and !replica[key].nil?
|
43
46
|
unless compare_structure(value, replica[key], compare_only_if_exist_key, patterns)
|
44
47
|
puts "NiceHash.compare_structure: key :#{key} different."
|
45
48
|
success = false
|
data/lib/nice/hash/generate.rb
CHANGED
@@ -111,13 +111,24 @@ class NiceHash
|
|
111
111
|
end
|
112
112
|
elsif value.kind_of?(Array)
|
113
113
|
array_pattern = false
|
114
|
-
value.
|
114
|
+
if value.size == 1
|
115
|
+
v = value[0]
|
115
116
|
if (v.kind_of?(String) or v.kind_of?(Symbol)) and StringPattern.analyze(v, silent: true).kind_of?(StringPattern::Pattern)
|
116
|
-
hashv[key] =
|
117
|
+
hashv[key] = []
|
118
|
+
(rand(5)+1).times do
|
119
|
+
hashv[key]<<StringPattern.generate(v, expected_errors: expected_errors)
|
120
|
+
end
|
117
121
|
array_pattern = true
|
118
|
-
break
|
119
122
|
end
|
120
|
-
|
123
|
+
else
|
124
|
+
value.each { |v|
|
125
|
+
if (v.kind_of?(String) or v.kind_of?(Symbol)) and StringPattern.analyze(v, silent: true).kind_of?(StringPattern::Pattern)
|
126
|
+
hashv[key] = StringPattern.generate(value, expected_errors: expected_errors)
|
127
|
+
array_pattern = true
|
128
|
+
break
|
129
|
+
end
|
130
|
+
}
|
131
|
+
end
|
121
132
|
unless array_pattern
|
122
133
|
value_ret = Array.new
|
123
134
|
value.each { |v|
|
data/lib/nice/hash/validate.rb
CHANGED
@@ -93,10 +93,18 @@ class NiceHash
|
|
93
93
|
results[key] = false unless value.to_s == values[key].to_s
|
94
94
|
end
|
95
95
|
elsif value.kind_of?(Range)
|
96
|
-
if
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
if value.size.to_s == 'Infinity'
|
97
|
+
if values[key].class != value.first.class
|
98
|
+
results[key] = false
|
99
|
+
elsif values[key] < value.first
|
100
|
+
results[key] = false
|
101
|
+
end
|
102
|
+
else
|
103
|
+
if values[key].class != value.first.class or values[key].class != value.last.class
|
104
|
+
results[key] = false
|
105
|
+
elsif values[key] < value.first or values[key] > value.last
|
106
|
+
results[key] = false
|
107
|
+
end
|
100
108
|
end
|
101
109
|
elsif value.kind_of?(Class) and value == DateTime
|
102
110
|
if values[key].is_a?(String) and values[key].size == 24
|
@@ -118,8 +126,19 @@ class NiceHash
|
|
118
126
|
complex_data = false
|
119
127
|
value.each { |v|
|
120
128
|
if (v.kind_of?(String) or v.kind_of?(Symbol)) and StringPattern.analyze(v, silent: true).kind_of?(StringPattern::Pattern)
|
121
|
-
|
122
|
-
|
129
|
+
if value.is_a?(Array) and value.size == 1 and values[key].is_a?(Array)
|
130
|
+
results[key] ||= []
|
131
|
+
values[key].each do |vk|
|
132
|
+
res = StringPattern.validate(pattern: value[0], text: vk)
|
133
|
+
results[key] << res
|
134
|
+
end
|
135
|
+
if results[key].flatten.size == 0
|
136
|
+
results.delete(key)
|
137
|
+
end
|
138
|
+
else
|
139
|
+
res = StringPattern.validate(pattern: value, text: values[key])
|
140
|
+
results[key] = res if res == false
|
141
|
+
end
|
123
142
|
array_pattern = true
|
124
143
|
break
|
125
144
|
elsif v.kind_of?(Hash) or v.kind_of?(Array) or v.kind_of?(Struct)
|
@@ -128,7 +147,7 @@ class NiceHash
|
|
128
147
|
end
|
129
148
|
}
|
130
149
|
unless array_pattern or results.include?(key)
|
131
|
-
if value.size == 1 and values[key].size >
|
150
|
+
if value.size == 1 and values[key].is_a?(Array) and values[key].size > 0
|
132
151
|
# for the case value == ['Ford|Newton|Seat'] and values == ['Ford', 'Newton', 'Ford']
|
133
152
|
i = 0
|
134
153
|
if values[key].class == value.class
|
@@ -155,10 +174,12 @@ class NiceHash
|
|
155
174
|
value.each { |v|
|
156
175
|
if v.is_a?(Hash)
|
157
176
|
res = NiceHash.validate([v, select_hash_key], values[key][i], only_patterns: only_patterns)
|
158
|
-
|
177
|
+
elsif v.is_a?(Array)
|
159
178
|
# for the case {cars: ['Ford|Newton|Seat']}
|
160
179
|
res = NiceHash.validate([{ key => v }, select_hash_key], { key => values[key][i] }, only_patterns: only_patterns)
|
161
180
|
array_pattern = true
|
181
|
+
else
|
182
|
+
res = []
|
162
183
|
end
|
163
184
|
if res.size > 0
|
164
185
|
results[key] = Array.new() if !results.keys.include?(key)
|
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.17.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: 2020-
|
11
|
+
date: 2020-08-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: string_pattern
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '2.2'
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 2.2.
|
22
|
+
version: 2.2.2
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,27 +29,27 @@ dependencies:
|
|
29
29
|
version: '2.2'
|
30
30
|
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: 2.2.
|
32
|
+
version: 2.2.2
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rspec
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: 3.
|
39
|
+
version: 3.9.0
|
40
40
|
- - "~>"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '3.
|
42
|
+
version: '3.9'
|
43
43
|
type: :development
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
47
|
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: 3.
|
49
|
+
version: 3.9.0
|
50
50
|
- - "~>"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '3.
|
52
|
+
version: '3.9'
|
53
53
|
description: 'NiceHash creates hashes following certain patterns so your testing will
|
54
54
|
be much easier. Parse and filter JSON. You can easily generate all the hashes you
|
55
55
|
want following the criteria you specify. Many other features coming to Hash class
|