Class: Picky::Backends::SQLite::Basic
- Includes:
- Helpers::File
- Defined in:
- lib/picky/backends/sqlite/basic.rb
Instance Attribute Summary collapse
-
#cache_path ⇒ Object
readonly
Returns the value of attribute cache_path.
-
#db ⇒ Object
readonly
Lazily creates SQLite client.
Instance Method Summary collapse
- #asynchronous ⇒ Object
- #clear ⇒ Object
- #drop_table ⇒ Object
- #dump(internal) ⇒ Object
- #dump_sqlite(internal) ⇒ Object
- #empty ⇒ Object
- #initial ⇒ Object
-
#initialize(cache_path, options = {}) ⇒ Basic
constructor
A new instance of Basic.
- #load(_) ⇒ Object
- #reset ⇒ Object
- #synchronous ⇒ Object
- #to_s ⇒ Object
- #transaction ⇒ Object
-
#truncate_db ⇒ Object
Drops the table and creates it anew.
Methods included from Helpers::File
Constructor Details
#initialize(cache_path, options = {}) ⇒ Basic
Returns a new instance of Basic.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/picky/backends/sqlite/basic.rb', line 13 def initialize cache_path, = {} @cache_path = "#{cache_path}.sqlite3" @empty = [:empty] @initial = [:initial] @realtime = [:realtime] # Note: If on OSX, too many files get opened during # the specs -> ulimit -n 3000 # # rescue SQLite3::CantOpenException => e # end |
Instance Attribute Details
#cache_path ⇒ Object (readonly)
Returns the value of attribute cache_path.
11 12 13 |
# File 'lib/picky/backends/sqlite/basic.rb', line 11 def cache_path @cache_path end |
#db ⇒ Object (readonly)
Lazily creates SQLite client. Note: Perhaps it would be advisable to create only one, when initialising.
50 51 52 |
# File 'lib/picky/backends/sqlite/basic.rb', line 50 def db @db end |
Instance Method Details
#asynchronous ⇒ Object
86 87 88 89 |
# File 'lib/picky/backends/sqlite/basic.rb', line 86 def asynchronous db.execute 'PRAGMA synchronous = OFF;' self end |
#clear ⇒ Object
43 44 45 |
# File 'lib/picky/backends/sqlite/basic.rb', line 43 def clear db.execute 'delete from key_value' end |
#drop_table ⇒ Object
82 83 84 |
# File 'lib/picky/backends/sqlite/basic.rb', line 82 def drop_table db.execute 'drop table if exists key_value;' end |
#dump(internal) ⇒ Object
34 35 36 37 |
# File 'lib/picky/backends/sqlite/basic.rb', line 34 def dump internal dump_sqlite internal unless @realtime self end |
#dump_sqlite(internal) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/picky/backends/sqlite/basic.rb', line 54 def dump_sqlite internal reset transaction do # Note: Internal structures need to # implement each. # internal.each do |key, value| encoded_value = MultiJson.encode value db.execute 'insert into key_value values (?,?)', key.to_s, encoded_value end end end |
#empty ⇒ Object
30 31 32 |
# File 'lib/picky/backends/sqlite/basic.rb', line 30 def empty @empty && @empty.clone || (@realtime ? self.reset.asynchronous : {}) end |
#initial ⇒ Object
26 27 28 |
# File 'lib/picky/backends/sqlite/basic.rb', line 26 def initial @initial && @initial.clone || (@realtime ? self.reset : {}) end |
#load(_) ⇒ Object
39 40 41 |
# File 'lib/picky/backends/sqlite/basic.rb', line 39 def load _ self end |
#reset ⇒ Object
68 69 70 71 |
# File 'lib/picky/backends/sqlite/basic.rb', line 68 def reset truncate_db self end |
#synchronous ⇒ Object
91 92 93 94 |
# File 'lib/picky/backends/sqlite/basic.rb', line 91 def synchronous db.execute 'PRAGMA synchronous = ON;' self end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/picky/backends/sqlite/basic.rb', line 102 def to_s "#{self.class}(#{cache_path})" end |
#transaction ⇒ Object
96 97 98 99 100 |
# File 'lib/picky/backends/sqlite/basic.rb', line 96 def transaction db.execute 'BEGIN;' yield db.execute 'COMMIT;' end |
#truncate_db ⇒ Object
Drops the table and creates it anew.
THINK Could this be replaced by a truncate (DELETE FROM) statement?
77 78 79 80 |
# File 'lib/picky/backends/sqlite/basic.rb', line 77 def truncate_db drop_table create_table end |