Class: Tsks::Storage
- Inherits:
-
Object
- Object
- Tsks::Storage
- Defined in:
- lib/tsks/storage.rb
Class Method Summary collapse
- .delete(tsk_id) ⇒ Object
- .delete_removed_tsk_ids ⇒ Object
- .init ⇒ Object
- .insert(tsk, ctx = nil) ⇒ Object
- .insert_many(tsks) ⇒ Object
- .select_active ⇒ Object
- .select_all ⇒ Object
- .select_by(params) ⇒ Object
- .select_removed_tsk_ids ⇒ Object
- .update(tsk_id, params = nil) ⇒ Object
-
.update_by(tsk_params, params = nil) ⇒ Object
TODO: write tests.
Class Method Details
.delete(tsk_id) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/tsks/storage.rb', line 125 def self.delete tsk_id storage = get_storage_instance removed_tsks = storage.execute("SELECT * FROM tsks WHERE id=?", tsk_id) if removed_tsks.empty? return false end storage.execute("INSERT INTO removed_tsks (tsk_id) VALUES (?)", removed_tsks[0][0]) storage.execute("DELETE FROM tsks WHERE id=?", tsk_id) end |
.delete_removed_tsk_ids ⇒ Object
147 148 149 150 |
# File 'lib/tsks/storage.rb', line 147 def self.delete_removed_tsk_ids storage = get_storage_instance storage.execute("DELETE FROM removed_tsks") end |
.init ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tsks/storage.rb', line 5 def self.init storage = get_storage_instance storage.execute <<-SQL CREATE TABLE tsks ( id INTEGER PRIMARY KEY UNIQUE, user_id INTEGER DEFAULT 0, tsk VARCHAR NOT NULL, status VARCHAR DEFAULT todo, context VARCHAR DEFAULT inbox, created_at VARCHAR NOT NULL, updated_at VARCHAR NOT NULL ) SQL storage.execute <<-SQL CREATE TABLE removed_tsks ( tsk_id INTEGER UNIQUE NOT NULL ) SQL end |
.insert(tsk, ctx = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tsks/storage.rb', line 26 def self.insert tsk, ctx=nil storage = get_storage_instance now = Time.now.strftime("%Y-%m-%dT%H:%M:%S.%LZ") if ctx storage.execute(" INSERT INTO tsks (tsk, status, context, created_at, updated_at) VALUES (?, ?, ?, ?, ?)", [tsk, 'todo', ctx, now, now] ) else storage.execute(" INSERT INTO tsks (tsk, status, created_at, updated_at) VALUES (?, ?, ?, ?)", [tsk, 'todo', now, now] ) end end |
.insert_many(tsks) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tsks/storage.rb', line 45 def self.insert_many tsks storage = get_storage_instance for tsk in tsks storage.execute(" INSERT INTO tsks (id, user_id, tsk, status, context, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)", [tsk[:id], tsk[:user_id], tsk[:tsk], tsk[:status], tsk[:context], tsk[:created_at], tsk[:updated_at]] ) end end |
.select_active ⇒ Object
119 120 121 122 123 |
# File 'lib/tsks/storage.rb', line 119 def self.select_active storage = get_storage_instance raw_tsks = storage.execute("SELECT rowid, * FROM tsks WHERE status NOT LIKE 'done'") tsks = structure_tsks raw_tsks end |
.select_all ⇒ Object
113 114 115 116 117 |
# File 'lib/tsks/storage.rb', line 113 def self.select_all storage = get_storage_instance raw_tsks = storage.execute("SELECT rowid, * FROM tsks") tsks = structure_tsks raw_tsks end |
.select_by(params) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tsks/storage.rb', line 93 def self.select_by params storage = get_storage_instance raw_tsks = nil if params.count == 2 raw_tsks = storage.execute( "SELECT rowid, * FROM tsks " \ "WHERE #{params.keys.first}=? and #{params.keys.last}=?", [params.values.first, params.values.last] ) else raw_tsks = storage.execute( "SELECT rowid, * FROM tsks WHERE #{params.keys.first}=?", params.values.first) end tsks = structure_tsks raw_tsks end |
.select_removed_tsk_ids ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/tsks/storage.rb', line 136 def self.select_removed_tsk_ids storage = get_storage_instance result = storage.execute("SELECT * FROM removed_tsks") tsk_ids = [] for item in result tsk_ids.append item[0] end return tsk_ids end |
.update(tsk_id, params = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tsks/storage.rb', line 64 def self.update tsk_id, params=nil storage = get_storage_instance if params && params.count == 1 # NOTE # there is only a currently in use case covered by this conditional # ant that is ok for now, but we should make sure it is updated when # Storage.update starting to be called from many different ways. storage.execute( "UPDATE tsks SET " \ "#{params.keys.first}=? " \ "WHERE id=?", [params.values.first, tsk_id]) else storage.execute "UPDATE tsks SET status='done' WHERE id=?", tsk_id end end |
.update_by(tsk_params, params = nil) ⇒ Object
TODO: write tests
83 84 85 86 87 88 89 90 91 |
# File 'lib/tsks/storage.rb', line 83 def self.update_by tsk_params, params=nil storage = get_storage_instance storage.execute( "UPDATE tsks SET " \ "#{params.keys.first}=? " \ "WHERE #{tsk_params.keys.first}=?", [params.values.first, tsk_params.values.first]) end |