Class: Surus::Array::IntegerSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/surus/array/integer_serializer.rb

Constant Summary collapse

ARRAY_REGEX =
%r{
  (?<=[\{,])                 (?# All elements are prefixed with either the opening brace or a comma)
  \-?\d+
  |
  NULL
}x

Instance Method Summary collapse

Instance Method Details

#dump(array) ⇒ Object



18
19
20
21
# File 'lib/surus/array/integer_serializer.rb', line 18

def dump(array)
  return unless array
  '{' + array.map { |s| format(s) }.join(",") + '}'
end

#format(value) ⇒ Object



23
24
25
# File 'lib/surus/array/integer_serializer.rb', line 23

def format(value)
  value == nil ? "NULL" : value
end

#load(string) ⇒ Object



11
12
13
14
15
16
# File 'lib/surus/array/integer_serializer.rb', line 11

def load(string)
  return unless string
  string.scan(ARRAY_REGEX).map do |match|
    match == "NULL" ? nil : Integer(match)
  end
end