Class: RedHash

Inherits:
Object
  • Object
show all
Defined in:
lib/redness/red_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_key = self.class.name, redis = Red.new) ⇒ RedHash

Returns a new instance of RedHash.



4
5
6
7
# File 'lib/redness/red_hash.rb', line 4

def initialize(hash_key = self.class.name, redis = Red.new)
  self.hash_key = hash_key
  self.redis = redis
end

Instance Attribute Details

#hash_keyObject

Returns the value of attribute hash_key.



2
3
4
# File 'lib/redness/red_hash.rb', line 2

def hash_key
  @hash_key
end

#redisObject

Returns the value of attribute redis.



2
3
4
# File 'lib/redness/red_hash.rb', line 2

def redis
  @redis
end

Instance Method Details

#[](key) ⇒ Object



16
17
18
19
20
# File 'lib/redness/red_hash.rb', line 16

def [](key)
  redis.execute_with_uncertainty(nil) do
    redis.hget(hash_key, key)
  end
end

#[]=(key, value) ⇒ Object



9
10
11
12
13
14
# File 'lib/redness/red_hash.rb', line 9

def []=(key, value)
  redis.execute_with_uncertainty(nil) do
    redis.hset(hash_key, key, value)
    value
  end
end

#allObject



28
29
30
31
32
33
34
# File 'lib/redness/red_hash.rb', line 28

def all
  redis.execute_with_uncertainty({}) do
    hash = redis.hgetall(hash_key)

    HashWithIndifferentAccess.new(hash)
  end
end

#clearObject



36
37
38
39
40
41
42
# File 'lib/redness/red_hash.rb', line 36

def clear
  redis.execute_with_uncertainty(false) do
    redis.hkeys(hash_key).each do |key|
      redis.hdel(hash_key, key)
    end
  end
end

#get(key) ⇒ Object



44
45
46
# File 'lib/redness/red_hash.rb', line 44

def get(key)
  self[key]
end

#remove(key) ⇒ Object



22
23
24
25
26
# File 'lib/redness/red_hash.rb', line 22

def remove(key)
  redis.execute_with_uncertainty(false) do
    redis.hdel(hash_key, key)
  end
end

#set(options) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/redness/red_hash.rb', line 48

def set(options)
  redis.execute_with_uncertainty(false) do
    options.keys.each do |option|
      self[option] = options[option]
    end
  end
end