Class: Whoosh::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/database.rb

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/whoosh/database.rb', line 7

def self.available?
  if @sequel_available.nil?
    @sequel_available = begin
      require "sequel"
      true
    rescue LoadError
      false
    end
  end
  @sequel_available
end

.config_from(config_data) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/whoosh/database.rb', line 33

def self.config_from(config_data)
  db_config = config_data["database"]
  return nil unless db_config && db_config["url"]
  {
    url: db_config["url"],
    max_connections: db_config["max_connections"] || 10,
    log_level: db_config["log_level"]
  }
end

.connect(url, max_connections: 10, log_level: nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/whoosh/database.rb', line 19

def self.connect(url, max_connections: 10, log_level: nil)
  ensure_available!
  db = Sequel.connect(url, max_connections: max_connections)
  db.loggers << ::Logger.new($stdout) if log_level == "debug"
  db
end

.connect_from_config(config_data, logger: nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/whoosh/database.rb', line 26

def self.connect_from_config(config_data, logger: nil)
  db_config = config_from(config_data)
  return nil unless db_config
  ensure_available!
  connect(db_config[:url], max_connections: db_config[:max_connections], log_level: db_config[:log_level])
end

.ensure_available!Object



43
44
45
# File 'lib/whoosh/database.rb', line 43

def self.ensure_available!
  raise Errors::DependencyError, "Database requires the 'sequel' gem" unless available?
end