Class: SqliteObjectStore
- Inherits:
-
Object
- Object
- SqliteObjectStore
- Includes:
- Enumerable
- Defined in:
- lib/sqlite_object_store.rb
Instance Attribute Summary collapse
-
#database_path ⇒ Object
Returns the value of attribute database_path.
-
#db ⇒ Object
Returns the value of attribute db.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #close ⇒ Object
- #create_table(table_name) ⇒ Object
- #each ⇒ Object
-
#initialize(database_path, table_name = "default_table") ⇒ SqliteObjectStore
constructor
A new instance of SqliteObjectStore.
- #json_decode(str) ⇒ Object
- #json_encode(value) ⇒ Object
- #keys ⇒ Object
- #open_database(path) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(database_path, table_name = "default_table") ⇒ SqliteObjectStore
Returns a new instance of SqliteObjectStore.
10 11 12 13 14 15 16 |
# File 'lib/sqlite_object_store.rb', line 10 def initialize(database_path, table_name = "default_table") @database_path = database_path @db = open_database(database_path) @table_name = table_name create_table(table_name) @find_statement = db.prepare "select value from #{table_name} where key = ?" end |
Instance Attribute Details
#database_path ⇒ Object
Returns the value of attribute database_path.
9 10 11 |
# File 'lib/sqlite_object_store.rb', line 9 def database_path @database_path end |
#db ⇒ Object
Returns the value of attribute db.
9 10 11 |
# File 'lib/sqlite_object_store.rb', line 9 def db @db end |
#table_name ⇒ Object
Returns the value of attribute table_name.
9 10 11 |
# File 'lib/sqlite_object_store.rb', line 9 def table_name @table_name end |
Instance Method Details
#[](key) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/sqlite_object_store.rb', line 24 def [](key) @find_statement.execute!(String(key)) do |row| return json_decode(row.first) end return nil end |
#[]=(key, value) ⇒ Object
31 32 33 |
# File 'lib/sqlite_object_store.rb', line 31 def []=(key, value) db.execute "insert or replace into #{table_name} (key, value) values (?, ?)", String(key), json_encode(value) end |
#close ⇒ Object
59 60 61 62 63 |
# File 'lib/sqlite_object_store.rb', line 59 def close @find_statement.close db.close @@databases.delete(File.(database_path)) end |
#create_table(table_name) ⇒ Object
55 56 57 |
# File 'lib/sqlite_object_store.rb', line 55 def create_table(table_name) db.execute "create table if not exists #{table_name} (key string primary key, value blob)" end |
#each ⇒ Object
35 36 37 38 39 |
# File 'lib/sqlite_object_store.rb', line 35 def each db.execute "select key, value from #{table_name}" do |row| yield [row[0], json_decode(row[1])] end end |
#json_decode(str) ⇒ Object
69 70 71 |
# File 'lib/sqlite_object_store.rb', line 69 def json_decode(str) Oj.load(str, :mode => :compat) end |
#json_encode(value) ⇒ Object
65 66 67 |
# File 'lib/sqlite_object_store.rb', line 65 def json_encode(value) Oj.dump(value, :mode => :compat) end |
#keys ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/sqlite_object_store.rb', line 41 def keys key_list = [] db.execute "select key from #{table_name}" do |row| key_list << row[0] end key_list end |
#open_database(path) ⇒ Object
18 19 20 21 22 |
# File 'lib/sqlite_object_store.rb', line 18 def open_database(path) full_path = File.(path) @@databases ||= {} @@databases[full_path] ||= SQLite3::Database.new(full_path) end |
#size ⇒ Object
49 50 51 52 53 |
# File 'lib/sqlite_object_store.rb', line 49 def size db.execute "select count(*) from #{table_name}" do |row| return row.first end end |