class Stargate::Marshal::Marshaller

Internal: Use it in order to pack data for exchange. Marshaller is created per instance of registry version. It packs basic values and objects of registered classes into Stargate::Marshal::Payload wraps.

Public Class Methods

new(registry_version) click to toggle source

Public: Constructor. Initializes marshaller for given registry version object.

# File lib/stargate/marshal/marshaller.rb, line 7
def initialize(registry_version)
  @registry_version = registry_version
end

Public Instance Methods

marshal(obj) click to toggle source

Public: Returns given object after marshalling.

# File lib/stargate/marshal/marshaller.rb, line 12
def marshal(obj)
  case obj
  when String, Numeric, TrueClass, FalseClass, Float, BigDecimal, NilClass
    Payload.new(Payload::SIMPLE, obj)
  when Symbol
    Payload.new(Payload::SYMBOL, obj.to_s)
  when Hash
    Payload.new(Payload::HASH, marshal_hash(obj))
  when Array
    Payload.new(Payload::LIST, marshal_list(obj))
  else
    Payload.new(*marshal_complex_object(obj))
  end
end

Private Instance Methods

marshal_complex_object(obj) click to toggle source

Internal: Checks if type of given object is registered for exchange. No? Raises an error. Yes? Marshals all public attributes and computed readers.

Returns marshalled object.

# File lib/stargate/marshal/marshaller.rb, line 46
def marshal_complex_object(obj)
  metadata = @registry_version.metadata_for(obj)

  attributes = (metadata.attributes + metadata.readers).inject({}) do |result,key|
    result[key.to_s] = marshal(obj.send(key.to_sym))
    result
  end

  [ metadata.name, attributes ]
end
marshal_hash(obj) click to toggle source

Internal: Returns hash marshalled.

# File lib/stargate/marshal/marshaller.rb, line 35
def marshal_hash(obj)
  obj.inject({}) do |res,(k,v)|
    res[k.to_s] = marshal(v)
    res
  end
end
marshal_list(obj) click to toggle source

Internal: Returns array marshalled.

# File lib/stargate/marshal/marshaller.rb, line 30
def marshal_list(obj)
  obj.map { |x| marshal(x) }
end