Class: Lobster::Serialization::ProfileSerializer

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

Overview

Transforms a profile 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/profile_serializer.rb', line 15

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

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

Creates a profile 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
# File 'lib/lobster/serialization/profile_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,
                         :alias        => String,
                         :color        => Fixnum,
                         :registration => Time,
                         :login        => Time,
                         :quote        => String,
                         :avatar       => Lobster::Uuid,
                         :coins        => Fixnum
                         })

  id      = engine.deserialize_inner(data[:id], Lobster::Uuid)
  profile = Lobster::Profile.new(id)

  profile.alias        = data[:alias]
  profile.color        = data[:color]
  profile.registration = engine.deserialize_inner(data[:registration], Time)
  profile.        = engine.deserialize_inner(data[:login], Time)
  profile.quote        = data[:quote]
  profile.avatar_id    = engine.deserialize_inner(data[:avatar], Lobster::Uuid)
  profile.coins        = data[:coins]

  profile
end

#serialize(obj, engine) ⇒ Object

Converts a profile to its marshaled format.


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

def serialize(obj, engine)
  {
      :id           => engine.serialize_inner(obj.id),
      :alias        => obj.alias,
      :color        => obj.color,
      :registration => engine.serialize_inner(obj.registration),
      :login        => engine.serialize_inner(obj.),
      :quote        => obj.quote,
      :avatar       => engine.serialize_inner(obj.avatar_id),
      :coins        => obj.coins
  }
end