Class: Hash
Instance Method Summary collapse
-
#bury(where, value) ⇒ Object
Stores a value on the location indicated input: where: (Array) value examples: my_hash.bury([:bip, :doom], "doom") # hash of hash my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash.
-
#deep_copy ⇒ Object
(also: #nice_copy)
returns a clean copy of the hash.
-
#generate(select_hash_key = nil, expected_errors: [], **synonyms) ⇒ Object
(also: #gen)
It will generate a new hash with the values generated from the string patterns and select fields specified.
-
#get_values(*keys) ⇒ Object
Get values of the keys supplied from the Hash structure.
-
#has_rkey?(search) ⇒ Boolean
Search if the hash contains the supplied key search can be a string, symbol or regexp.
-
#method_missing(m, *arguments, &block) ⇒ Object
Returns the value of the key specified in case doesn't exist a Hash method with the same name The keys can be accessed also adding underscore to avoid problems with existent methods Also set values in case = supplied examples: my_hash.address.correct my_hash._address._correct my_hash.city my_hash._city my_hash.city="Paris" my_hash.products[1].price.wrong="AAAAA".
-
#nice_filter(keys) ⇒ Object
Filter the hash and returns only the specified keys More info: NiceHash.nice_filter.
-
#nice_merge(hash = nil, return_self = false) ⇒ Object
Merging multi-dimensional hashes.
- #nice_merge!(hash = nil) ⇒ Object
-
#pattern_fields(*select_hash_key) ⇒ Object
(also: #patterns)
It will return an array of the keys where we are using string patterns.
-
#select_fields(*select_hash_key) ⇒ Object
It will return an array of the keys where we are using select values of the kind: "value1|value2|value3".
-
#select_key(select_hash_key) ⇒ Object
It will filter the hash by the key specified on select_hash_key.
-
#set_values(hash_values) ⇒ Object
It will search for the keys supplied and it will set the value specified More info: NiceHash.set_values.
-
#validate(select_hash_key = nil, values_hash_to_validate) ⇒ Object
(also: #val)
Validates a given values_hash_to_validate with string patterns and select fields More info: NiceHash.validate alias: val.
-
#validate_patterns(select_hash_key = nil, values_hash_to_validate) ⇒ Object
Validates a given values_hash_to_validate with string patterns More info: NiceHash.validate.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *arguments, &block) ⇒ Object
Returns the value of the key specified in case doesn't exist a Hash method with the same name The keys can be accessed also adding underscore to avoid problems with existent methods Also set values in case = supplied examples: my_hash.address.correct my_hash._address._correct my_hash.city my_hash._city my_hash.city="Paris" my_hash.products[1].price.wrong="AAAAA"
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/nice/hash/add_to_ruby.rb', line 169 def method_missing(m, *arguments, &block) m = m[1..-1].to_sym if m[0] == "_" if key?(m) self[m] elsif key?(m.to_s) self[m.to_s] elsif m.to_s[-1] == "=" if key?(m.to_s.chop) self[m.to_s.chop] = arguments[0] else self[m.to_s.chop.to_sym] = arguments[0] end else nil end end |
Instance Method Details
#bury(where, value) ⇒ Object
Stores a value on the location indicated input: where: (Array) value examples: my_hash.bury([:bip, :doom], "doom") # hash of hash my_hash.bury([:original, 1, :doom],"the value to set") #hash of array of hash
195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/nice/hash/add_to_ruby.rb', line 195 def bury(where, value) me = self where[0..-2].each do |key| me = me[key] end key = where[-1] key = [key] unless where[-1].is_a?(Array) # for the case same value for different keys, for example pwd1, pwd2, pwd3 key.each do |k| me[k] = value end end |
#deep_copy ⇒ Object Also known as: nice_copy
returns a clean copy of the hash
210 211 212 |
# File 'lib/nice/hash/add_to_ruby.rb', line 210 def deep_copy NiceHash.deep_clone(self) end |
#generate(select_hash_key = nil, expected_errors: [], **synonyms) ⇒ Object Also known as: gen
It will generate a new hash with the values generated from the string patterns and select fields specified. 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 If expected_errors specified the values will be generated with the specified errors. More info: NiceHash.generate alias: gen
230 231 232 |
# File 'lib/nice/hash/add_to_ruby.rb', line 230 def generate(select_hash_key = nil, expected_errors: [], **synonyms) NiceHash.generate(self, select_hash_key, expected_errors: expected_errors, **synonyms) end |
#get_values(*keys) ⇒ Object
Get values of the keys supplied from the Hash structure. More info: NiceHash.get_values
271 272 273 |
# File 'lib/nice/hash/add_to_ruby.rb', line 271 def get_values(*keys) NiceHash.get_values(self, keys.flatten) end |
#has_rkey?(search) ⇒ Boolean
Search if the hash contains the supplied key search can be a string, symbol or regexp. In case of string or symbol it will return true even if only part of the key fits the 'search'
288 289 290 291 |
# File 'lib/nice/hash/add_to_ruby.rb', line 288 def has_rkey?(search) search = Regexp.new(search.to_s) unless search.is_a?(Regexp) !!keys.detect{ |key| key =~ search } end |
#nice_filter(keys) ⇒ Object
Filter the hash and returns only the specified keys More info: NiceHash.nice_filter
297 298 299 |
# File 'lib/nice/hash/add_to_ruby.rb', line 297 def nice_filter(keys) NiceHash.nice_filter(self, keys) end |
#nice_merge(hash = nil, return_self = false) ⇒ Object
Merging multi-dimensional hashes
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/nice/hash/add_to_ruby.rb', line 304 def nice_merge(hash = nil, return_self = false) if return_self base = self else base = self.deep_copy end return base unless hash.is_a?(Hash) hash.each do |key, v| if base[key].is_a?(Hash) && hash[key].is_a?(Hash) base[key].nice_merge!(hash[key]) else base[key]= hash[key] end end base end |
#nice_merge!(hash = nil) ⇒ Object
320 321 322 |
# File 'lib/nice/hash/add_to_ruby.rb', line 320 def nice_merge!(hash = nil) return nice_merge(hash, true) end |
#pattern_fields(*select_hash_key) ⇒ Object Also known as: patterns
It will return an array of the keys where we are using string patterns. More info: NiceHash.pattern_fields
255 256 257 |
# File 'lib/nice/hash/add_to_ruby.rb', line 255 def pattern_fields(*select_hash_key) NiceHash.pattern_fields(self, *select_hash_key) end |
#select_fields(*select_hash_key) ⇒ Object
It will return an array of the keys where we are using select values of the kind: "value1|value2|value3". More info: NiceHash.select_fields
263 264 265 |
# File 'lib/nice/hash/add_to_ruby.rb', line 263 def select_fields(*select_hash_key) NiceHash.select_fields(self, *select_hash_key) end |
#select_key(select_hash_key) ⇒ Object
It will filter the hash by the key specified on select_hash_key. In case a subhash specified on a value it will be selected only the value of the key specified on select_hash_key More info: NiceHash.select_key
219 220 221 |
# File 'lib/nice/hash/add_to_ruby.rb', line 219 def select_key(select_hash_key) NiceHash.select_key(self, select_hash_key) end |
#set_values(hash_values) ⇒ Object
It will search for the keys supplied and it will set the value specified More info: NiceHash.set_values
279 280 281 |
# File 'lib/nice/hash/add_to_ruby.rb', line 279 def set_values(hash_values) NiceHash.set_values(self, hash_values) end |
#validate(select_hash_key = nil, values_hash_to_validate) ⇒ Object Also known as: val
Validates a given values_hash_to_validate with string patterns and select fields More info: NiceHash.validate alias: val
239 240 241 |
# File 'lib/nice/hash/add_to_ruby.rb', line 239 def validate(select_hash_key = nil, values_hash_to_validate) NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: false) end |
#validate_patterns(select_hash_key = nil, values_hash_to_validate) ⇒ Object
Validates a given values_hash_to_validate with string patterns More info: NiceHash.validate
247 248 249 |
# File 'lib/nice/hash/add_to_ruby.rb', line 247 def validate_patterns(select_hash_key = nil, values_hash_to_validate) NiceHash.validate([self, select_hash_key], values_hash_to_validate, only_patterns: true) end |