module Lp::Serializable::Utilities

Constants

REDUNDANT_KEYS

Private Instance Methods

expose_data(hash) click to toggle source
# File lib/lp/serializable/utilities.rb, line 22
def expose_data(hash)
  hash[:data]
end
flatten_array_of_hashes(array) click to toggle source
# File lib/lp/serializable/utilities.rb, line 16
def flatten_array_of_hashes(array)
  array.map do |hash|
    flatten_hash(hash)
  end
end
flatten_hash(hash) click to toggle source
# File lib/lp/serializable/utilities.rb, line 36
def flatten_hash(hash)
  return unless hash
  hash.each_with_object({}) do |(key, value), h|
    if hash_and_matches_redundant_keys?(key, value)
      flatten_hash_map(value, h)
    elsif hash_and_has_data_key?(value)
      h[key] = expose_data(value)
    else
      h[key] = value
    end
  end
end
flatten_hash_map(value, hash) click to toggle source
# File lib/lp/serializable/utilities.rb, line 49
def flatten_hash_map(value, hash)
  flatten_hash(value).map do |h_k, h_v|
    hash[h_k.to_s.to_sym] = h_v
  end
end
hash_and_has_data_key?(value) click to toggle source
# File lib/lp/serializable/utilities.rb, line 55
def hash_and_has_data_key?(value)
  value.is_a?(Hash) && value.key?(:data)
end
hash_and_matches_redundant_keys?(key, value) click to toggle source

NOTE Supports native relationship references in serializer

# File lib/lp/serializable/utilities.rb, line 60
def hash_and_matches_redundant_keys?(key, value)
  value.is_a?(Hash) && REDUNDANT_KEYS.any? { |sym| sym == key }
end
nest_data?(resource, nested) click to toggle source
# File lib/lp/serializable/utilities.rb, line 8
def nest_data?(resource, nested)
  if nested
    resource
  else
    nest_resource_under_data_key(resource)
  end
end
nest_resource_under_data_key(resource) click to toggle source
# File lib/lp/serializable/utilities.rb, line 26
def nest_resource_under_data_key(resource)
  hash = new_hash
  hash[:data] = resource
  hash
end
new_hash() click to toggle source
# File lib/lp/serializable/utilities.rb, line 32
def new_hash
  Hash.new(0)
end