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)


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

def custom?
  true
end

#dump(value) ⇒ Object



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

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

#load(value) ⇒ Object



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

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)


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

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

#typecast_to_primitive(value) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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