Class: Lich::Common::DatabaseAdapter
- Inherits:
-
Object
- Object
- Lich::Common::DatabaseAdapter
- Defined in:
- documented/common/settings/database_adapter.rb
Overview
Database adapter to separate database concerns Database adapter to separate database concerns
This class handles the interaction with the database, including setting up the database table and saving/loading settings.
Instance Method Summary collapse
-
#get_settings(script_name, scope = ":") ⇒ Hash
Retrieves settings for a given script and scope.
-
#initialize(data_dir, table_name) ⇒ DatabaseAdapter
constructor
Initializes a new DatabaseAdapter instance.
-
#save_settings(script_name, settings, scope = ":") ⇒ Boolean
Saves settings for a given script and scope.
-
#setup! ⇒ void
Sets up the database table if it does not exist.
-
#table ⇒ Sequel::Dataset
Returns the database table object.
Constructor Details
#initialize(data_dir, table_name) ⇒ DatabaseAdapter
Initializes a new DatabaseAdapter instance.
16 17 18 19 20 21 |
# File 'documented/common/settings/database_adapter.rb', line 16 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.
47 48 49 50 |
# File 'documented/common/settings/database_adapter.rb', line 47 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 = ":") ⇒ Boolean
Saves settings for a given script and scope.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'documented/common/settings/database_adapter.rb', line 62 def save_settings(script_name, settings, scope = ":") unless settings.is_a?(Hash) Lich::Messaging.msg("error", "--- Error: Report this - settings must be a Hash, got #{settings.class} ---") Lich.log("--- Error: settings must be a Hash, got #{settings.class} from call initiated by #{script_name} ---") Lich.log(settings.inspect) return false end begin blob = Sequel::SQL::Blob.new(Marshal.dump(settings)) rescue => e Lich::Messaging.msg("error", "--- Error: failed to serialize settings ---") Lich.log("--- Error: failed to serialize settings ---") Lich.log("#{e.}\n#{e.backtrace.join("\n")}") return false end begin @table .insert_conflict(target: [:script, :scope], update: { hash: blob }) .insert(script: script_name, scope: scope, hash: blob) return true rescue Sequel::DatabaseError => db_err Lich::Messaging.msg("error", "--- Database error while saving settings ---") Lich.log("--- Database error while saving settings ---") Lich.log("#{db_err.}\n#{db_err.backtrace.join("\n")}") rescue => e Lich::Messaging.msg("error", "--- Unexpected error while saving settings ---") Lich.log("--- Unexpected error while saving settings ---") Lich.log("#{e.}\n#{e.backtrace.join("\n")}") end false end |
#setup! ⇒ void
This method returns an undefined value.
Sets up the database table if it does not exist.
26 27 28 29 30 31 32 33 |
# File 'documented/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 |
#table ⇒ Sequel::Dataset
Returns the database table object.
38 39 40 |
# File 'documented/common/settings/database_adapter.rb', line 38 def table @table end |