Module: Serializable

Included in:
Friends::Event, Friends::Friend, Friends::Location
Defined in:
lib/friends/serializable.rb

Overview

Serializable provides functionality around serialization to the classes that extend it. This includes a class method to deserialize a string and create an instance of the class.

Defined Under Namespace

Classes: SerializationError

Instance Method Summary collapse

Instance Method Details

#deserialize(str) ⇒ Object

Note: this method assumes the calling class provides the following methods:

- deserialization_regex
    a regex for the string which includes named parameters for the
    different initializer arguments
- deserialization_expectation
    a string for what was expected, if the regex does not match

Parameters:

  • str (String)

    the serialized object string

Returns:

  • (Object)

    the object represented by the serialized string


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/friends/serializable.rb', line 16

def deserialize(str)
  match = str.to_s.match(deserialization_regex)

  unless match
    raise SerializationError,
          "Expected \"#{deserialization_expectation}\""
  end

  args = match.names.
         map { |name| { name.to_sym => match[name.to_sym] } }.
         reduce(:merge).
         reject { |_, v| v.nil? }

  new(args)
end