Class: Transit::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/transit/writer.rb

Overview

Transit::Writer marshals Ruby objects as transit values to an output stream.

Instance Method Summary collapse

Constructor Details

#initialize(format, io, opts = {}) ⇒ Writer

Creates a new Writer configured to write to io in format (:json, :json_verbose, :msgpack).

Use opts to register custom write handlers, associating each one with its type.

Examples:

json_writer                 = Transit::Writer.new(:json, io)
json_verbose_writer         = Transit::Writer.new(:json_verbose, io)
msgpack_writer              = Transit::Writer.new(:msgpack, io)
writer_with_custom_handlers = Transit::Writer.new(:json, io,
  :handlers => {Point => PointWriteHandler})

Parameters:

  • format (Symbol)

    required :json, :json_verbose, or :msgpack

  • io (IO)

    required

  • opts (Hash) (defaults to: {})

    optional

See Also:


39
40
41
42
43
44
45
46
47
48
49
# File 'lib/transit/writer.rb', line 39

def initialize(format, io, opts={})
  @marshaler = case format
               when :json
                 Marshaler::Json.new(io, {:handlers => {},
                                          :oj_opts => {:indent => -1}}.merge(opts))
               when :json_verbose
                 Marshaler::VerboseJson.new(io, {:handlers => {}}.merge(opts))
               else
                 Marshaler::MessagePack.new(io, {:handlers => {}}.merge(opts))
               end
end

Instance Method Details

#write(obj) ⇒ Object


59
60
61
# File 'lib/transit/writer.rb', line 59

def write(obj)
  @marshaler.write(obj)
end