Class: Lobster::Serialization::UserStatusSerializer

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

Overview

Transforms a user status 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/user_status_serializer.rb', line 15

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

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

Creates a user status from its marshaled format.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lobster/serialization/user_status_serializer.rb', line 41

def deserialize(data, type, engine, *args)
  validate_structure(data, {
                         :id      => Lobster::Uuid,
                         :active  => Time,
                         :state   => Symbol,
                         :idle    => :Boolean,
                         :message => String
                         })
  id     = engine.deserialize_inner(data[:id], Lobster::Uuid)
  status = Lobster::UserStatus.new(id)

  status.active  = engine.deserialize_inner(data[:active], Time)
  status.state   = engine.deserialize_inner(data[:state], Symbol)
  status.idle    = data[:idle]
  status.message = data[:message]

  status
end

#serialize(obj, engine) ⇒ Object

Converts a user status to its marshaled format.


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

def serialize(obj, engine)
  {
      :id      => engine.serialize_inner(obj.id),
      :active  => engine.serialize_inner(obj.active),
      :state   => engine.serialize_inner(obj.state),
      :idle    => obj.idle?,
      :message => obj.message
  }
end