Class: Pacer::TitanEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/pacer-titan/encoder.rb

Overview

This encoder was originally part of pacer-neo4j. It uses native data where Neo4j could and for everything else it uses (slow (but easy)) human-readable YAML encoding. Modified to convert times to UTC when saving and use Rails’ time zone support

Class Method Summary collapse

Class Method Details

.decode_property(value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pacer-titan/encoder.rb', line 63

def self.decode_property(value)
  if value.is_a? String and value[0, 1] == ' '
    if value[1, 4] == 'time'
      # FIXME: we lose the milliseconds here (and time zone if no Rails)...
      if defined? Rails
        Time.zone.parse value[6..-1]
      else
        Time.parse value[6..-1]
      end
    else
      YAML.load(value[1..-1])
    end
  elsif value.is_a? ArrayJavaProxy
    value.to_a
  else
    value
  end
rescue Psych::SyntaxError
  value
end

.encode_property(value) ⇒ Object



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
60
61
# File 'lib/pacer-titan/encoder.rb', line 9

def self.encode_property(value)
  case value
  when nil
    nil
  when String
    value = value.strip
    value = nil if value == ''
    value
  when Symbol
    value.to_s
  when Numeric
    if value.is_a? Bignum
      dump value
    else
      value.to_java
    end
  when true, false
    value.to_java
  when Date
    value.to_time.strftime ' time %Y-%m-%d'
  when DateTime, Time
    value.to_time.utc.strftime ' time %Y-%m-%d %H:%M:%S.%L %z'
  when Array
    if value.length == 0
      value_type = Fixnum
    else
      value_type = value.first.class
      value_type = TrueClass if value_type == FalseClass
      value.each do |v|
        if value_type != v.class or (value == true or value == false and value_type == TrueClass)
          value_type = nil
          break
        end
      end
    end
    case value_type
    when Fixnum
      value.to_java :long
    when Float
      value.to_java :double
    when TrueClass
      value.to_java :boolean
    when String
      value.to_java :string
    when Symbol
      value.to_java :string
    else
      dump value
    end
  else
    dump value
  end
end