Class: Harrods::Database
- Inherits:
-
Object
- Object
- Harrods::Database
- Defined in:
- lib/harrods/database.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Class Method Summary collapse
Instance Method Summary collapse
- #del(key) ⇒ Object
- #flush! ⇒ Object
- #get(key) ⇒ Object
- #hget(key, field) ⇒ Object
- #hgetall(key) ⇒ Object
- #hmgetall(*keys) ⇒ Object
- #hmset(key, hash) ⇒ Object
- #hset(key, field, value) ⇒ Object
-
#initialize(path) ⇒ Database
constructor
A new instance of Database.
- #reload! ⇒ Object
- #sadd(key, *values) ⇒ Object
- #set(key, value) ⇒ Object
- #smembers(key) ⇒ Object
Constructor Details
#initialize(path) ⇒ Database
Returns a new instance of Database.
8 9 10 |
# File 'lib/harrods/database.rb', line 8 def initialize(path) @db = Daybreak::DB.new path end |
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
6 7 8 |
# File 'lib/harrods/database.rb', line 6 def db @db end |
Class Method Details
Instance Method Details
#del(key) ⇒ Object
20 21 22 |
# File 'lib/harrods/database.rb', line 20 def del(key) @db[key] = nil end |
#flush! ⇒ Object
74 75 76 |
# File 'lib/harrods/database.rb', line 74 def flush! @db.flush! end |
#get(key) ⇒ Object
16 17 18 |
# File 'lib/harrods/database.rb', line 16 def get(key) @db[key] end |
#hget(key, field) ⇒ Object
45 46 47 |
# File 'lib/harrods/database.rb', line 45 def hget(key, field) @db[key].nil? ? nil : @db[key][field] end |
#hgetall(key) ⇒ Object
49 50 51 |
# File 'lib/harrods/database.rb', line 49 def hgetall(key) @db[key] end |
#hmgetall(*keys) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/harrods/database.rb', line 53 def hmgetall(*keys) out = {} keys.each do |key| if key.is_a?(Array) key.each{ |k| out[k] = @db[k] } else out[key] = @db[key] end end out.each{ |key, val| out.delete(key) unless val } return out end |
#hmset(key, hash) ⇒ Object
36 37 38 |
# File 'lib/harrods/database.rb', line 36 def hmset(key, hash) @db[key] = hash end |
#hset(key, field, value) ⇒ Object
40 41 42 43 |
# File 'lib/harrods/database.rb', line 40 def hset(key, field, value) @db[key] ||= {} @db[key][field] = value end |
#reload! ⇒ Object
70 71 72 |
# File 'lib/harrods/database.rb', line 70 def reload! @db.read! end |
#sadd(key, *values) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/harrods/database.rb', line 24 def sadd(key, *values) terms = @db[key] || [] values.each do |val| if val.is_a?(Array) val.each{ |v| terms << v unless terms.include?(v) } else terms << val unless terms.include?(val) end end @db[key] = terms end |
#set(key, value) ⇒ Object
12 13 14 |
# File 'lib/harrods/database.rb', line 12 def set(key, value) @db[key] = value end |
#smembers(key) ⇒ Object
66 67 68 |
# File 'lib/harrods/database.rb', line 66 def smembers(key) @db[key] end |