Class: Firebug::Serializer

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

Overview

A class for converting a Ruby object into a PHP serialized string.

Class Method Summary collapse

Class Method Details

.parse(obj) ⇒ String

Convert a ruby object into a PHP serialized string.

Parameters:

  • obj (Object)

Returns:

  • (String)

Raises:

  • (ArgumentError)

    for unsupported types



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/firebug/serializer.rb', line 11

def self.parse(obj) # rubocop:disable CyclomaticComplexity
  case obj
  when NilClass
    'N;'
  when TrueClass
    'b:1;'
  when FalseClass
    'b:0;'
  when Integer
    "i:#{obj};"
  when Float
    "d:#{obj};"
  when String, Symbol
    "s:#{obj.to_s.bytesize}:\"#{obj}\";"
  when Array
    "a:#{obj.length}:{#{obj.map.with_index { |e, i| "#{parse(i)}#{parse(e)}" }.join}}"
  when Hash
    "a:#{obj.length}:{#{obj.map { |k, v| "#{parse(k)}#{parse(v)}" }.join}}"
  else
    raise ArgumentError, "unsupported type #{obj.class.name}"
  end
end