Module: Linkage::Helpers::Database

Included in:
MatchSets::Database, ResultSets::Database, ScoreSets::Database
Defined in:
lib/linkage/helpers/database.rb

Instance Method Summary collapse

Instance Method Details

#database_connection(connection_options = {}, default_options = {}) ⇒ Object #database_connection(url) ⇒ Object #database_connection(database) ⇒ Object

Returns a Sequel::Database.

Overloads:

  • #database_connection(connection_options = {}, default_options = {}) ⇒ Object

    Parameters:

    • connection_options (Hash) (defaults to: {})

      Options to establish a connection. Any options not explicitly listed below are passed directly to Sequel.connect.

    Options Hash (connection_options):

    • :dir (String)

      Parent directory to use for SQLite database

    • :filename (String)

      SQLite database filename

  • #database_connection(url) ⇒ Object

    Parameters:

    • url (String)

      Sequel-style connection url

  • #database_connection(database) ⇒ Object

    Parameters:

    • database (Sequel::Database)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/linkage/helpers/database.rb', line 15

def database_connection(connection_options = {}, default_options = {})
  sequel_options = nil
  connection_options ||= default_options

  case connection_options
  when Hash
    connection_options = default_options.merge(connection_options)
    sequel_options = connection_options.reject do |key, value|
      key == :dir || key == :filename
    end

    if sequel_options.empty?
      filename = connection_options[:filename] || 'linkage.db'
      if connection_options[:dir]
        dir = File.expand_path(connection_options[:dir])
        FileUtils.mkdir_p(dir)
        filename = File.join(dir, filename)
      end
      sequel_options[:adapter] = :sqlite
      sequel_options[:database] = filename
    end
  when String
    sequel_options = connection_options
  when Sequel::Database
    return connection_options
  else
    raise ArgumentError, "Expected Hash or String, got #{connection_options.class}"
  end
  Sequel.connect(sequel_options)
end