Class: Gdbmish::Dump::Ascii

Inherits:
Object
  • Object
show all
Defined in:
lib/gdbmish/dump.rb

Overview

Dumping GDBM data as ASCII (aka default) format.

Defined Under Namespace

Classes: Appender

Constant Summary collapse

GDBM_MAX_DUMP_LINE_LEN =

GDBM does not split base64 strings at 60 encoded characters (as defined by RFC 2045).

See _GDBM_MAX_DUMP_LINE_LEN in gdbmdefs.h

76

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, uid: nil, user: nil, gid: nil, group: nil, mode: nil) ⇒ Ascii

Builds a new Ascii format dumper

Dumping file information is optional.

  • uid, user, gid, group and mode will only be used when file is given
  • user will only be used when uid is given
  • group will only be used when gid is given

Examples:

fileoptions = {file: "test.db", uid: "1000", user: "ziggy", gid: "1000", group: "staff", mode: 0o600}
File.open("test.dump", "w") do |file|
  Gdbmish::Dump::Ascii.new(**fileoptions).dump(file) do |appender|
    MyDataSource.each do |key, value|
      appender.push(key.to_s, value.to_s)
    end
  end
end


74
75
76
77
78
79
80
81
# File 'lib/gdbmish/dump.rb', line 74

def initialize(file: nil, uid: nil, user: nil, gid: nil, group: nil, mode: nil)
  @file = file
  @uid = uid
  @user = user
  @gid = gid
  @group = group
  @mode = mode
end

Instance Method Details

#dump(io) ⇒ IO #dump(io, data) ⇒ IO #dump(io) {|appender| ... } ⇒ IO #dump(io, data) {|appender| ... } ⇒ IO

Overloads:

  • #dump(io) ⇒ IO

    Dump only the header and footer

    Parameters:

    • io (IO)

      The IO to dump to

    Returns:

    • (IO)

      The IO written to

  • #dump(io, data) ⇒ IO

    Dump data to io

    Parameters:

    • io (IO)

      The IO to dump to

    • data (#each_pair)

      The data to dump

    Returns:

    • (IO)

      The IO written to

  • #dump(io) {|appender| ... } ⇒ IO

    Yield an Appender to call push on.

    Parameters:

    • io (IO)

      The IO to dump to

    Yield Parameters:

    • appender (Appender)

      The appender to push data onto the dump

    Returns:

    • (IO)

      The IO written to

  • #dump(io, data) {|appender| ... } ⇒ IO

    Dump data to io and then yield an Appender to call push on.

    Parameters:

    • io (IO)

      The IO to dump to

    • data (#each_pair)

      The data to dump

    Yield Parameters:

    • appender (Appender)

      The appender to push data onto the dump

    Returns:

    • (IO)

      The IO written to

Yields:

  • (appender)


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gdbmish/dump.rb', line 103

def dump(io, data = nil, &block)
  appender = Appender.new(io)

  dump_header!(io)
  data&.each_pair do |k, v|
    appender.push(k.to_s, v.to_s)
  end
  yield appender if block
  dump_footer!(io, appender.count)

  io
end