Class: Keymap::ConnectionAdapters::RedisAdapter::RedisHash
- Inherits:
-
Object
- Object
- Keymap::ConnectionAdapters::RedisAdapter::RedisHash
- Includes:
- Enumerable
- Defined in:
- lib/keymap/connection_adapters/redis_adapter.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#sentinel ⇒ Object
readonly
Returns the value of attribute sentinel.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each ⇒ Object
- #each_pair ⇒ Object
- #each_value ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(connection, id, sentinel = nil) ⇒ RedisHash
constructor
n.b.
- #merge!(hash) ⇒ Object (also: #merge)
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
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
92 93 94 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 92 def connection @connection end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
92 93 94 |
# File 'lib/keymap/connection_adapters/redis_adapter.rb', line 92 def id @id end |
#sentinel ⇒ Object (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 |
#each ⇒ Object
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_pair ⇒ Object
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_value ⇒ Object
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
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 |