class Hash

Constants

DEFAULT_EMBED_AS

Public Instance Methods

add_optional(field, embed_as: DEFAULT_EMBED_AS) click to toggle source

Add field to hash as is or embedded under a key given by embed_as. Addition only occurs when field contains data. @param field [Hash] The fields to add to the hash @param embed_as [#to_s] The key under which the field should be embedded

in the hash

@return [Hash] creates a new has with modifications

# File lib/hf.rb, line 87
def add_optional(field, embed_as: DEFAULT_EMBED_AS)
  return self.merge(field) if embed_as.eql?(DEFAULT_EMBED_AS)
  return self.clone if field.empty?
  self.merge({embed_as => field})
end
add_optional!(field, embed_as: DEFAULT_EMBED_AS) click to toggle source

Add field to hash as is or embedded under a key given by embed_as. Addition only occurs when field contains data. @param field [Hash] The fields to add to the hash @param embed_as [#to_s] The key under which the field should be embedded

in the hash

@return [Hash] replace existing hash with modified hash

# File lib/hf.rb, line 75
def add_optional!(field, embed_as: DEFAULT_EMBED_AS)
  return self.merge!(field) if embed_as.eql?(DEFAULT_EMBED_AS)
  return self if field.empty?
  self.merge!({embed_as => field})
end
drop(*keys) click to toggle source

@param (see slice) @return [Hash] a Hash containing none of the keys given

# File lib/hf.rb, line 101
def drop(*keys)
  self.reject{|k,_| keys.include?(k)}
end
slice(*keys) click to toggle source

@param keys [HashKeyArray] @return [Hash] a Hash containing only the keys given.

# File lib/hf.rb, line 95
def slice(*keys)
  self.select{|k,_| keys.include?(k)}
end