Class: Gdbmish::Read::Ascii

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

Overview

Main abstraction to read an GDBM Ascii dump file (aka "standard format")

Examples:

File.open("path/to/file.dump") do |io|
  file = Gdbmish::Read::Ascii.new(io)

  file.data do |key, value|
    puts "#{key.inspect} => #{value.inspect}"
  end
  pp file.meta

  # Note: your `io`'s file pointer posistion has changed.
  io.pos # => 317
end

# Produces:
# "some_key" => "Some Value"
# "otherKey" => "Other\nValue"
#<Gdbmish::Read::AsciiMetaData @count=nil, @file="test.db", @gid="1000", @group="staff", @mode=384, @uid="1000", @user="ziggy", @version="1.1">

Instance Method Summary collapse

Constructor Details

#initialize(io, load_meta: true, encoding: Encoding::UTF_8) ⇒ Ascii

Create a new Ascii reader.

load_meta can be:

  • true (default) load meta data, but skip count for preformance reasons.
  • false skip loading meta data.
  • :count load meta data, including count.

Parameters:

  • io (IO)

    The IO to read from. Assumed to point to the beginning of the GDBM dump.

  • load_meta (Boolean, Symbol) (defaults to: true)

    Whether to load meta data.

  • encoding (String, Encoding) (defaults to: Encoding::UTF_8)

    The encoding to use for key/value pairs.



39
40
41
42
43
# File 'lib/gdbmish/read.rb', line 39

def initialize(io, load_meta: true, encoding: Encoding::UTF_8)
  @io = io
  @load_meta = load_meta
  @encoding = encoding
end

Instance Method Details

#data {|key, value| ... } ⇒ AsciiDataIterator

Note:

This will consume the IO, so you can only iterate once. After that, you need to rewind the IO.

Returns an Enumerator over key/value pairs. Given a block, it will yield each key/value pair, which is a shortcut for #data.each.

Depending on the size of the dataset, you might want to read everything into an Array or Hash:

Ascii.new(io1).data.to_a # => [["some_key", "Some Value"], ["otherKey", "Other\nValue"]]
Ascii.new(io2).data.to_h # => {"some_key" => "Some Value", "otherKey" => "Other\nValue"}

Yields:

  • (key, value)

    for each key/value pair

Yield Parameters:

  • key (String)

    the key

  • value (String)

    the value

Returns:



60
61
62
63
64
# File 'lib/gdbmish/read.rb', line 60

def data(&block)
  load_meta!
  @data_iterator ||= AsciiDataIterator.new(@io, encoding: @encoding)
  block ? @data_iterator.each(&block) : @data_iterator
end

#metaAsciiMetaData?

Parses for meta data, depending on the load_meta value in #initialize

Returns:



68
69
70
# File 'lib/gdbmish/read.rb', line 68

def meta
  load_meta!
end