module LookUpApi
Module that holds methods Get, Delete, Update
Public Instance Methods
Private method to lookup inside a document
# File lib/lookup_api.rb, line 5 def _get(key:, document:) if document.instance_of? Array document.each do |array_element| _get(key: key, document: array_element) end elsif document.instance_of? Hash document.each do |hash_key, hash_value| @result = hash_value if hash_key.to_s.eql?(key) _get(key: key, document: hash_value) end end # Return NIL if the key is not present in the document @result end
Returns a document that includes everything but the given key.
document = { a: 1, b: 2, c: 3 } document.nested_delete('c') # => { a: 1, b: 2 } document # => { a: 1, b: 2, c: 3 }
@param document Might be Array of Hashes (or)
Hash of Arrays (or) Hash of array of hash etc...
@param key: Key to delete
@return Result document
# File lib/lookup_api.rb, line 41 def nested_delete(key) # Create deep copy of the object temp = Marshal.load(Marshal.dump(self)) temp.nested_delete!(key) end
Replaces the document without the given key.
document = { a: 1, b: 2, c: 3 } document.nested_delete!('c') # => { a: 1, b: 2 } document # => { a: 1, b: 2 }
@param document Might be Array of Hashes (or)
Hash of Arrays (or) Hash of array of hash etc...
@param key: Key to delete
@return self
# File lib/lookup_api.rb, line 57 def nested_delete!(key) if instance_of? Array each do |array_element| array_element.nested_delete!(key) end elsif instance_of? Hash each do |hash_key, hash_value| if hash_key.to_s.eql?(key) delete(hash_key) else hash_value.nested_delete!(key) end end end self end
Method to get the value from a deeply nested document
@param document Might be Array of Hashes (or)
Hash of Arrays (or) Hash of array of hash etc...
@param key: Key to lookup
@return Value if found else NULL
# File lib/lookup_api.rb, line 27 def nested_get(key) _get(key: key, document: dup) end
Returns a document that has updated key, value pair.
document = { a: 1, b: 2, c: 3 } document.nested_update(key: 'c', value: 4) # => { a: 1, b: 2 , c: 4} document # => { a: 1, b: 2, c: 3 }
@param document Might be Array of Hashes (or)
Hash of Arrays (or) Hash of array of hash etc...
@param key: Key to Update @param value: Value to be updated
@return Result document
# File lib/lookup_api.rb, line 85 def nested_update(key:, value:) # Create deep copy of the object temp = Marshal.load(Marshal.dump(self)) temp.nested_update!(key: key, value: value) end
Replaces the document with the updated key, value pair.
document = { a: 1, b: 2, c: 3 } document.nested_update!(key: 'c', value: 4) # => { a: 1, b: 2 , c: 4} document # => { a: 1, b: 2, c: 4 }
@param document Might be Array of Hashes (or)
Hash of Arrays (or) Hash of array of hash etc...
@param key: Key to Update @param value: Value to be updated
@return self
# File lib/lookup_api.rb, line 102 def nested_update!(key:, value:) if instance_of? Array each do |array_element| array_element.nested_update!(key: key, value: value) end elsif instance_of? Hash each do |hash_key, hash_value| if hash_key.to_s.eql?(key) self[hash_key] = value else hash_value.nested_update!(key: key, value: value) end end end self end