Class: Harrods::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/harrods/database.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#dbObject

Returns the value of attribute db.



6
7
8
# File 'lib/harrods/database.rb', line 6

def db
  @db
end

Class Method Details

.loadObject



79
80
81
82
# File 'lib/harrods/database.rb', line 79

def load
  path = Harrods.config.database_path
  new(path)
end

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