Class: Linkage::MatchSets::Database
- Inherits:
-
Linkage::MatchSet
- Object
- Linkage::MatchSet
- Linkage::MatchSets::Database
- Includes:
- Helpers::Database
- Defined in:
- lib/linkage/match_sets/database.rb
Overview
MatchSets::Database is an implementation of Linkage::MatchSet for saving matches in a relational database.
Matches are saved in a database table with the following columns:
- id_1 (string)
- id_2 (string)
- score (float)
You can setup a database connection in a few different ways. By default, a
SQLite database with the filename of matches.db
will be created in the
current working directory. If you want something different, you can either
specify a Sequel-style URI, provide connection options for
Sequel.connect
, or you can just specify a Sequel::Database
object to
use.
There are a couple of non-Sequel connection options:
:filename
- specify filename to use for a SQLite database:dir
- specify the parent directory for a SQLite database
In addition to connection options, there are behavioral options you can
set. By default, the table name used is called matches
, but you change
that by setting the :table_name
option in the second options hash. If
the table already exists, an ExistsError will be raised unless you set
the :overwrite
option to a truthy value in the second options hash.
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :filename => 'matches.db' }
Instance Method Summary collapse
- #add_match(id_1, id_2, score) ⇒ Object
- #close ⇒ Object
-
#initialize(connection_options = {}, options = {}) ⇒ Database
constructor
A new instance of Database.
- #open_for_writing ⇒ Object
Methods included from Helpers::Database
Methods inherited from Linkage::MatchSet
Constructor Details
#initialize(connection_options = {}, options = {}) ⇒ Database
Returns a new instance of Database.
45 46 47 48 49 |
# File 'lib/linkage/match_sets/database.rb', line 45 def initialize( = {}, = {}) @database = database_connection(, DEFAULT_OPTIONS) @table_name = [:table_name] || :matches @overwrite = [:overwrite] end |
Instance Method Details
#add_match(id_1, id_2, score) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/linkage/match_sets/database.rb', line 69 def add_match(id_1, id_2, score) raise "not in write mode" if @mode != :write @dataset.insert({ :id_1 => id_1, :id_2 => id_2, :score => score }) end |
#close ⇒ Object
79 80 81 |
# File 'lib/linkage/match_sets/database.rb', line 79 def close @mode = nil end |
#open_for_writing ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/linkage/match_sets/database.rb', line 51 def open_for_writing return if @mode == :write if @overwrite @database.drop_table?(@table_name) elsif @database.table_exists?(@table_name) raise ExistsError, "#{@table_name} table exists and not in overwrite mode" end @database.create_table(@table_name) do String :id_1 String :id_2 Float :score end @dataset = @database[@table_name] @mode = :write end |