Class: AppConfig::Storage::SQLite
- Defined in:
- lib/app_config/storage/sqlite.rb
Overview
SQLite storage method.
Constant Summary collapse
- DEFAULTS =
{ database: File.join(Dir.home, '.app_config.sqlite3'), table: 'app_config', }
Instance Method Summary collapse
-
#initialize(options) ⇒ SQLite
constructor
A new instance of SQLite.
- #reload! ⇒ Object
- #save! ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(options) ⇒ SQLite
Returns a new instance of SQLite.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/app_config/storage/sqlite.rb', line 14 def initialize() # Allows passing `true` as an option to just use defaults. if .is_a?(Hash) = DEFAULTS.merge() else = DEFAULTS end @database = ::SQLite3::Database.new([:database]) @table = [:table] fetch_data! end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class AppConfig::Storage::Base
Instance Method Details
#reload! ⇒ Object
28 29 30 |
# File 'lib/app_config/storage/sqlite.rb', line 28 def reload! fetch_data! end |
#save! ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/app_config/storage/sqlite.rb', line 32 def save! data_hash = @data.to_h data_hash.delete(:id) if @id # Update existing row. set_attrs = data_hash.map { |k, v| "#{k} = '#{v}'" }.join(', ') save_query = "UPDATE #{@table} SET #{set_attrs} WHERE id = #{@id};" else # Insert a new row. if data_hash.empty? # Use table defaults. save_query = "INSERT INTO #{@table}(id) VALUES(NULL);" else columns = data_hash.keys.join(', ') values = data_hash.map { |_, v| "'#{v}'" }.join(', ') save_query = "INSERT INTO #{@table} (#{columns}) VALUES (#{values});" end end @database.execute(save_query) @database.changes == 1 end |