Class: Lobster::Serialization::RoomSerializer

Inherits:
Serializer
  • Object
show all
Defined in:
lib/lobster/serialization/room_serializer.rb

Overview

Transforms a room to and from its marshaled format.

Instance Method Summary collapse

Instance Method Details

#add_to(engine) ⇒ nil

This method is abstract.

Adds the serializer to an engine.

Parameters:

Returns:

  • (nil)

15
16
17
# File 'lib/lobster/serialization/room_serializer.rb', line 15

def add_to(engine)
  engine.add_serializer(self, Lobster::Room)
end

#deserialize(data, type, engine, *args) ⇒ Object

Creates a room from its marshaled format.

Parameters:

  • data (Object)

    Room in its marshaled format.

  • type (Class)

    Type of object that should be created.

  • engine (SerializationEngine)

    Engine performing the serialization. Use this to de-serialize other types of objects.

  • args

    Extra information that might be need for de-serializing.

Returns:

  • (Object)

    Newly created instance of the room representative of its marshaled format.


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lobster/serialization/room_serializer.rb', line 44

def deserialize(data, type, engine, *args)
  # Raise a format error if any of the keys are missing.
  validate_structure(data, {
                         :id          => Lobster::Uuid,
                         :name        => String,
                         :description => String,
                         :owner       => Lobster::Uuid,
                         :creation    => Time,
                         :hidden      => :Boolean
                         }, {
                         :topic       => String,
                         :script      => Lobster::Uuid
                         })

  id       = engine.deserialize_inner(data[:id], Lobster::Uuid)
  creation = engine.deserialize_inner(data[:creation], Time)
  room     = Lobster::Room.new(id, creation)

  room.name        = data[:name]
  room.description = data[:description]
  room.topic       = data[:topic]
  room.script_id   = engine.deserialize_inner(data[:script], Lobster::Uuid)
  room.owner_id    = engine.deserialize_inner(data[:owner], Lobster::Uuid)
  room.hidden      = data[:hidden]

  room
end

#serialize(obj, engine) ⇒ Object

Converts a room to its marshaled format.

Parameters:

  • obj (Room)

    Room to serialize.

  • engine (SerializationEngine)

    Engine performing the serialization. Use this to serialize other types of objects.

Returns:

  • (Object)

    Room marshaled to its simplest form.


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lobster/serialization/room_serializer.rb', line 24

def serialize(obj, engine)
  {
      :id          => engine.serialize_inner(obj.id),
      :name        => obj.name,
      :description => obj.description,
      :topic       => obj.topic,
      :script      => engine.serialize_inner(obj.script_id),
      :owner       => engine.serialize_inner(obj.owner_id),
      :creation    => engine.serialize_inner(obj.creation),
      :hidden      => obj.hidden?
  }
end