Class: DataMapper::Property::Json

Inherits:
Text
  • Object
show all
Defined in:
lib/dm-types/json.rb

Instance Method Summary collapse

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/dm-types/json.rb', line 8

def custom?
  true
end

#dump(value) ⇒ Object



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

def dump(value)
  if value.nil? || value.is_a?(::String)
    value
  else
    ::JSON.dump(value)
  end
end

#load(value) ⇒ Object



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

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

#primitive?(value) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/dm-types/json.rb', line 12

def primitive?(value)
  value.kind_of?(::Array) || value.kind_of?(::Hash)
end

#typecast_to_primitive(value) ⇒ Object



38
39
40
# File 'lib/dm-types/json.rb', line 38

def typecast_to_primitive(value)
  ::JSON.load(value.to_s)
end

#valid?(value, negated = false) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/dm-types/json.rb', line 16

def valid?(value, negated = false)
  super || dump(value).kind_of?(::String)
end