Module: HashSerializer::Serializer

Defined in:
lib/hash_serializer/serializer.rb

Overview

Serializes Ruby objects to JSON for storage in Postgres tables

Class Method Summary collapse

Class Method Details

.dump(hash) ⇒ Object

Dump the contents of hash to JSON

Example:

>> HashSerializer.dump({name: 'John'})
=> "{'name': 'John'}"

Parameters:

  • hash (Hash)


14
15
16
# File 'lib/hash_serializer/serializer.rb', line 14

def self.dump(hash)
  hash.to_json
end

.load(hash) ⇒ Object

Loads the contents of hash from JSON if hash is a String or returns the array otherwise

Example:

>> HashSerializer.load("{name: 'John'}")
=> {'name': 'John'}

>> HashSerializer.load({name: 'John'})
=> {'name': 'John'}

>> HashSerializer.load(nil)
=> {}

Parameters:

  • hash (String, Hash)


31
32
33
34
# File 'lib/hash_serializer/serializer.rb', line 31

def self.load(hash)
  hash = JSON.parse(hash) if hash.is_a?(String)
  (hash || {}).with_indifferent_access
end