Class: Lich::Common::DatabaseAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/common/settings/database_adapter.rb

Overview

Database adapter to separate database concerns

Instance Method Summary collapse

Constructor Details

#initialize(data_dir, table_name) ⇒ DatabaseAdapter

Initializes a new DatabaseAdapter instance.

Examples:

adapter = Lich::Common::DatabaseAdapter.new('/path/to/data', 'settings_table')

Parameters:

  • data_dir (String)

    the directory where the database file is located.

  • table_name (String)

    the name of the table to be used in the database.

Raises:

  • (Sequel::DatabaseError)

    if there is an error connecting to the database.



13
14
15
16
17
18
# File 'lib/common/settings/database_adapter.rb', line 13

def initialize(data_dir, table_name)
  @file = File.join(data_dir, "lich.db3")
  @db = Sequel.sqlite(@file)
  @table_name = table_name
  setup!
end

Instance Method Details

#get_settings(script_name, scope = ":") ⇒ Hash

Retrieves settings for a given script and scope.

Examples:

settings = adapter.get_settings('my_script', 'my_scope')

Parameters:

  • script_name (String)

    the name of the script for which settings are retrieved.

  • scope (String) (defaults to: ":")

    the scope of the settings (default is “:”).

Returns:

  • (Hash)

    the settings associated with the script and scope, or an empty hash if none found.

Raises:

  • (Sequel::DatabaseError)

    if there is an error querying the database.



52
53
54
55
# File 'lib/common/settings/database_adapter.rb', line 52

def get_settings(script_name, scope = ":")
  entry = @table.first(script: script_name, scope: scope)
  entry.nil? ? {} : Marshal.load(entry[:hash])
end

#save_settings(script_name, settings, scope = ":") ⇒ void

This method returns an undefined value.

Saves settings for a given script and scope.

Examples:

adapter.save_settings('my_script', { key: 'value' }, 'my_scope')

Parameters:

  • script_name (String)

    the name of the script for which settings are saved.

  • settings (Hash)

    the settings to be saved.

  • scope (String) (defaults to: ":")

    the scope of the settings (default is “:”).

Raises:

  • (Sequel::DatabaseError)

    if there is an error saving to the database.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/common/settings/database_adapter.rb', line 66

def save_settings(script_name, settings, scope = ":")
  blob = Sequel::SQL::Blob.new(Marshal.dump(settings))

  if @table.where(script: script_name, scope: scope).count > 0
    @table
      .where(script: script_name, scope: scope)
      .insert_conflict(:replace)
      .update(hash: blob)
  else
    @table.insert(
      script: script_name,
      scope: scope,
      hash: blob
    )
  end
end

#setup!void

Note:

This method is called during initialization.

This method returns an undefined value.

Sets up the database table if it does not already exist.

Examples:

adapter.setup!


26
27
28
29
30
31
32
33
# File 'lib/common/settings/database_adapter.rb', line 26

def setup!
  @db.create_table?(@table_name) do
    text :script
    text :scope
    blob :hash
  end
  @table = @db[@table_name]
end

#tableSequel::Dataset

Returns the database table object.

Examples:

table = adapter.table

Returns:

  • (Sequel::Dataset)

    the dataset representing the table.



40
41
42
# File 'lib/common/settings/database_adapter.rb', line 40

def table
  @table
end