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.
48 49 50 51 52 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 48 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.
84 85 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 84 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.
67 68 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 67 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)
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 21 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.
37 38 39 40 41 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 37 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.
59 60 |
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 59 def update_db end |