Class: State::Db
Instance Method Summary collapse
- #[](key) ⇒ Object (also: #get)
- #[]=(key, val) ⇒ Object (also: #set)
- #blob(val) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #dump(value) ⇒ Object
- #each ⇒ Object
-
#initialize(path, options = {}) ⇒ Db
constructor
A new instance of Db.
- #keys ⇒ Object
- #load(value) ⇒ Object
- #migrate! ⇒ Object
- #size ⇒ Object
- #transaction(&block) ⇒ Object
- #update(key, *val, &block) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(path, options = {}) ⇒ Db
Returns a new instance of Db.
73 74 75 76 77 |
# File 'lib/state.rb', line 73 def initialize path, = {} self.path = path db! migrate! end |
Instance Method Details
#[](key) ⇒ Object Also known as: get
111 112 113 114 115 116 |
# File 'lib/state.rb', line 111 def [] key db.query("select val from #{ table } where key=?", blob(key)) do |result| val = result.each{|row| break row.first} return( val ? load(val) : val ) end end |
#[]=(key, val) ⇒ Object Also known as: set
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/state.rb', line 119 def []= key, val key, val = blob(key), blob(val) transaction do begin statement = "insert into #{ table }(key, val) values (?, ?)" db.execute statement, key, val return val rescue Object begin statement = "update #{ table } set val=? where key=?" db.execute statement, val, key return val rescue Object begin statement = "delete from #{ table } where key=?" db.execute statement, key statement = "insert into #{ table }(key, val) values (?, ?)" db.execute statement, key, val return val rescue Object end end end end end |
#blob(val) ⇒ Object
176 177 178 |
# File 'lib/state.rb', line 176 def blob val SQLite3::Blob.new dump(val) end |
#clear ⇒ Object
79 80 81 |
# File 'lib/state.rb', line 79 def clear db.execute "delete from #{ table }" end |
#delete(key) ⇒ Object
83 84 85 |
# File 'lib/state.rb', line 83 def delete key db.execute "delete from #{ table } where key=?", blob(key) end |
#dump(value) ⇒ Object
180 181 182 |
# File 'lib/state.rb', line 180 def dump value Marshal.dump(value) end |
#each ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/state.rb', line 93 def each db.query("select * from #{ table }") do |result| result.each do |row| key, val, *ignored = row yield load(key), load(val) end end end |
#keys ⇒ Object
103 104 105 |
# File 'lib/state.rb', line 103 def keys map{|pair| pair.first} end |
#load(value) ⇒ Object
184 185 186 |
# File 'lib/state.rb', line 184 def load value Marshal.load value end |
#migrate! ⇒ Object
188 189 190 191 192 193 194 |
# File 'lib/state.rb', line 188 def migrate! begin db.execute "select count(*) from #{ table }" rescue Object db.execute schema end end |
#size ⇒ Object
87 88 89 90 91 |
# File 'lib/state.rb', line 87 def size db.query("select count(*) from #{ table }") do |result| return result.each{|row| break Integer(row.first)} end end |
#transaction(&block) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/state.rb', line 163 def transaction &block if defined?(@transaction) and @transaction block.call db else @transaction = true begin db.transaction{ block.call db } ensure @transaction = false end end end |
#update(key, *val, &block) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/state.rb', line 147 def update key, *val, &block if block and not val.empty? raise ArgumentError, 'both block and value' end if val.empty? and block.nil? raise ArgumentError, 'no update value' end transaction do old = get key val = (block ? block.call(old) : val.first) set key, val end end |
#values ⇒ Object
107 108 109 |
# File 'lib/state.rb', line 107 def values map{|pair| pair.last} end |