class Amplitude::HashSlicer

Public Class Methods

call(obj, *keys) click to toggle source
# File lib/amplitude/hash_slicer.rb, line 3
def call(obj, *keys)
  return {} if keys.length == 0

  keys = keys[0] if keys[0].is_a?(Array)
  hash = obj.to_h

  if hash.respond_to?(:slice)
    slice_hash(hash, keys)
  else
    map_hash(hash, keys)
  end
end

Private Class Methods

map_hash(hash, keys) click to toggle source
# File lib/amplitude/hash_slicer.rb, line 22
def map_hash(hash, keys)
  pick = hash.keys & keys
  pick.map { |key| [key, hash[key]] }.to_h
end
slice_hash(hash, keys) click to toggle source
# File lib/amplitude/hash_slicer.rb, line 18
def slice_hash(hash, keys)
  hash.slice(*keys)
end