Class: Keymap::ConnectionAdapters::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.



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

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.



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

def connection
  @connection
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#sentinelObject (readonly)

Returns the value of attribute sentinel.



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

def sentinel
  @sentinel
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



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

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

#delete(key) ⇒ Object



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

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

#eachObject



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

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



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

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



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

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)


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

def empty?
  connection.hlen id == 1
end

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



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

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