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.



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.



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.



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