Class: DataMapper::Types::Json

Inherits:
DataMapper::Type show all
Defined in:
lib/gems/dm-types-0.9.7/lib/dm-types/json.rb

Constant Summary

Constants inherited from DataMapper::Type

DataMapper::Type::PROPERTY_OPTIONS, DataMapper::Type::PROPERTY_OPTION_ALIASES

Class Method Summary collapse

Methods inherited from DataMapper::Type

bind, configure, inherited, options, primitive

Class Method Details

.dump(value, property) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/gems/dm-types-0.9.7/lib/dm-types/json.rb', line 20

def self.dump(value, property)
  if value.nil?
    nil
  elsif value.is_a?(String)
    value
  else
    ::JSON.dump(value)
  end
end

.load(value, property) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/gems/dm-types-0.9.7/lib/dm-types/json.rb', line 10

def self.load(value, property)
  if value.nil?
    nil
  elsif value.is_a?(String)
    ::JSON.load(value)
  else
    raise ArgumentError.new("+value+ must be nil or a String")
  end
end

.typecast(value, property) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/gems/dm-types-0.9.7/lib/dm-types/json.rb', line 30

def self.typecast(value, property)
  # Arrays and hashes are left alone, while strings are parsed as JSON.
  if value.kind_of?(Array) || value.kind_of?(Hash)
    value
  else
    ::JSON.load(value.to_s)
  end
end