Module: RSence::Plugins::PluginSqliteDB
- Defined in:
- lib/rsence/plugins/plugin_sqlite_db.rb
Overview
Include this module in your plugin class to automatically create, update and connect/disconnect a sqlite database file.
The Plugin instances including this module will have a @db Sequel object referring to the sqlite database automatically created.
Instance Method Summary collapse
-
#close ⇒ nil
Extends PluginBase#close to close (disconnect) the database object.
-
#create_db_tables ⇒ nil
Extend this method to define tables or initial data for the tables.
-
#flush_db ⇒ nil
Extend this method to do something immediately before the @db object is disconnected.
-
#init ⇒ nil
Extends Plugin#init to specify @db_path as the name of the bundle with a
.db
suffix in the project environmentdb
path. -
#open ⇒ nil
Extends PluginBase#open to open the sqlite database from @db_path as a @db instance variable.
-
#update_db ⇒ nil
Extend this method to do something immediately after the @db object is created.
Instance Method Details
#close ⇒ nil
Extends PluginBase#close to close (disconnect) the database object.
Calls #flush_db (extend with your own method) before closing the database object.
56 57 58 59 60 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 56 def close flush_db @db.disconnect super end |
#create_db_tables ⇒ nil
Extend this method to define tables or initial data for the tables. It’s called once, when the database is created.
92 93 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 92 def create_db_tables end |
#flush_db ⇒ nil
Extend this method to do something immediately before the @db object is disconnected.
An usage scenario would be deleting some junk rows or writing some pending data in memory into the database.
75 76 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 75 def flush_db end |
#init ⇒ nil
Extends Plugin#init to specify @db_path as the name of the bundle with a .db
suffix in the project environment db
path.
Calls #create_db_tables (extend with your own method), if no database is found (typically on the first run in an environment)
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 29 def init super db_dir = File.join( RSence.args[:env_path], 'db' ) @db_path = File.join( db_dir, "#{@name}.db" ) unless File.exist?( @db_path ) @db = Sequel.sqlite( @db_path ) create_db_tables @db.disconnect end end |
#open ⇒ nil
Extends PluginBase#open to open the sqlite database from @db_path as a @db instance variable.
Calls #update_db (extend with your own method) after the database object is created.
45 46 47 48 49 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 45 def open @db = Sequel.sqlite( @db_path ) update_db super end |
#update_db ⇒ nil
Extend this method to do something immediately after the @db object is created.
An usage scenario would be updating some tables or deleting some junk rows.
67 68 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 67 def update_db end |