class Stargate::Marshal::Unmarshaller

Internal: On the client side there's unmarshalling needed then. Use this unmarshaller to unpack data. Unmarshaller is assigned to a module (namespace). It shall look up within that module in case of unpacking complex objects of specific type.

Public Instance Methods

unmarshal(data) click to toggle source

Public: Unpacks given object from payload wrap.

There are two scenarios for unpacking:

  • Basic value - Just return value or adapt contents (like symbol, arrays or hashes).

  • An object of specific type - Check if our namespace defines such class. No? Erorr. Yes. Unmarshal into.

Returns object unmarshalled.

# File lib/stargate/marshal/unmarshaller.rb, line 15
def unmarshal(data)
  payload = Payload.new(*data)

  case payload.type.to_sym
  when Payload::SIMPLE
    payload.data
  when Payload::SYMBOL
    payload.data.to_sym
  when Payload::LIST
    unmarshal_list(payload.data)
  when Payload::HASH
    unmarshal_hash(payload.data)
  else
    klass = payload.type.constantize
    klass.new(unmarshal_hash(payload.data))
  end
end

Private Instance Methods

unmarshal_hash(hash) click to toggle source

Internal: Unmarshals standalone hash.

# File lib/stargate/marshal/unmarshaller.rb, line 36
def unmarshal_hash(hash)
  hash.inject({}) { |result,(k,v)| result[k] = unmarshal(v); result }
end
unmarshal_list(list) click to toggle source

Internal: Unmarshals list.

# File lib/stargate/marshal/unmarshaller.rb, line 41
def unmarshal_list(list)
  list.map { |obj| unmarshal(obj) }
end