Class: Ruote::Serializer

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

Overview

To get a YAML serializer :

serializer = Serializer.new(:yaml)

A marshal + base64 one :

serializer = Serializer.new(:marshal64)

A marshal only one :

serializer = Serializer.new(:marshal)

For the rest, it’s the classical :

data = serializer.encode(object)

object = serializer.decode(data)

Constant Summary collapse

MARSHAL =

plain marshal serialization

[
  lambda { },
  lambda { |o| Marshal.dump(o) },
  lambda { |s| Marshal.load(s) }
]
MARSHAL64 =

marshal serialization with base64 encoding

[
  lambda { require 'base64' },
  lambda { |o| Base64.encode64(Marshal.dump(o)) },
  lambda { |s| Marshal.load(Base64.decode64(s)) }
]
YAML =

YAML serialization

[
  lambda { require 'yaml' },
  lambda { |o| ::YAML.dump(o) },
  lambda { |s| ::YAML.load(s) }
]
FLAVOURS =

The flavour map

{
  :marshal => MARSHAL, :marshal64 => MARSHAL64, :yaml => YAML
}

Instance Method Summary collapse

Constructor Details

#initialize(flavour) ⇒ Serializer

Returns a new instance of Serializer.



85
86
87
88
89
90
# File 'lib/ruote/util/serializer.rb', line 85

def initialize(flavour)

  @flavour = FLAVOURS[flavour] || MARSHAL64

  @flavour[0].call # initializes the flavour
end

Instance Method Details

#decode(s) ⇒ Object



97
98
99
100
# File 'lib/ruote/util/serializer.rb', line 97

def decode(s)

  @flavour[2].call(s)
end

#encode(o) ⇒ Object



92
93
94
95
# File 'lib/ruote/util/serializer.rb', line 92

def encode(o)

  @flavour[1].call(o)
end