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
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.
-
#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"
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/nice/hash/add_to_ruby.rb', line 158 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
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/nice/hash/add_to_ruby.rb', line 184 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
returns a clean copy of the hash
199 200 201 |
# File 'lib/nice/hash/add_to_ruby.rb', line 199 def deep_copy Marshal.load(Marshal.dump(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
219 220 221 |
# File 'lib/nice/hash/add_to_ruby.rb', line 219 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
260 261 262 |
# File 'lib/nice/hash/add_to_ruby.rb', line 260 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'
277 278 279 280 |
# File 'lib/nice/hash/add_to_ruby.rb', line 277 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
286 287 288 |
# File 'lib/nice/hash/add_to_ruby.rb', line 286 def nice_filter(keys) NiceHash.nice_filter(self, keys) 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
244 245 246 |
# File 'lib/nice/hash/add_to_ruby.rb', line 244 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
252 253 254 |
# File 'lib/nice/hash/add_to_ruby.rb', line 252 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
208 209 210 |
# File 'lib/nice/hash/add_to_ruby.rb', line 208 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
268 269 270 |
# File 'lib/nice/hash/add_to_ruby.rb', line 268 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
228 229 230 |
# File 'lib/nice/hash/add_to_ruby.rb', line 228 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
236 237 238 |
# File 'lib/nice/hash/add_to_ruby.rb', line 236 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 |