Module: Spark::Serializer

Defined in:
lib/spark/serializer.rb,
lib/spark/serializer/oj.rb,
lib/spark/serializer/base.rb,
lib/spark/serializer/pair.rb,
lib/spark/serializer/text.rb,
lib/spark/serializer/batched.rb,
lib/spark/serializer/marshal.rb,
lib/spark/serializer/cartesian.rb,
lib/spark/serializer/compressed.rb,
lib/spark/serializer/auto_batched.rb,
lib/spark/serializer/message_pack.rb

Overview

Serializer

Defined Under Namespace

Classes: AutoBatched, Base, Batched, Cartesian, Compressed, Marshal, MessagePack, Oj, Pair, Text

Constant Summary collapse

DEFAULT_COMPRESS =
false
DEFAULT_BATCH_SIZE =
1024
DEFAULT_SERIALIZER_NAME =
'marshal'
@@registered =
{}

Class Method Summary collapse

Class Method Details

.build(text = nil, &block) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/spark/serializer.rb', line 54

def self.build(text=nil, &block)
  if block_given?
    class_eval(&block)
  else
    class_eval(text.to_s.downcase)
  end
end

.find(name) ⇒ Object



40
41
42
# File 'lib/spark/serializer.rb', line 40

def self.find(name)
  @@registered[name.to_s.downcase]
end

.find!(name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/spark/serializer.rb', line 44

def self.find!(name)
  klass = find(name)

  if klass.nil?
    raise Spark::SerializeError, "Unknow serializer #{name}."
  end

  klass
end

.register(*args) ⇒ Object

Register class and create method for quick access. Class will be available also as __name__ for using in build method (Proc binding problem).

Examples:

register('test1', 'test2', Class)

Spark::Serializer.test1
Spark::Serializer.test2

# Proc binding problem
build { marshal } # => Spark::Serializer::Marshal

marshal = 1
build { marshal } # => 1

build { __marshal__ } # => Spark::Serializer::Marshal


31
32
33
34
35
36
37
38
# File 'lib/spark/serializer.rb', line 31

def self.register(*args)
  klass = args.pop
  args.each do |arg|
    @@registered[arg] = klass
    define_singleton_method(arg.to_sym){|*args| klass.new(*args) }
    define_singleton_method("__#{arg}__".to_sym){|*args| klass.new(*args) }
  end
end