Class: DataMapper::Property::Json

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

Instance Method Summary collapse

Methods included from DirtyMinder

#set!

Instance Method Details

#custom?Boolean

Returns:

  • (Boolean)


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

def custom?
  true
end

#dump(value) ⇒ Object



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

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

#load(value) ⇒ Object



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

def load(value)
  if value.nil?
    nil
  elsif value.is_a?(::String)
    typecast_to_primitive(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)


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

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

#typecast_to_primitive(value) ⇒ Object



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

def typecast_to_primitive(value)
  MultiJson.decode(value.to_s)
end

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

Returns:

  • (Boolean)


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

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