Class: GoldMine::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/gold_mine/db.rb

Overview

Database of fortunes.

Example:

db = DB.new(path: "/home/user/fortunes")
db.random # => random fortune

Options:

:path

The path of database.

:comments

Pass true if allow comments is needed.

:delim

The character which is used as delimiter in a database.

Direct Known Subclasses

IDB

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DB

Returns a new instance of DB.



28
29
30
31
# File 'lib/gold_mine/db.rb', line 28

def initialize(options = {})
  @path = options.fetch(:path, GoldMine.default_db_path)
  @options = self.class.default_options.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/gold_mine/db.rb', line 26

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/gold_mine/db.rb', line 26

def path
  @path
end

Class Method Details

.default_optionsObject



20
21
22
23
24
# File 'lib/gold_mine/db.rb', line 20

def self.default_options
  @default_options ||= {
    delim: "%"
  }
end

Instance Method Details

#fortunesObject



37
38
39
# File 'lib/gold_mine/db.rb', line 37

def fortunes
  @fortunes ||= read_fortunes
end

#randomObject



33
34
35
# File 'lib/gold_mine/db.rb', line 33

def random
  fortunes.sample
end