Class: Surus::Hstore::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/surus/hstore/serializer.rb

Constant Summary collapse

KEY_VALUE_REGEX =
%r{
  "
  ((?:[^"\\]|\\.)*)
  "
  =>
  (
    "
    (?:[^"\\]|\\.)*
    "
    |(NULL)
  )
}x

Instance Method Summary collapse

Instance Method Details

#dump(hash) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/surus/hstore/serializer.rb', line 57

def dump(hash)
  return unless hash
  
  key_types = {}
  value_types = {}
  
  stringified_hash = hash.each_with_object({}) do |key_value, stringified_hash|
    key_string, key_type = stringify(key_value[0])
    value_string, value_type = stringify(key_value[1])
    
    stringified_hash[key_string] = value_string
    
    key_types[key_string] = key_type unless key_type == "String"
    value_types[key_string] = value_type unless value_type == "String"
  end

  # Use YAML for recording types as it is much simpler than trying to
  # handle all the special characters that could be in a key or value
  # and encoding them again to fit into one string. Let YAML handle all
  # the mess for us.
  stringified_hash["__key_types"] = YAML.dump(key_types) if key_types.present?
  stringified_hash["__value_types"] = YAML.dump(value_types) if value_types.present?
  
  stringified_hash.map do |key, value|
    "#{format_key(key)}=>#{format_value(value)}"
  end.join(", ")
end

#escape(value) ⇒ Object

Escape a value for use as a key or value in an hstore



94
95
96
97
98
# File 'lib/surus/hstore/serializer.rb', line 94

def escape(value)
  value
    .gsub('\\', '\\\\\\')
    .gsub('"', '\\"')
end

#format_key(key) ⇒ Object



85
86
87
# File 'lib/surus/hstore/serializer.rb', line 85

def format_key(key)
  %Q("#{escape(key)}")
end

#format_value(value) ⇒ Object



89
90
91
# File 'lib/surus/hstore/serializer.rb', line 89

def format_value(value)
  value ? %Q("#{escape(value)}") : "NULL"
end

#load(string) ⇒ Object



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
# File 'lib/surus/hstore/serializer.rb', line 17

def load(string)
  return unless string
  stringified_hash = string.scan(KEY_VALUE_REGEX).each_with_object({}) do |key_value, hash|
    key, value = key_value
    key = unescape(key)
    value = if value == "NULL"
      nil
    else
      unescape(value[1..-2])
    end
    
    hash[key] = value
  end

  key_types = stringified_hash.delete "__key_types"
  key_types = YAML.load key_types if key_types
  value_types = stringified_hash.delete "__value_types"
  value_types = YAML.load value_types if value_types
  
  return stringified_hash unless key_types || value_types
  
  stringified_hash.each_with_object({}) do |key_value, hash|
    string_key, string_value = key_value
    
    key = if key_types && key_types.key?(string_key) 
      typecast(string_key, key_types[string_key])
    else
      string_key
    end
    
    value = if value_types && value_types.key?(string_key)
      typecast(string_value, value_types[string_key])
    else
      string_value
    end
    
    hash[key] = value
  end    
end

#stringify(value) ⇒ Object

Returns an array of value as a string and value type



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/surus/hstore/serializer.rb', line 108

def stringify(value)
  if value.kind_of?(String)
    [value, "String"]
  elsif value.kind_of?(Symbol)
    [value.to_s, "Symbol"]
  elsif value.kind_of?(Integer)
    [value.to_s, "Integer"]
  elsif value.kind_of?(Float)
    [value.to_s, "Float"]
  elsif value.kind_of?(BigDecimal)
    [value.to_s, "BigDecimal"]
  elsif value.kind_of?(Date)
    [value.to_s(:db), "Date"]
  elsif value.kind_of?(TrueClass)
    [value.to_s, "TrueClass"]
  elsif value.kind_of?(FalseClass)
    [value.to_s, "FalseClass"]
  elsif value == nil
    [nil, "String"] # we don't actually stringify nil because format_value special cases nil
  else
    [YAML.dump(value), "YAML"]
  end  
end

#typecast(value, type) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/surus/hstore/serializer.rb', line 132

def typecast(value, type)
  case type
  when "Symbol"
    value.to_sym
  when "Integer"
    Integer(value)
  when "Float"
    Float(value)
  when "BigDecimal"
    BigDecimal(value)
  when "Date"
    Date.parse(value)
  when "TrueClass"
    true
  when "FalseClass"
    false
  when "YAML"
    YAML.load(value)
  else
    raise ArgumentError, "Can't typecast: #{type}"
  end
end

#unescape(value) ⇒ Object

Unescape a value from a key or value in an hstore



101
102
103
104
105
# File 'lib/surus/hstore/serializer.rb', line 101

def unescape(value)
  value
    .gsub('\\\\', '\\')
    .gsub('\\"', '"')
end