Class: ActiveRecord::Coders::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-postgres-json/coders.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default = Hashie::Mash.new) ⇒ JSON

Returns a new instance of JSON.



15
16
17
# File 'lib/activerecord-postgres-json/coders.rb', line 15

def initialize(default = Hashie::Mash.new)
  @default = default
end

Class Method Details

.dump(json) ⇒ Object



11
12
13
# File 'lib/activerecord-postgres-json/coders.rb', line 11

def self.dump(json)
  new.dump(json)
end

.load(json) ⇒ Object



7
8
9
# File 'lib/activerecord-postgres-json/coders.rb', line 7

def self.load(json)
  new.load(json)
end

Instance Method Details

#dump(obj) ⇒ Object



19
20
21
# File 'lib/activerecord-postgres-json/coders.rb', line 19

def dump(obj)
  obj.nil? ? (@default.nil? ? nil : to_json(@default)) : to_json(obj)
end

#load(json) ⇒ Object



23
24
25
# File 'lib/activerecord-postgres-json/coders.rb', line 23

def load(json)
  json.nil? ? @default : from_json(json)
end