Class: Lich::Common::DatabaseAdapter
- Inherits:
-
Object
- Object
- Lich::Common::DatabaseAdapter
- Defined in:
- documented/common/settings/database_adapter.rb
Overview
Handles database interactions for Lich.
This class is responsible for setting up the database and managing settings related to scripts and their scopes.
Instance Method Summary collapse
-
#get_settings(script_name, scope = ":") ⇒ Hash
Retrieves settings for a given script and scope.
-
#initialize(data_dir, table_name) ⇒ void
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) ⇒ void
Initializes a new DatabaseAdapter instance.
14 15 16 17 18 19 |
# File 'documented/common/settings/database_adapter.rb', line 14 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.
42 43 44 45 |
# File 'documented/common/settings/database_adapter.rb', line 42 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.
54 55 56 57 58 59 60 61 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 |
# File 'documented/common/settings/database_adapter.rb', line 54 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.
23 24 25 26 27 28 29 30 |
# File 'documented/common/settings/database_adapter.rb', line 23 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.
34 35 36 |
# File 'documented/common/settings/database_adapter.rb', line 34 def table @table end |