nice_hash 1.7.3 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +4 -4
  3. data/LICENSE +21 -21
  4. data/README.md +654 -646
  5. data/lib/nice/hash/add_to_ruby.rb +240 -240
  6. data/lib/nice_hash.rb +720 -718
  7. metadata +3 -3
@@ -1,240 +1,240 @@
1
- class String
2
- ###########################################################################
3
- # When comparing an string and an integer, float or nil, it will be automatically converted to string:
4
- # "300" == 300 #will return true
5
- # 200.1=="200.1" #will return true
6
- # ""==nil #will return true
7
- ###########################################################################
8
- def ==(par)
9
- if par.is_a?(Integer) || par.nil? || par.is_a?(Float)
10
- super(par.to_s)
11
- else
12
- super(par)
13
- end
14
- end
15
-
16
- ###########################################################################
17
- # In case the string is a json it will return the keys specified. the keys need to be provided as symbols.
18
- # In case the string is not a json then it will notify the error and return empty Hash
19
- # input:
20
- # keys:
21
- # 1 value with key or an array of keys
22
- # In case the key supplied doesn't exist in the hash then it will be returned nil for that one
23
- # output:
24
- # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash op arrays.
25
- # if no keys given: the json string as a ruby structure.
26
- # if no json string or wrong json string, an empty hash.
27
- ###########################################################################
28
- def json(*keys)
29
- require 'json'
30
- result = {}
31
- begin
32
- feed_symbols = JSON.parse(self, symbolize_names: true)
33
- if !keys.empty?
34
- result_tmp = if keys[0].is_a?(Symbol)
35
- NiceHash.get_values(feed_symbols, keys)
36
- else
37
- {}
38
- end
39
-
40
- if result_tmp.size == 1
41
- result = if result_tmp.values.is_a?(Array) && (result_tmp.values.size == 1)
42
- result_tmp.values[0]
43
- else
44
- result_tmp.values
45
- end
46
- else
47
- result_tmp.each do |key, value|
48
- result[key] = if (value.is_a?(Array) || value.is_a?(Hash)) && (value.size == 1)
49
- value[0]
50
- else
51
- value
52
- end
53
- end
54
- end
55
-
56
- else
57
- result = feed_symbols
58
- end
59
- rescue StandardError => stack
60
- puts stack.to_s
61
- end
62
- result
63
- end
64
- end
65
-
66
- class Array
67
- ###########################################################################
68
- # Stores a value on the location indicated
69
- # input:
70
- # where: (Array)
71
- # value
72
- # examples:
73
- # my_array.bury([3, 0], "doom") # array of array
74
- # my_array.bury([2, 1, :original],"the value to set") #array of array of hash
75
- ###########################################################################
76
- def bury(where, value)
77
- me = self
78
- where[0..-2].each do |key|
79
- me = me[key]
80
- end
81
- me[where[-1]] = value
82
- end
83
-
84
- ###########################################################################
85
- # In case of an array of json strings will return the keys specified. The keys need to be provided as symbols
86
- # input:
87
- # keys:
88
- # 1 value with key or an array of keys
89
- # In case the key supplied doesn't exist in the hash then it will be return nil for that one
90
- # output:
91
- # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays
92
- # if no keys given, an empty hash
93
- ###########################################################################
94
- def json(*keys)
95
- json_string = "[#{join(',')}]"
96
- json_string.json(*keys)
97
- end
98
- end
99
-
100
- require 'date'
101
- class Date
102
- ###########################################################################
103
- # It will generate a random date
104
- # In case days is a Date it will generate until that date
105
- # In case days is an Integer it will generate from the self date + the number of days specified
106
- # examples:
107
- # puts Date.today.random(60) # random date from today to 60 days after
108
- # puts Date.strptime('01-09-2005', '%d-%m-%Y').random(100)
109
- # puts Date.new(2003,10,31).random(Date.today) #Random date from 2003/10/31 to today
110
- ###########################################################################
111
- def random(days)
112
- if days.is_a?(Date)
113
- dif_dates = self - (days + 1)
114
- date_result = self + rand(dif_dates)
115
- date_result
116
- elsif days.is_a?(Integer)
117
- date_result = self + rand(days + 1)
118
- date_result
119
- end
120
- end
121
- end
122
-
123
- class Time
124
- # It will return in the format: '%Y-%m-%dT%H:%M:%S.%LZ'
125
- # Example: puts Time.now.stamp
126
- def stamp
127
- strftime('%Y-%m-%dT%H:%M:%S.%LZ')
128
- end
129
- end
130
-
131
- class Hash
132
- ###########################################################################
133
- # Returns the value of the key specified in case doesn't exist a Hash method with the same name
134
- # The keys can be accessed also adding underscore to avoid problems with existent methods
135
- # Also set values in case = supplied
136
- # examples:
137
- # my_hash.address.correct
138
- # my_hash._address._correct
139
- # my_hash.city
140
- # my_hash._city
141
- # my_hash.city="Paris"
142
- # my_hash.products[1].price.wrong="AAAAA"
143
- ###########################################################################
144
- def method_missing(m, *arguments, &block)
145
- m = m[1..-1].to_sym if m[0] == '_'
146
- if key?(m)
147
- self[m]
148
- elsif m.to_s[-1] == '='
149
- self[m.to_s.chop.to_sym] = arguments[0]
150
- else
151
- super
152
- end
153
- end
154
-
155
- ###########################################################################
156
- # Stores a value on the location indicated
157
- # input:
158
- # where: (Array)
159
- # value
160
- # examples:
161
- # my_hash.bury([:bip, :doom], "doom") # hash of hash
162
- # my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash
163
- ###########################################################################
164
- def bury(where, value)
165
- me = self
166
- where[0..-2].each do |key|
167
- me = me[key]
168
- end
169
- key = where[-1]
170
- key = [key] unless where[-1].is_a?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3
171
- key.each do |k|
172
- me[k] = value
173
- end
174
- end
175
-
176
- ###########################################################################
177
- # It will filter the hash by the key specified on select_hash_key.
178
- # In case a subhash specified on a value it will be selected only the value of the key specified on select_hash_key
179
- # More info: NiceHash.select_key
180
- ###########################################################################
181
- def select_key(select_hash_key)
182
- NiceHash.select_key(self, select_hash_key)
183
- end
184
-
185
- ###########################################################################
186
- # It will generate a new hash with the values generated from the string patterns and select fields specified.
187
- # In case supplied select_hash_key and a subhash specified on a value it will be selected only the value of the key specified on select_hash_key
188
- # If expected_errors specified the values will be generated with the specified errors.
189
- # More info: NiceHash.generate
190
- # alias: gen
191
- ###########################################################################
192
- def generate(select_hash_key = nil, expected_errors: [], **synonyms)
193
- NiceHash.generate(self, select_hash_key, expected_errors: expected_errors, **synonyms)
194
- end
195
-
196
- ###########################################################################
197
- # Validates a given values_hash_to_validate with string patterns and select fields
198
- # More info: NiceHash.validate
199
- # alias: val
200
- ###########################################################################
201
- def validate(select_hash_key = nil, values_hash_to_validate)
202
- NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: false)
203
- end
204
-
205
- ###########################################################################
206
- # Validates a given values_hash_to_validate with string patterns
207
- # More info: NiceHash.validate
208
- ###########################################################################
209
- def validate_patterns(select_hash_key = nil, values_hash_to_validate)
210
- NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: true)
211
- end
212
-
213
- ###########################################################################
214
- # It will return an array of the keys where we are using string patterns.
215
- # More info: NiceHash.pattern_fields
216
- ###########################################################################
217
- def pattern_fields(*select_hash_key)
218
- NiceHash.pattern_fields(self, *select_hash_key)
219
- end
220
-
221
- ###########################################################################
222
- # It will return an array of the keys where we are using select values of the kind: "value1|value2|value3".
223
- # More info: NiceHash.select_fields
224
- ###########################################################################
225
- def select_fields(*select_hash_key)
226
- NiceHash.select_fields(self, *select_hash_key)
227
- end
228
-
229
- ###########################################################################
230
- # Get values of the keys supplied from the Hash structure.
231
- # More info: NiceHash.get_values
232
- ###########################################################################
233
- def get_values(*keys)
234
- NiceHash.get_values(self, keys)
235
- end
236
-
237
- alias gen generate
238
- alias val validate
239
- alias patterns pattern_fields
240
- end
1
+ class String
2
+ ###########################################################################
3
+ # When comparing an string and an integer, float or nil, it will be automatically converted to string:
4
+ # "300" == 300 #will return true
5
+ # 200.1=="200.1" #will return true
6
+ # ""==nil #will return true
7
+ ###########################################################################
8
+ def ==(par)
9
+ if par.is_a?(Integer) || par.nil? || par.is_a?(Float)
10
+ super(par.to_s)
11
+ else
12
+ super(par)
13
+ end
14
+ end
15
+
16
+ ###########################################################################
17
+ # In case the string is a json it will return the keys specified. the keys need to be provided as symbols.
18
+ # In case the string is not a json then it will notify the error and return empty Hash
19
+ # input:
20
+ # keys:
21
+ # 1 value with key or an array of keys
22
+ # In case the key supplied doesn't exist in the hash then it will be returned nil for that one
23
+ # output:
24
+ # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash op arrays.
25
+ # if no keys given: the json string as a ruby structure.
26
+ # if no json string or wrong json string, an empty hash.
27
+ ###########################################################################
28
+ def json(*keys)
29
+ require 'json'
30
+ result = {}
31
+ begin
32
+ feed_symbols = JSON.parse(self, symbolize_names: true)
33
+ if !keys.empty?
34
+ result_tmp = if keys[0].is_a?(Symbol)
35
+ NiceHash.get_values(feed_symbols, keys)
36
+ else
37
+ {}
38
+ end
39
+
40
+ if result_tmp.size == 1
41
+ result = if result_tmp.values.is_a?(Array) && (result_tmp.values.size == 1)
42
+ result_tmp.values[0]
43
+ else
44
+ result_tmp.values
45
+ end
46
+ else
47
+ result_tmp.each do |key, value|
48
+ result[key] = if (value.is_a?(Array) || value.is_a?(Hash)) && (value.size == 1)
49
+ value[0]
50
+ else
51
+ value
52
+ end
53
+ end
54
+ end
55
+
56
+ else
57
+ result = feed_symbols
58
+ end
59
+ rescue StandardError => stack
60
+ puts stack.to_s
61
+ end
62
+ result
63
+ end
64
+ end
65
+
66
+ class Array
67
+ ###########################################################################
68
+ # Stores a value on the location indicated
69
+ # input:
70
+ # where: (Array)
71
+ # value
72
+ # examples:
73
+ # my_array.bury([3, 0], "doom") # array of array
74
+ # my_array.bury([2, 1, :original],"the value to set") #array of array of hash
75
+ ###########################################################################
76
+ def bury(where, value)
77
+ me = self
78
+ where[0..-2].each do |key|
79
+ me = me[key]
80
+ end
81
+ me[where[-1]] = value
82
+ end
83
+
84
+ ###########################################################################
85
+ # In case of an array of json strings will return the keys specified. The keys need to be provided as symbols
86
+ # input:
87
+ # keys:
88
+ # 1 value with key or an array of keys
89
+ # In case the key supplied doesn't exist in the hash then it will be return nil for that one
90
+ # output:
91
+ # if keys given: a hash of (keys, values) or the value, if the key is found more than once in the json string, then it will be return a hash of arrays
92
+ # if no keys given, an empty hash
93
+ ###########################################################################
94
+ def json(*keys)
95
+ json_string = "[#{join(',')}]"
96
+ json_string.json(*keys)
97
+ end
98
+ end
99
+
100
+ require 'date'
101
+ class Date
102
+ ###########################################################################
103
+ # It will generate a random date
104
+ # In case days is a Date it will generate until that date
105
+ # In case days is an Integer it will generate from the self date + the number of days specified
106
+ # examples:
107
+ # puts Date.today.random(60) # random date from today to 60 days after
108
+ # puts Date.strptime('01-09-2005', '%d-%m-%Y').random(100)
109
+ # puts Date.new(2003,10,31).random(Date.today) #Random date from 2003/10/31 to today
110
+ ###########################################################################
111
+ def random(days)
112
+ if days.is_a?(Date)
113
+ dif_dates = self - (days + 1)
114
+ date_result = self + rand(dif_dates)
115
+ date_result
116
+ elsif days.is_a?(Integer)
117
+ date_result = self + rand(days + 1)
118
+ date_result
119
+ end
120
+ end
121
+ end
122
+
123
+ class Time
124
+ # It will return in the format: '%Y-%m-%dT%H:%M:%S.%LZ'
125
+ # Example: puts Time.now.stamp
126
+ def stamp
127
+ strftime('%Y-%m-%dT%H:%M:%S.%LZ')
128
+ end
129
+ end
130
+
131
+ class Hash
132
+ ###########################################################################
133
+ # Returns the value of the key specified in case doesn't exist a Hash method with the same name
134
+ # The keys can be accessed also adding underscore to avoid problems with existent methods
135
+ # Also set values in case = supplied
136
+ # examples:
137
+ # my_hash.address.correct
138
+ # my_hash._address._correct
139
+ # my_hash.city
140
+ # my_hash._city
141
+ # my_hash.city="Paris"
142
+ # my_hash.products[1].price.wrong="AAAAA"
143
+ ###########################################################################
144
+ def method_missing(m, *arguments, &block)
145
+ m = m[1..-1].to_sym if m[0] == '_'
146
+ if key?(m)
147
+ self[m]
148
+ elsif m.to_s[-1] == '='
149
+ self[m.to_s.chop.to_sym] = arguments[0]
150
+ else
151
+ super
152
+ end
153
+ end
154
+
155
+ ###########################################################################
156
+ # Stores a value on the location indicated
157
+ # input:
158
+ # where: (Array)
159
+ # value
160
+ # examples:
161
+ # my_hash.bury([:bip, :doom], "doom") # hash of hash
162
+ # my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash
163
+ ###########################################################################
164
+ def bury(where, value)
165
+ me = self
166
+ where[0..-2].each do |key|
167
+ me = me[key]
168
+ end
169
+ key = where[-1]
170
+ key = [key] unless where[-1].is_a?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3
171
+ key.each do |k|
172
+ me[k] = value
173
+ end
174
+ end
175
+
176
+ ###########################################################################
177
+ # It will filter the hash by the key specified on select_hash_key.
178
+ # In case a subhash specified on a value it will be selected only the value of the key specified on select_hash_key
179
+ # More info: NiceHash.select_key
180
+ ###########################################################################
181
+ def select_key(select_hash_key)
182
+ NiceHash.select_key(self, select_hash_key)
183
+ end
184
+
185
+ ###########################################################################
186
+ # It will generate a new hash with the values generated from the string patterns and select fields specified.
187
+ # In case supplied select_hash_key and a subhash specified on a value it will be selected only the value of the key specified on select_hash_key
188
+ # If expected_errors specified the values will be generated with the specified errors.
189
+ # More info: NiceHash.generate
190
+ # alias: gen
191
+ ###########################################################################
192
+ def generate(select_hash_key = nil, expected_errors: [], **synonyms)
193
+ NiceHash.generate(self, select_hash_key, expected_errors: expected_errors, **synonyms)
194
+ end
195
+
196
+ ###########################################################################
197
+ # Validates a given values_hash_to_validate with string patterns and select fields
198
+ # More info: NiceHash.validate
199
+ # alias: val
200
+ ###########################################################################
201
+ def validate(select_hash_key = nil, values_hash_to_validate)
202
+ NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: false)
203
+ end
204
+
205
+ ###########################################################################
206
+ # Validates a given values_hash_to_validate with string patterns
207
+ # More info: NiceHash.validate
208
+ ###########################################################################
209
+ def validate_patterns(select_hash_key = nil, values_hash_to_validate)
210
+ NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: true)
211
+ end
212
+
213
+ ###########################################################################
214
+ # It will return an array of the keys where we are using string patterns.
215
+ # More info: NiceHash.pattern_fields
216
+ ###########################################################################
217
+ def pattern_fields(*select_hash_key)
218
+ NiceHash.pattern_fields(self, *select_hash_key)
219
+ end
220
+
221
+ ###########################################################################
222
+ # It will return an array of the keys where we are using select values of the kind: "value1|value2|value3".
223
+ # More info: NiceHash.select_fields
224
+ ###########################################################################
225
+ def select_fields(*select_hash_key)
226
+ NiceHash.select_fields(self, *select_hash_key)
227
+ end
228
+
229
+ ###########################################################################
230
+ # Get values of the keys supplied from the Hash structure.
231
+ # More info: NiceHash.get_values
232
+ ###########################################################################
233
+ def get_values(*keys)
234
+ NiceHash.get_values(self, keys)
235
+ end
236
+
237
+ alias gen generate
238
+ alias val validate
239
+ alias patterns pattern_fields
240
+ end