Class: OrientdbBinary::Serialization::Serialize

Inherits:
Object
  • Object
show all
Defined in:
lib/orientdb_binary/serialization/serialize.rb

Instance Method Summary collapse

Constructor Details

#initializeSerialize

Returns a new instance of Serialize.



4
5
# File 'lib/orientdb_binary/serialization/serialize.rb', line 4

def initialize()
end

Instance Method Details

#serialize_document(document, is_map) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/orientdb_binary/serialization/serialize.rb', line 61

def serialize_document(document, is_map)
  klass = "@"
  result = []
  document.each do |key, value|
    unless [:version, :rid, :type].include? key
      if key == :class
        klass = value + "@"
      else
        field_wrap = is_map ? "\"" : ""
      end
      result << "#{field_wrap}#{key.to_s}#{field_wrap}:#{serialize_field_value(value)}"
    end
  end

  return klass + "@" + result.join(',')
end

#serialize_field_value(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/orientdb_binary/serialization/serialize.rb', line 7

def serialize_field_value(value)
  result = ""
  if value.is_a? String
    return value if /^#\-{0,1}[\d]+:\-{0,1}[\d]+$/.match(value)
    value = value.sub(/\\/, "\\\\")
    value = value.gsub(/"/, "\\\"")
    return "\"#{value}\""
  end

  if value.is_a? Integer
    return value.to_s
  end

  if value.is_a? Float
    return value.to_s + "f"
  end

  if value.is_a? Bignum
    return value.to_s + "l"
  end

  if value.is_a? BigDecimal
    return value.to_s + "d"
  end

  if value.is_a? Array or value.is_a? Set
    type = value.class.to_s.downcase == "set" ? "<$>" : "[$]"
    result = []

    value.each do |el|
      result << serialize_field_value(el)
    end
    return type.gsub("$", result.join(','))
  end

  if value.is_a?(TrueClass) or value.is_a?(FalseClass)
    return value.to_s
  end

  if value.is_a? Hash
    type = value[:type] == "d" ? "($)" : "{$}"
    return type.gsub("$", serialize_document(value, value[:type] != "d"))
  end

  if value.is_a? Date
  end

  if value.is_a? DateTime
  end

  if value.is_a? Time
  end
end