nice_hash 1.6.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -3
- data/lib/nice/hash/add_to_ruby.rb +2 -2
- data/lib/nice_hash.rb +36 -30
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64a7a0508b0a8452ff40d40be3d5d947bdeb17abb33e29bcc21ab85f2d59ccb4
|
4
|
+
data.tar.gz: 17efe9a0977fbec3eb5aca744474791627daac82aac02c7db4cd99f16f9b7540
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 721bcb361f65bf07618a17501aa4fa62808cc87a6f824d7219fe69bf408f48d5126d8d94969438014dcfcee0590cb64916cb1858cb9a40b03ba446aa9519aa33
|
7
|
+
data.tar.gz: 686843fbedb61d5380182d0aeb18af8481d7380830cea2a701429c53b025486e606aecf059547cba4ab175fc20c74a4e2642a88d976f469533f36d77b53eca83
|
data/README.md
CHANGED
@@ -374,7 +374,7 @@ pattern_fields_on_correct contains:
|
|
374
374
|
```
|
375
375
|
|
376
376
|
|
377
|
-
|
377
|
+
### dig and bury Hash methods
|
378
378
|
In case you want to access the values on a hash structure by using the key array location, you can use the 'dig' method on the Hash class:
|
379
379
|
|
380
380
|
```ruby
|
@@ -539,7 +539,7 @@ Take a look at a full example: https://gist.github.com/MarioRuiz/824d7a462b62fd8
|
|
539
539
|
|
540
540
|
### Adding other values on real time when calling `generate` method
|
541
541
|
|
542
|
-
If you need a value to be supplied for your key on real time
|
542
|
+
If you need a value to be supplied for your key on real time every time you call the `generate` method you can use `lambda`
|
543
543
|
|
544
544
|
```ruby
|
545
545
|
my_hash = {
|
@@ -570,8 +570,47 @@ This is the output:
|
|
570
570
|
:other=>"2019-01-02T13:41:05.536"}
|
571
571
|
```
|
572
572
|
|
573
|
+
#### Accessing other values of the hash on real time
|
573
574
|
|
574
|
-
|
575
|
+
If you need for example to access another value of the key to generate a value on real time you can use `NiceHash.values`
|
576
|
+
|
577
|
+
Take a look at this example:
|
578
|
+
|
579
|
+
```ruby
|
580
|
+
|
581
|
+
my_hash = {
|
582
|
+
loginname: :"10:Ln",
|
583
|
+
send_email: :"true|false",
|
584
|
+
email: lambda {
|
585
|
+
if NiceHash.values._send_email=='true'
|
586
|
+
:"30-50:@".gen
|
587
|
+
else
|
588
|
+
""
|
589
|
+
end
|
590
|
+
}
|
591
|
+
}
|
592
|
+
|
593
|
+
pp my_hash.gen
|
594
|
+
pp my_hash.gen
|
595
|
+
pp my_hash.gen
|
596
|
+
|
597
|
+
```
|
598
|
+
|
599
|
+
This code will generate a hash where `send_email` can be `true` or `false`. In case it is `true` it will generate a value for the key `email` from 30 to 50 characters valid email, in case it is `false` it will contain empty string.
|
600
|
+
|
601
|
+
This is a possible output of the previous code:
|
602
|
+
|
603
|
+
```ruby
|
604
|
+
{:loginname=>"jnazA9iGN3",
|
605
|
+
:send_email=>"true",
|
606
|
+
:email=>"aRR4SsPaA.0ilh_RW0_y.sQL@goxrssgtkp4df.nkc"}
|
607
|
+
|
608
|
+
{:loginname=>"2CjT9wLMxq", :send_email=>"false", :email=>""}
|
609
|
+
|
610
|
+
{:loginname=>"XlMpgNPlLR", :send_email=>"false", :email=>""}
|
611
|
+
```
|
612
|
+
|
613
|
+
### Other useful methods
|
575
614
|
|
576
615
|
In case you need the time stamp, we added the method `stamp` to the `Time` class
|
577
616
|
|
@@ -118,10 +118,10 @@ end
|
|
118
118
|
|
119
119
|
|
120
120
|
class Time
|
121
|
-
# It will return in the format: '%Y-%m-%dT%H:%M:%S.%
|
121
|
+
# It will return in the format: '%Y-%m-%dT%H:%M:%S.%LZ'
|
122
122
|
# Example: puts Time.now.stamp
|
123
123
|
def stamp
|
124
|
-
return self.strftime('%Y-%m-%dT%H:%M:%S.%
|
124
|
+
return self.strftime('%Y-%m-%dT%H:%M:%S.%LZ')
|
125
125
|
end
|
126
126
|
end
|
127
127
|
|
data/lib/nice_hash.rb
CHANGED
@@ -27,6 +27,10 @@ require 'string_pattern'
|
|
27
27
|
# }
|
28
28
|
###########################################################################
|
29
29
|
class NiceHash
|
30
|
+
class << self
|
31
|
+
attr_reader :values
|
32
|
+
end
|
33
|
+
@values = {}
|
30
34
|
|
31
35
|
###########################################################################
|
32
36
|
# It will filter the hash by the key specified on select_hash_key.
|
@@ -44,7 +48,7 @@ class NiceHash
|
|
44
48
|
# new_hash = my_hash.select_key(:wrong)
|
45
49
|
###########################################################################
|
46
50
|
def NiceHash.select_key(pattern_hash, select_hash_key)
|
47
|
-
|
51
|
+
hashv=Hash.new()
|
48
52
|
|
49
53
|
if pattern_hash.kind_of?(Hash) and pattern_hash.size>0
|
50
54
|
pattern_hash.each {|key, value|
|
@@ -60,7 +64,7 @@ class NiceHash
|
|
60
64
|
array_pattern=false
|
61
65
|
value.each {|v|
|
62
66
|
if (v.kind_of?(String) or v.kind_of?(Symbol)) and StringPattern.analyze(v, silent: true).kind_of?(StringPattern::Pattern)
|
63
|
-
|
67
|
+
hashv[key]=value
|
64
68
|
array_pattern=true
|
65
69
|
break
|
66
70
|
end
|
@@ -71,16 +75,16 @@ class NiceHash
|
|
71
75
|
ret=NiceHash.select_key(v, select_hash_key)
|
72
76
|
value_ret<<ret
|
73
77
|
}
|
74
|
-
|
78
|
+
hashv[key]=value_ret
|
75
79
|
end
|
76
80
|
else
|
77
|
-
|
81
|
+
hashv[key]=value
|
78
82
|
end
|
79
83
|
}
|
80
84
|
else
|
81
85
|
return pattern_hash
|
82
86
|
end
|
83
|
-
return
|
87
|
+
return hashv
|
84
88
|
end
|
85
89
|
|
86
90
|
###########################################################################
|
@@ -296,7 +300,7 @@ class NiceHash
|
|
296
300
|
# new_hash = my_hash.generate(:correct, errors: [:min_length])
|
297
301
|
###########################################################################
|
298
302
|
def NiceHash.generate(pattern_hash, select_hash_key=nil, expected_errors: [], **synonyms)
|
299
|
-
|
303
|
+
hashv=Hash.new()
|
300
304
|
same_values=Hash.new()
|
301
305
|
expected_errors=synonyms[:errors] if synonyms.keys.include?(:errors)
|
302
306
|
if expected_errors.kind_of?(Symbol)
|
@@ -321,45 +325,45 @@ class NiceHash
|
|
321
325
|
|
322
326
|
if value.kind_of?(String) or value.kind_of?(Symbol)
|
323
327
|
if ((StringPattern.optimistic and value.kind_of?(String)) or value.kind_of?(Symbol)) and value.to_s.scan(/^!?\d+-?\d*:.+/).size>0
|
324
|
-
|
328
|
+
hashv[key]=StringPattern.generate(value, expected_errors: expected_errors)
|
325
329
|
elsif ((StringPattern.optimistic and value.kind_of?(String)) or value.kind_of?(Symbol)) and value.to_s.scan(/^([\w\s\-]+\|)+[\w\s\-]+$/).size>0
|
326
330
|
if expected_errors.include?(:min_length) or (expected_errors.include?(:length) and rand.round==0)
|
327
331
|
min=value.to_s.split("|").min {|a, b| a.size <=> b.size}
|
328
|
-
|
332
|
+
hashv[key]=min[0..-2] unless min==""
|
329
333
|
end
|
330
|
-
if !
|
334
|
+
if !hashv.keys.include?(key) and (expected_errors.include?(:max_length) or expected_errors.include?(:length))
|
331
335
|
max=value.to_s.split("|").max {|a, b| a.size <=> b.size}
|
332
|
-
|
336
|
+
hashv[key]=max+max[-1]
|
333
337
|
end
|
334
338
|
if expected_errors.include?(:value) or
|
335
339
|
expected_errors.include?(:string_set_not_allowed) or
|
336
340
|
expected_errors.include?(:required_data)
|
337
|
-
if
|
338
|
-
v=
|
341
|
+
if hashv.keys.include?(key)
|
342
|
+
v=hashv[key]
|
339
343
|
else
|
340
344
|
v=value.to_s.split("|").sample
|
341
345
|
end
|
342
346
|
unless expected_errors.include?(:string_set_not_allowed)
|
343
347
|
v=StringPattern.generate(:"#{v.size}:[#{value.to_s.split("|").join.split(//).uniq.join}]")
|
344
|
-
|
348
|
+
hashv[key]=v unless value.to_s.split("|").include?(v)
|
345
349
|
end
|
346
|
-
unless
|
350
|
+
unless hashv.keys.include?(key)
|
347
351
|
one_wrong_letter=StringPattern.generate(:"1:LN$[%#{value.to_s.split("|").join.split(//).uniq.join}%]")
|
348
352
|
v[rand(v.size)]=one_wrong_letter
|
349
|
-
|
353
|
+
hashv[key]=v unless value.to_s.split("|").include?(v)
|
350
354
|
end
|
351
355
|
end
|
352
|
-
unless
|
353
|
-
|
356
|
+
unless hashv.keys.include?(key)
|
357
|
+
hashv[key]=value.to_s.split("|").sample
|
354
358
|
end
|
355
359
|
else
|
356
|
-
|
360
|
+
hashv[key]=value
|
357
361
|
end
|
358
362
|
elsif value.kind_of?(Array)
|
359
363
|
array_pattern=false
|
360
364
|
value.each {|v|
|
361
365
|
if (v.kind_of?(String) or v.kind_of?(Symbol)) and StringPattern.analyze(v, silent: true).kind_of?(StringPattern::Pattern)
|
362
|
-
|
366
|
+
hashv[key]=StringPattern.generate(value, expected_errors: expected_errors)
|
363
367
|
array_pattern=true
|
364
368
|
break
|
365
369
|
end
|
@@ -371,24 +375,26 @@ class NiceHash
|
|
371
375
|
ret=v if ret.kind_of?(Hash) and ret.size==0
|
372
376
|
value_ret<<ret
|
373
377
|
}
|
374
|
-
|
378
|
+
hashv[key]=value_ret
|
375
379
|
end
|
376
380
|
elsif value.kind_of?(Proc)
|
377
|
-
|
381
|
+
hashv[key]=value.call
|
378
382
|
else
|
379
|
-
|
383
|
+
hashv[key]=value
|
380
384
|
end
|
381
385
|
|
382
386
|
if same_values.include?(key)
|
383
387
|
same_values[key].each {|k|
|
384
|
-
|
388
|
+
hashv[k]=hashv[key]
|
385
389
|
}
|
386
390
|
end
|
387
391
|
|
392
|
+
@values = hashv
|
393
|
+
|
388
394
|
}
|
389
395
|
end
|
390
396
|
|
391
|
-
return
|
397
|
+
return hashv
|
392
398
|
end
|
393
399
|
|
394
400
|
|
@@ -586,7 +592,7 @@ class NiceHash
|
|
586
592
|
# Get values from the Hash structure (array of Hashes allowed)
|
587
593
|
# In case the key supplied doesn't exist in the hash then it will be return nil for that one
|
588
594
|
# input:
|
589
|
-
#
|
595
|
+
# hashv: a simple hash or a hash containing arrays. Example:
|
590
596
|
# example={"id"=>344,
|
591
597
|
# "customer"=>{
|
592
598
|
# "name"=>"Peter Smith",
|
@@ -607,7 +613,7 @@ class NiceHash
|
|
607
613
|
# {"idt"=>[345,3123,3145]}
|
608
614
|
#
|
609
615
|
####################################################
|
610
|
-
def NiceHash.get_values(
|
616
|
+
def NiceHash.get_values(hashv,keys)
|
611
617
|
if keys.kind_of?(String) or keys.kind_of?(Symbol) then
|
612
618
|
keys=[keys]
|
613
619
|
end
|
@@ -616,8 +622,8 @@ class NiceHash
|
|
616
622
|
keys.each {|key|
|
617
623
|
number_of_results[key]=0
|
618
624
|
}
|
619
|
-
if
|
620
|
-
|
625
|
+
if hashv.kind_of?(Array) then
|
626
|
+
hashv.each {|tmp|
|
621
627
|
if tmp.kind_of?(Array) or tmp.kind_of?(Hash) then
|
622
628
|
n_result=get_values(tmp, keys)
|
623
629
|
if n_result!=:error then
|
@@ -644,8 +650,8 @@ class NiceHash
|
|
644
650
|
end
|
645
651
|
end
|
646
652
|
}
|
647
|
-
elsif
|
648
|
-
|
653
|
+
elsif hashv.kind_of?(Hash) then
|
654
|
+
hashv.each {|key, value|
|
649
655
|
#if keys.include?(key) then
|
650
656
|
#added to be able to access the keys with symbols to strings and opposite
|
651
657
|
if keys.include?(key) or keys.include?(key.to_s) or keys.include?(key.to_sym) then
|
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.7.1
|
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-01-
|
11
|
+
date: 2019-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: string_pattern
|
@@ -52,7 +52,7 @@ homepage: https://github.com/MarioRuiz/nice_hash
|
|
52
52
|
licenses:
|
53
53
|
- MIT
|
54
54
|
metadata: {}
|
55
|
-
post_install_message:
|
55
|
+
post_install_message: Thanks for installing! Visit us on https://github.com/MarioRuiz/nice_hash
|
56
56
|
rdoc_options: []
|
57
57
|
require_paths:
|
58
58
|
- lib
|