Class: DataMapper::Property::PgJSON

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-postgres-types/property/pg_json.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options = {}) ⇒ PgJSON

Returns a new instance of PgJSON.



11
12
13
14
# File 'lib/dm-postgres-types/property/pg_json.rb', line 11

def initialize(model, name, options = {})
  super
  @load_raw_value = @options[:load_raw_value] || false
end

Instance Attribute Details

#load_raw_valueObject (readonly)

Returns the value of attribute load_raw_value.



10
11
12
# File 'lib/dm-postgres-types/property/pg_json.rb', line 10

def load_raw_value
  @load_raw_value
end

Instance Method Details

#dump(value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/dm-postgres-types/property/pg_json.rb', line 16

def dump(value)
  case value
  when ::NilClass, ::String
    value
  when ::Hash, ::Array
    Oj.dump(value, mode: :compat)
  else
    '{}'
  end
end

#load(value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/dm-postgres-types/property/pg_json.rb', line 27

def load(value)
  case value
  when ::Hash, ::Array
    (load_raw_value) ? Oj.dump(value, mode: :compat) : value
  when ::String
    (load_raw_value) ? value : Oj.load(value)
  else
    (load_raw_value) ? '{}' : {}
  end
end

#primitive?(value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/dm-postgres-types/property/pg_json.rb', line 38

def primitive?(value)
  value.kind_of?(::String)
end