Class: Picky::Backends::SQLite::Array

Inherits:
Basic show all
Defined in:
lib/picky/backends/sqlite/array.rb

Direct Known Subclasses

IntegerKeyArray, StringKeyArray

Instance Attribute Summary

Attributes inherited from Basic

#cache_path, #db

Instance Method Summary collapse

Methods inherited from Basic

#asynchronous, #clear, #drop_table, #dump, #dump_sqlite, #empty, #initial, #initialize, #load, #reset, #synchronous, #to_s, #transaction, #truncate_db

Methods included from Helpers::File

#create_directory

Constructor Details

This class inherits a constructor from Picky::Backends::SQLite::Basic

Instance Method Details

#[](key) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/picky/backends/sqlite/array.rb', line 29

def [] key
  res = db.execute "SELECT value FROM key_value WHERE key = ? LIMIT 1",
                   key.to_s

  array = res.blank? ? [] : MultiJson.decode(res.first.first)
  DirectlyManipulable.make self, array, key
  array
end

#[]=(key, array) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/picky/backends/sqlite/array.rb', line 18

def []= key, array
  unless array.empty?
    db.execute 'INSERT OR REPLACE INTO key_value (key,value) VALUES (?,?)',
               key.to_s,
               MultiJson.encode(array)
  end

  DirectlyManipulable.make self, array, key
  array
end

#create_tableObject



9
10
11
# File 'lib/picky/backends/sqlite/array.rb', line 9

def create_table
  db.execute 'create table key_value (key varchar(255), value text);'
end

#delete(key) ⇒ Object



38
39
40
# File 'lib/picky/backends/sqlite/array.rb', line 38

def delete key
  db.execute "DELETE FROM key_value WHERE key = (?)", key.to_s
end

#sizeObject



13
14
15
16
# File 'lib/picky/backends/sqlite/array.rb', line 13

def size
  result = db.execute 'SELECT COUNT(*) FROM key_value'
  result.first.first.to_i
end