Class: Rack::Bug::SpeedTracerPanel::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bug/panels/speedtracer_panel/database.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name) ⇒ Database

Returns a new instance of Database.



30
31
32
33
34
35
36
37
38
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 30

def initialize(table_name)
  @table_name = table_name
  if(db.execute("select * from sqlite_master where name = ?", table_name).empty?)
    db.execute("create table #@table_name ( key varchar primary key, value varchar )")
  end
  if Rails.logger
    Rails.logger.debug{ "Initializing a table called #{table_name}" }
  end
end

Class Method Details

.dbObject



8
9
10
11
12
13
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 8

def db
  if @db.nil?
    open_database
  end
  return @db
end

.open_databaseObject



15
16
17
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 15

def open_database
  @db = SQLite3::Database.new("rack_bug.sqlite")
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 40

def [](key)
  rows = db.execute("select value from #@table_name where key = ?", key.to_s)
  if rows.empty?
    return nil
  else
    Marshal.load(rows.first.first)
  end
end

#[]=(key, value) ⇒ Object



49
50
51
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 49

def []=(key, value)
  db.execute("insert or replace into #@table_name ( key, value ) values ( ?, ? )", key.to_s, Marshal.dump(value))
end

#dbObject



26
27
28
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 26

def db
  self.class.db
end

#keysObject



53
54
55
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 53

def keys
  db.execute("select key from #@table_name").flatten
end

#to_aObject



57
58
59
60
61
# File 'lib/rack/bug/panels/speedtracer_panel/database.rb', line 57

def to_a
  db.execute("select key, value from #@table_name").map do |key, value|
    [key, Marshal.load(value)]
  end
end