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

Instance Method Details

#closenil

Extends PluginBase#close to close (disconnect) the database object.

Calls #flush_db (extend with your own method) before closing the database object.

Returns:

  • (nil)


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_tablesnil

Extend this method to define tables or initial data for the tables. It’s called once, when the database is created.

Examples:

Creates a table named :my_table and inserts one row.

def create_db_tables
  @db.create_table :my_table do
    primary_key :id
    String :my_text_column
  end
  my_table = @db[:my_table]
  my_table.insert(:my_text_column => 'Some text')
end

Returns:

  • (nil)


84
85
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 84

def create_db_tables
end

#flush_dbnil

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.

Returns:

  • (nil)


67
68
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 67

def flush_db
end

#initnil

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)

Examples:

If your plugin bundle is named my_app, a db/my_app.db database is created under your project environment.

Returns:

  • (nil)


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

#opennil

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.

Returns:

  • (nil)


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_dbnil

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.

Returns:

  • (nil)


59
60
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 59

def update_db
end