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(params = {}) ⇒ JSON

Returns a new instance of JSON.



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

def initialize(params = {})
  @default = {}
  return unless params.class.name == 'Hash'

  @default = params[:default] if params[:default]
  @symbolize_keys = params[:symbolize_keys] if params[:symbolize_keys]
end

Class Method Details

.dump(json) ⇒ Object



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

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

.load(json) ⇒ Object



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

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

Instance Method Details

#dump(obj) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/activerecord-postgres-json/coders.rb', line 24

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

#load(json) ⇒ Object



32
33
34
# File 'lib/activerecord-postgres-json/coders.rb', line 32

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