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)


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_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)


92
93
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 92

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)


75
76
# File 'lib/rsence/plugins/plugin_sqlite_db.rb', line 75

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)


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

#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)


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_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)


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

def update_db
end