Module: Lorentz::Keys
Instance Method Summary collapse
- #del(*keys) ⇒ Object
- #exists(key) ⇒ Object
- #keys(pattern) ⇒ Object
- #randomkey ⇒ Object
- #rename(key, newkey) ⇒ Object
- #renamenx(key, newkey) ⇒ Object
Methods included from Glob
Instance Method Details
#del(*keys) ⇒ Object
12 13 14 15 16 |
# File 'lib/lorentz/keys.rb', line 12 def del(*keys) run do keys.map{ |k| @db.delete(k) }.compact.size end end |
#exists(key) ⇒ Object
6 7 8 9 10 |
# File 'lib/lorentz/keys.rb', line 6 def exists(key) run do !!@db[key] end end |
#keys(pattern) ⇒ Object
18 19 20 21 22 |
# File 'lib/lorentz/keys.rb', line 18 def keys(pattern) run do @db.keys.grep(compile(pattern)) end end |
#randomkey ⇒ Object
46 47 48 49 50 51 |
# File 'lib/lorentz/keys.rb', line 46 def randomkey return nil if @db.empty? run do @db.keys[rand(@db.keys.length)] end end |
#rename(key, newkey) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lorentz/keys.rb', line 24 def rename(key, newkey) if key == newkey raise LorentzException, "newkey: #{newkey} must be different than key: #{key}" end raise LorentzException, "key: #{key} does not exist" unless exists(key) save do val = get(key) del(key) set(newkey, val) end end |
#renamenx(key, newkey) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/lorentz/keys.rb', line 36 def renamenx(key, newkey) if key == newkey raise LorentzException, "newkey: #{newkey} must be different than key: #{key}" end raise LorentzException, "key: #{key} does not exist" unless exists(key) return 0 if exists(newkey) rename(key, newkey) return 1 end |