class GoldMine::IndexReader

Reads an index for fortunes database. The index is a binary file which contains a header and pointers.

The header stores statistical information and instruction specifying how to read the file. Pointer indicates an initial position for the related fortune.

Constants

HEADER_SIZE
POINTER_SIZE

Attributes

delim[R]
flags[R]
longlen[R]
numstr[R]
path[R]
shortlen[R]
version[R]

Public Class Methods

new(path) click to toggle source
# File lib/gold_mine/index_reader.rb, line 14
def initialize(path)
  @path = path

  header = header_fields
  @version = header[0]
  @numstr = header[1]
  @longlen = header[2]
  @shortlen = header[3]
  @flags = header[4].to_switch
  @delim = header[5].chr
end

Public Instance Methods

get_pointer_at(index) click to toggle source

Returns a pointer from a certain position.

# File lib/gold_mine/index_reader.rb, line 72
def get_pointer_at(index)
  IO.binread(@path, POINTER_SIZE, HEADER_SIZE + POINTER_SIZE * index).unpack("N").first
end
get_pointers() click to toggle source

Returns all pointers.

# File lib/gold_mine/index_reader.rb, line 66
def get_pointers
  IO.binread(@path, @numstr * POINTER_SIZE, HEADER_SIZE).unpack("N*")
end
header_fields() click to toggle source

Returns a header.

The header consists of six 32-bit unsigned integers. Integers are stored in big-endian byte order.

The order and meaning of the fields are as follows:

version

version number

numstr

number of pointers

longlen

size of longest fortune

shortlen

size of shortest fortune

flags

stores multiple booleans (bit-field)

1

randomize order

2

sorting in alphabetical order

4

Caesar encryption

8

allow comments

delim

8-bit unsigned integer packed to 32-bit

which represents a delimeter character
# File lib/gold_mine/index_reader.rb, line 60
def header_fields
  IO.binread(@path, HEADER_SIZE, 0).unpack("N5C1")
end
options() click to toggle source

Returns a hash with selected header fields.

# File lib/gold_mine/index_reader.rb, line 30
def options
  {
    version: @version,
    delim: @delim,
    randomized: @flags[0],
    ordered: @flags[1],
    rotated: @flags[2],
    comments: @flags[3]
  }
end
random_pointer() click to toggle source
# File lib/gold_mine/index_reader.rb, line 76
def random_pointer
  get_pointer_at rand(@numstr)
end