Class: RubySerial::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-serial/serializer.rb,
lib/ruby-serial/versions/1/serializer.rb

Defined Under Namespace

Modules: Versions

Constant Summary collapse

DEFAULT_VERSION =

Default encoding version (should be the last one)

'1'

Instance Method Summary collapse

Constructor Details

#initialize(obj, options = {}) ⇒ Serializer

Constructor

Parameters
  • obj (Object): Ruby object to serialize

  • options (map<Symbol,Object>): Options [default = {}]

    • :version (String): The version to be used to encode



14
15
16
17
18
19
20
21
22
# File 'lib/ruby-serial/serializer.rb', line 14

def initialize(obj, options = {})
  @version = (options[:version] || DEFAULT_VERSION)
  @obj = obj
  # Map of objects ID, with their corresponding number of shared objects among their descendants (those having 0 share nothing)
  # map< ObjectID, NbrSharedObjects >
  @objs = {}
  # Map of shared objects' associated to a boolean indicating whether they have already been serialized or not
  @shared_objs = {}
end

Instance Method Details

#dumpObject

Serialize the object

Result
  • String: The object serialized



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-serial/serializer.rb', line 28

def dump
  serializer = nil
  begin
    require "ruby-serial/versions/#{@version}/serializer"
    serializer = eval("RubySerial::Serializer::Versions::Version_#{@version}")::Serializer.new
  rescue
    raise "Unknown serializer version #{@version}: #{$!}"
  end

  return "#{@version}\x00#{serializer.pack_data(@obj)}".force_encoding(Encoding::BINARY)
end