Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/hash/add_to_ruby.rb

Instance Method Summary collapse

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"



141
142
143
144
145
146
147
148
149
150
# File 'lib/nice/hash/add_to_ruby.rb', line 141

def method_missing(m, *arguments, &block)
  m = m[1..-1].to_sym if m[0] == '_'
  if key?(m)
    self[m]
  elsif m.to_s[-1] == '='
    self[m.to_s.chop.to_sym] = arguments[0]
  else
    super
  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



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nice/hash/add_to_ruby.rb', line 161

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

#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



189
190
191
# File 'lib/nice/hash/add_to_ruby.rb', line 189

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



230
231
232
# File 'lib/nice/hash/add_to_ruby.rb', line 230

def get_values(*keys)
  NiceHash.get_values(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



214
215
216
# File 'lib/nice/hash/add_to_ruby.rb', line 214

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



222
223
224
# File 'lib/nice/hash/add_to_ruby.rb', line 222

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



178
179
180
# File 'lib/nice/hash/add_to_ruby.rb', line 178

def select_key(select_hash_key)
  NiceHash.select_key(self, select_hash_key)
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



198
199
200
# File 'lib/nice/hash/add_to_ruby.rb', line 198

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



206
207
208
# File 'lib/nice/hash/add_to_ruby.rb', line 206

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