Class: Chartspec::Db
- Inherits:
-
Object
- Object
- Chartspec::Db
- Defined in:
- lib/chartspec/db.rb
Instance Method Summary collapse
- #add(file, chart, name, duration, measured_at = Time.now.to_i) ⇒ Object
- #file_name ⇒ Object
-
#initialize(db = nil) ⇒ Db
constructor
A new instance of Db.
- #select_by_file_and_chart(file, chart) ⇒ Object
Constructor Details
#initialize(db = nil) ⇒ Db
Returns a new instance of Db.
5 6 7 8 9 10 11 12 13 |
# File 'lib/chartspec/db.rb', line 5 def initialize db = nil @db_file = db || "tmp/chartspec.sqlite3" db_dirname = File.dirname(@db_file) unless File.directory?(db_dirname) FileUtils.mkdir_p(db_dirname) end @db = SQLite3::Database.new @db_file @db.execute( "CREATE TABLE IF NOT EXISTS specs(id INTEGER PRIMARY KEY, file TEXT, chart TEXT, name TEXT, duration NUMERIC, measured_at DATETIME);" ) end |
Instance Method Details
#add(file, chart, name, duration, measured_at = Time.now.to_i) ⇒ Object
15 16 17 18 19 |
# File 'lib/chartspec/db.rb', line 15 def add(file, chart, name, duration, measured_at = Time.now.to_i) @db.execute("INSERT INTO specs(file, chart, name, duration, measured_at) VALUES (?, ?, ?, ?, ?)", [ file, chart.to_s, name, duration, measured_at ]) end |
#file_name ⇒ Object
27 28 29 |
# File 'lib/chartspec/db.rb', line 27 def file_name @db_file end |
#select_by_file_and_chart(file, chart) ⇒ Object
21 22 23 24 25 |
# File 'lib/chartspec/db.rb', line 21 def select_by_file_and_chart(file, chart) @db.execute( "select name, duration, measured_at from specs where file = ? and chart = ? and measured_at > ?", [ file, chart.to_s, (Time.now - ((ENV['CHARTSPEC_HISTORY_HOURS'] || 8)*3600)).to_i ]) end |