Class: Redis::HashKey
- Inherits:
-
BaseObject
- Object
- BaseObject
- Redis::HashKey
- Includes:
- Enumerable, Redis::Helpers::CoreCommands, Redis::Helpers::Serialize
- Defined in:
- lib/redis/hash_key.rb
Overview
Class representing a Redis hash.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Class Method Summary collapse
-
.[](*args) ⇒ Object
Needed since Redis::Hash masks bare Hash in redis.rb.
Instance Method Summary collapse
-
#[](field) ⇒ Object
Gets the value of a field.
-
#[]=(field, value) ⇒ Object
Sets a field to value.
-
#all ⇒ Object
(also: #clone)
Retrieve the entire hash.
-
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments.
-
#bulk_set(*args) ⇒ Object
Set keys in bulk, takes a hash of field/values => ‘val1’.
-
#clear ⇒ Object
Clears the dict of all keys/values.
-
#delete(field) ⇒ Object
Delete field.
-
#each(&block) ⇒ Object
Enumerate through all fields.
-
#each_key(&block) ⇒ Object
Enumerate through each keys.
-
#each_value(&block) ⇒ Object
Enumerate through all values.
-
#empty? ⇒ Boolean
Returns true if dict is empty.
-
#fetch(field) ⇒ Object
Redis: HGET.
-
#has_key?(field) ⇒ Boolean
(also: #include?, #key?, #member?)
Verify that a field exists.
-
#incrby(field, val = 1) ⇒ Object
(also: #incr)
Increment value by integer at field.
-
#initialize(key, *args) ⇒ HashKey
constructor
A new instance of HashKey.
-
#keys ⇒ Object
Return all the keys of the hash.
-
#size ⇒ Object
(also: #length, #count)
Return the size of the dict.
-
#store(field, value) ⇒ Object
Redis: HSET.
-
#values ⇒ Object
(also: #vals)
Return all the values of the hash.
Methods included from Redis::Helpers::Serialize
Methods included from Redis::Helpers::CoreCommands
#exists?, #expire, #expireat, #move, #rename, #renamenx, #sort, #ttl, #type
Constructor Details
#initialize(key, *args) ⇒ HashKey
Returns a new instance of HashKey.
17 18 19 20 |
# File 'lib/redis/hash_key.rb', line 17 def initialize(key, *args) super @options[:marshal_keys] ||= {} end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/redis/hash_key.rb', line 15 def key @key end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/redis/hash_key.rb', line 15 def @options end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
15 16 17 |
# File 'lib/redis/hash_key.rb', line 15 def redis @redis end |
Class Method Details
.[](*args) ⇒ Object
Needed since Redis::Hash masks bare Hash in redis.rb
23 24 25 |
# File 'lib/redis/hash_key.rb', line 23 def self.[](*args) ::Hash[*args] end |
Instance Method Details
#[](field) ⇒ Object
Gets the value of a field
33 34 35 |
# File 'lib/redis/hash_key.rb', line 33 def [](field) fetch(field) end |
#[]=(field, value) ⇒ Object
Sets a field to value
28 29 30 |
# File 'lib/redis/hash_key.rb', line 28 def []=(field, value) store(field, to_redis(value)) end |
#all ⇒ Object Also known as: clone
Retrieve the entire hash. Redis: HGETALL
72 73 74 75 76 |
# File 'lib/redis/hash_key.rb', line 72 def all h = redis.hgetall(key) h.each { |k,v| h[k] = from_redis(v, [:marshal_keys][k]) } h end |
#bulk_get(*fields) ⇒ Object
Get keys in bulk, takes an array of fields as arguments. Redis: HMGET
120 121 122 123 124 125 126 127 |
# File 'lib/redis/hash_key.rb', line 120 def bulk_get(*fields) hsh = {} res = redis.hmget(key, *fields.flatten) fields.each do |k| hsh[k] = from_redis(res.shift, [:marshal_keys][k]) end hsh end |
#bulk_set(*args) ⇒ Object
Set keys in bulk, takes a hash of field/values => ‘val1’. Redis: HMSET
112 113 114 115 116 117 |
# File 'lib/redis/hash_key.rb', line 112 def bulk_set(*args) raise ArgumentError, "Argument to bulk_set must be hash of key/value pairs" unless args.last.is_a?(::Hash) redis.hmset(key, *args.last.inject([]){ |arr,kv| arr + [kv[0], to_redis(kv[1], [:marshal_keys][kv[0]])] }) end |
#clear ⇒ Object
Clears the dict of all keys/values. Redis: DEL
107 108 109 |
# File 'lib/redis/hash_key.rb', line 107 def clear redis.del(key) end |
#delete(field) ⇒ Object
Delete field. Redis: HDEL
56 57 58 |
# File 'lib/redis/hash_key.rb', line 56 def delete(field) redis.hdel(key, field) end |
#each(&block) ⇒ Object
Enumerate through all fields. Redis: HGETALL
80 81 82 |
# File 'lib/redis/hash_key.rb', line 80 def each(&block) all.each(&block) end |
#each_key(&block) ⇒ Object
Enumerate through each keys. Redis: HKEYS
85 86 87 |
# File 'lib/redis/hash_key.rb', line 85 def each_key(&block) keys.each(&block) end |
#each_value(&block) ⇒ Object
Enumerate through all values. Redis: HVALS
90 91 92 |
# File 'lib/redis/hash_key.rb', line 90 def each_value(&block) values.each(&block) end |
#empty? ⇒ Boolean
Returns true if dict is empty
102 103 104 |
# File 'lib/redis/hash_key.rb', line 102 def empty? true if size == 0 end |
#fetch(field) ⇒ Object
Redis: HGET
43 44 45 |
# File 'lib/redis/hash_key.rb', line 43 def fetch(field) from_redis redis.hget(key, field), [:marshal_keys][field] end |
#has_key?(field) ⇒ Boolean Also known as: include?, key?, member?
Verify that a field exists. Redis: HEXISTS
48 49 50 |
# File 'lib/redis/hash_key.rb', line 48 def has_key?(field) redis.hexists(key, field) end |
#incrby(field, val = 1) ⇒ Object Also known as: incr
Increment value by integer at field. Redis: HINCRBY
130 131 132 133 134 135 136 137 |
# File 'lib/redis/hash_key.rb', line 130 def incrby(field, val = 1) ret = redis.hincrby(key, field, val) unless ret.is_a? Array ret.to_i else nil end end |
#keys ⇒ Object
Return all the keys of the hash. Redis: HKEYS
61 62 63 |
# File 'lib/redis/hash_key.rb', line 61 def keys redis.hkeys(key) end |
#size ⇒ Object Also known as: length, count
Return the size of the dict. Redis: HLEN
95 96 97 |
# File 'lib/redis/hash_key.rb', line 95 def size redis.hlen(key) end |
#store(field, value) ⇒ Object
Redis: HSET
38 39 40 |
# File 'lib/redis/hash_key.rb', line 38 def store(field, value) redis.hset(key, field, to_redis(value, [:marshal_keys][field])) end |
#values ⇒ Object Also known as: vals
Return all the values of the hash. Redis: HVALS
66 67 68 |
# File 'lib/redis/hash_key.rb', line 66 def values from_redis redis.hvals(key) end |