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.



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

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



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

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

.load(json) ⇒ Object



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

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

Instance Method Details

#dump(obj) ⇒ Object



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

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

#load(json) ⇒ Object



29
30
31
# File 'lib/activerecord-postgres-json/coders.rb', line 29

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