Class: Picky::Backends::SQLite::Basic

Inherits:
Object
  • Object
show all
Includes:
Helpers::File
Defined in:
lib/picky/backends/sqlite/basic.rb

Direct Known Subclasses

Array, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::File

#create_directory

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, options = {}
  @cache_path = "#{cache_path}.sqlite3"
  @empty      = options[:empty]
  @initial    = options[:initial]
  @realtime   = options[: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_pathObject (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

#dbObject (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

#asynchronousObject



86
87
88
89
# File 'lib/picky/backends/sqlite/basic.rb', line 86

def asynchronous
  db.execute 'PRAGMA synchronous = OFF;'
  self
end

#clearObject



43
44
45
# File 'lib/picky/backends/sqlite/basic.rb', line 43

def clear
  db.execute 'delete from key_value'
end

#drop_tableObject



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

#emptyObject



30
31
32
# File 'lib/picky/backends/sqlite/basic.rb', line 30

def empty
  @empty && @empty.clone || (@realtime ? self.reset.asynchronous : {})
end

#initialObject



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

#resetObject



68
69
70
71
# File 'lib/picky/backends/sqlite/basic.rb', line 68

def reset
  truncate_db
  self
end

#synchronousObject



91
92
93
94
# File 'lib/picky/backends/sqlite/basic.rb', line 91

def synchronous
  db.execute 'PRAGMA synchronous = ON;'
  self
end

#to_sObject



102
103
104
# File 'lib/picky/backends/sqlite/basic.rb', line 102

def to_s
  "#{self.class}(#{cache_path})"
end

#transactionObject



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_dbObject

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