Class: Keymap::ConnectionAdapters::RedisAdapter::RedisHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/keymap/connection_adapters/redis_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, id, sentinel = nil) ⇒ RedisHash

n.b. nil gets represented as an empty string by redis, so the two are in effect identical keys.



96
97
98
99
100
101
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 96

def initialize(connection, id, sentinel=nil)
  @connection = connection
  @id = id
  @sentinel = sentinel
  self[sentinel] = sentinel
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



92
93
94
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 92

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.



92
93
94
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 92

def id
  @id
end

#sentinelObject (readonly)

Returns the value of attribute sentinel.



92
93
94
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 92

def sentinel
  @sentinel
end

Instance Method Details

#[](key) ⇒ Object



107
108
109
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 107

def [](key)
  connection.hget id, key
end

#[]=(key, value) ⇒ Object



111
112
113
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 111

def []=(key, value)
  connection.hset id, key, value
end

#delete(key) ⇒ Object



139
140
141
142
143
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 139

def delete(key)
  value = self[key]
  connection.hdel id, key
  value
end

#eachObject



115
116
117
118
119
120
121
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 115

def each
  if block_given?
    hash_keys.each { |key| yield [key, self[key]] unless key == sentinel }
  else
    ::Enumerable::Enumerator.new(self, :each)
  end
end

#each_pairObject



123
124
125
126
127
128
129
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 123

def each_pair
  if block_given?
    hash_keys.each { |key| yield key, self[key] unless key == sentinel }
  else
    ::Enumerable::Enumerator.new(self, :each_pair)
  end
end

#each_valueObject



131
132
133
134
135
136
137
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 131

def each_value
  if block_given?
    hash_keys.each { |key| yield self[key] unless key == sentinel }
  else
    ::Enumerable::Enumerator.new(self, :each_value)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 103

def empty?
  connection.hlen id == 1
end

#merge!(hash) ⇒ Object Also known as: merge



145
146
147
148
149
150
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 145

def merge!(hash)
  hash.each do |key, value|
    self[key] = value
  end
  self
end