class SerializedHashie::Hash

Public Class Methods

dump(obj) click to toggle source
# File lib/serialized_hashie/hash.rb, line 12
def dump(obj)
  hash = dump_hash(obj)
  hash.to_json
end
load(raw_hash) click to toggle source
# File lib/serialized_hashie/hash.rb, line 17
def load(raw_hash)
  hash = JSON.parse(presence(raw_hash) || '{}')
  hash = load_hash(hash)
  new(hash)
end

Private Class Methods

blank?(value) click to toggle source
# File lib/serialized_hashie/hash.rb, line 25
def blank?(value)
  return true if value.nil?
  return true if value.is_a?(String) && value.empty?

  false
end
dump_hash(hash) click to toggle source
# File lib/serialized_hashie/hash.rb, line 36
def dump_hash(hash)
  hash = hash.transform_values do |value|
    dump_value(value)
  end
  hash.reject { |_, v| blank?(v) }
end
dump_value(value) click to toggle source
# File lib/serialized_hashie/hash.rb, line 43
def dump_value(value)
  if blank?(value)
    return nil
  end

  if value.is_a?(::Hash)
    return dump_hash(value)
  end

  if value.is_a?(::Array)
    return value.map { |v| dump_value(v) }.compact
  end

  SerializedHashie.dump_extensions.run(value)
end
load_hash(hash) click to toggle source
# File lib/serialized_hashie/hash.rb, line 59
def load_hash(hash)
  hash.transform_values do |value|
    load_value(value)
  end
end
load_value(value) click to toggle source
# File lib/serialized_hashie/hash.rb, line 65
def load_value(value)
  if value.is_a?(::Hash)
    hash = SerializedHashie.load_hash_extensions.run(value)

    # If the result is still a hash, we'll return that here
    return load_hash(hash) if hash.is_a?(::Hash)

    # If the result is not a hash, we'll just return whatever
    # was returned as a normal value.
    return load_value(hash)
  end

  return value.map { |v| load_value(v) } if value.is_a?(Array)

  SerializedHashie.load_extensions.run(value)
end
presence(value) click to toggle source
# File lib/serialized_hashie/hash.rb, line 32
def presence(value)
  blank?(value) ? nil : value
end