Module: Gdbmish::Dump

Defined in:
lib/gdbmish/dump.rb

Overview

Wrapper for different dump formats, providing various shortcut methods. Currently, there is only Ascii mode.

Ascii mode optionally dumps file information such as filename, owner, mode. See Ascii#new on how they are used.

Defined Under Namespace

Classes: Ascii

Class Method Summary collapse

Class Method Details

.ascii(data = nil, io = nil, **fileoptions) {|appender| ... } ⇒ String, IO

Dump data as standard ASCII format. When an io is given, the dump will be written to it. Otherwise, a new String will be returned. See Gdbmish::Dump::Ascii#initialize for fileoptions. See Gdbmish::Dump::Ascii#dump for data and block behaviour.

Examples:

Dumping simple Hash into an IO

File.open("test.dump", "w") do |file|
  Gdbmish::Dump.ascii({some: "data"}, file)
end

Dumping into a String

string_dump = Gdbmish::Dump.ascii({some: "data"})

Dumping into IO with file information and block

Gdbmish::Dump.ascii(io, file: "test.db", uid: "1000", user: "ziggy", gid: "1000", group: "staff", mode: 0o600) do |appender|
  MyData.each do |object|
    appender.push(object.id, object.dump)
  end
end

Parameters:

  • data (#each_pair, nil) (defaults to: nil)

    The data to dump

  • io (IO, nil) (defaults to: nil)

    The IO to dump to

  • fileoptions (Hash)

    Options for the dump file

Yields:

  • (appender)

    The appender to push data onto the dump

Returns:

  • (String)

    The dump as a string when no io is given

  • (IO)

    The IO written to when io is given



187
188
189
190
191
192
193
194
195
# File 'lib/gdbmish/dump.rb', line 187

def self.ascii(data = nil, io = nil, **fileoptions, &block)
  if io.nil?
    io = StringIO.new
    to_string = true
  end

  io = Ascii.new(**fileoptions).dump(io, data, &block)
  to_string ? io.string : io
end