Class: CoverRage::Stores::Sqlite
- Inherits:
-
Object
- Object
- CoverRage::Stores::Sqlite
- Defined in:
- lib/cover_rage/stores/sqlite.rb
Instance Method Summary collapse
- #clear ⇒ Object
-
#initialize(path) ⇒ Sqlite
constructor
A new instance of Sqlite.
- #list ⇒ Object
- #transaction ⇒ Object
- #update(records) ⇒ Object
Constructor Details
#initialize(path) ⇒ Sqlite
Returns a new instance of Sqlite.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cover_rage/stores/sqlite.rb', line 10 def initialize(path) @mutex = Mutex.new @path = path @db = SQLite3::Database.new(path) @db.busy_handler { true } @db.execute <<-SQL create table if not exists records ( path text primary key not null, revision blob not null, source text not null, execution_count text not null, last_executed_at text not null ) SQL process_ext = Module.new process_ext.module_exec(self) do |store| define_method :_fork do store.instance_variable_get(:@db).close pid = super() store.instance_variable_set(:@db, SQLite3::Database.new(path)) store.instance_variable_get(:@db).busy_handler { true } pid end end Process.singleton_class.prepend(process_ext) end |
Instance Method Details
#clear ⇒ Object
74 75 76 |
# File 'lib/cover_rage/stores/sqlite.rb', line 74 def clear @db.execute('delete from records') end |
#list ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cover_rage/stores/sqlite.rb', line 60 def list @db .execute('select path, revision, source, execution_count, last_executed_at from records') .map do |(path, revision, source, execution_count, last_executed_at)| Record.new( path:, revision:, source:, execution_count: JSON.parse(execution_count), last_executed_at: JSON.parse(last_executed_at) ) end end |
#transaction ⇒ Object
37 38 39 40 41 |
# File 'lib/cover_rage/stores/sqlite.rb', line 37 def transaction(&) @mutex.synchronize do @db.transaction(:exclusive, &) end end |
#update(records) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/cover_rage/stores/sqlite.rb', line 43 def update(records) @db.execute( "insert or replace into records (path, revision, source, execution_count, last_executed_at) values #{ (['(?,?,?,?,?)'] * records.length).join(',') }", records.each_with_object([]) do |record, memo| memo.push( record.path, record.revision, record.source, JSON.dump(record.execution_count), JSON.dump(record.last_executed_at) ) end ) end |