Class: Redpear::Store::Hash
- Inherits:
-
Enumerable
- Object
- Base
- Enumerable
- Redpear::Store::Hash
- Defined in:
- lib/redpear/store/hash.rb
Constant Summary
Constants inherited from Base
Base::IS_NIL, Base::IS_ONE, Base::IS_TRUE, Base::IS_ZERO, Base::PICK_FIRST, Base::TO_INT, Base::TO_SET
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Comparator.
-
#all ⇒ Hash
(also: #to_hash, #value)
All pairs.
- #decrement(field, value = 1) ⇒ Object
- #delete(field) ⇒ Object
- #each {|field, value| ... } ⇒ Object
-
#empty? ⇒ Boolean
True, if empty.
-
#fetch(field) ⇒ String
(also: #[])
Value stored in
field
. - #increment(field, value = 1) ⇒ Object
-
#key?(field) ⇒ Boolean
(also: #has_key?, #member?, #include?)
True, if field exists.
-
#keys ⇒ Array
All keys.
-
#length ⇒ Intege
The number of pairs in the hash.
- #merge!(hash) ⇒ Object
- #store(field, value, options = {}) ⇒ Object (also: #[]=)
- #update(hash) ⇒ Object
-
#values ⇒ Array
All values.
-
#values_at(*fields) ⇒ Array
Values.
Methods inherited from Enumerable
Methods inherited from Base
#clear, #exists?, #expire, #expire_at, #expire_in, #initialize, #inspect, #purge!, temporary, #ttl, #type, #watch
Constructor Details
This class inherits a constructor from Redpear::Store::Base
Instance Method Details
#==(other) ⇒ Boolean
Comparator
120 121 122 |
# File 'lib/redpear/store/hash.rb', line 120 def ==(other) other.respond_to?(:to_hash) && other.to_hash == to_hash end |
#all ⇒ Hash Also known as: to_hash, value
Returns all pairs.
11 12 13 |
# File 'lib/redpear/store/hash.rb', line 11 def all conn.hgetall(key) || {} end |
#decrement(field, value = 1) ⇒ Object
91 92 93 |
# File 'lib/redpear/store/hash.rb', line 91 def decrement(field, value = 1) increment(field, -value) end |
#delete(field) ⇒ Object
19 20 21 |
# File 'lib/redpear/store/hash.rb', line 19 def delete(field) conn.hdel key, field end |
#each {|field, value| ... } ⇒ Object
6 7 8 |
# File 'lib/redpear/store/hash.rb', line 6 def each(&block) all.each(&block) end |
#empty? ⇒ Boolean
Returns true, if empty.
32 33 34 35 36 37 38 39 |
# File 'lib/redpear/store/hash.rb', line 32 def empty? case value = length when Redis::Future value.instance_eval { @transformation = IS_ZERO } else value.zero? end end |
#fetch(field) ⇒ String Also known as: []
Returns value stored in field
.
44 45 46 |
# File 'lib/redpear/store/hash.rb', line 44 def fetch(field) conn.hget key, field end |
#increment(field, value = 1) ⇒ Object
83 84 85 |
# File 'lib/redpear/store/hash.rb', line 83 def increment(field, value = 1) conn.hincrby key, field, value end |
#key?(field) ⇒ Boolean Also known as: has_key?, member?, include?
Returns true, if field exists.
24 25 26 |
# File 'lib/redpear/store/hash.rb', line 24 def key?(field) conn.hexists(key, field) end |
#keys ⇒ Array
Returns all keys.
65 66 67 |
# File 'lib/redpear/store/hash.rb', line 65 def keys conn.hkeys key end |
#length ⇒ Intege
Returns the number of pairs in the hash.
75 76 77 |
# File 'lib/redpear/store/hash.rb', line 75 def length conn.hlen key end |
#merge!(hash) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/redpear/store/hash.rb', line 111 def merge!(hash) hash = hash.reject do |field, value| delete(field) if value.nil? end conn.hmset key, *hash.flatten unless hash.empty? end |
#store(field, value, options = {}) ⇒ Object Also known as: []=
55 56 57 58 59 60 61 |
# File 'lib/redpear/store/hash.rb', line 55 def store(field, value, = {}) if value.nil? delete field else conn.hset key, field, value end end |
#update(hash) ⇒ Object
104 105 106 107 |
# File 'lib/redpear/store/hash.rb', line 104 def update(hash) result = merge!(hash) Redis::Future === result ? result : to_hash end |
#values ⇒ Array
Returns all values.
70 71 72 |
# File 'lib/redpear/store/hash.rb', line 70 def values conn.hvals key end |
#values_at(*fields) ⇒ Array
Returns values.
98 99 100 |
# File 'lib/redpear/store/hash.rb', line 98 def values_at(*fields) conn.hmget key, *fields end |