Class: RedisObj::Hash
- Inherits:
-
Base
- Object
- Base
- RedisObj::Hash
show all
- Includes:
- Enumerable
- Defined in:
- lib/redis_obj/hash.rb
Instance Attribute Summary
Attributes inherited from Base
#key, #redis
Instance Method Summary
collapse
-
#clear ⇒ Object
-
#dec(field) ⇒ Object
(also: #decr)
-
#each(&blk) ⇒ Object
-
#empty? ⇒ Boolean
-
#has_value?(val) ⇒ Boolean
-
#hdel(field) ⇒ Object
(also: #delete)
-
#hexists(field) ⇒ Object
(also: #key?, #has_key?)
-
#hget(field) ⇒ Object
(also: #[])
-
#hgetall ⇒ Object
(also: #to_h)
-
#hkeys ⇒ Object
-
#hlen ⇒ Object
(also: #size, #count, #length)
-
#hmget(*fields) ⇒ Object
(also: #values_at)
-
#hmset(*fields) ⇒ Object
-
#hset(field, val) ⇒ Object
(also: #[]=)
-
#hvals ⇒ Object
(also: #values)
-
#inc(field) ⇒ Object
(also: #incr)
-
#incby(field, amt) ⇒ Object
(also: #hincrby, #hincrbyfloat, #hincbyfloat)
-
#merge!(hsh) ⇒ Object
-
#method_missing(method, *arguments, &blk) ⇒ Object
Allow for non prefixed versions of the commands to be sent as well as making future proof for new versions of redis.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
-
#to_a ⇒ Object
Methods inherited from Base
#==, #del_key, #get_keys, #initialize
Constructor Details
This class inherits a constructor from RedisObj::Base
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &blk) ⇒ Object
Allow for non prefixed versions of the commands to be sent as well as making future proof for new versions of redis
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/redis_obj/hash.rb', line 111
def method_missing method, *arguments, &blk
if redis.respond_to?(method)
redis.__send__(method,key,*arguments,&blk)
else
if method.to_s[0] != 'h' && (new_method = "h#{method}") && redis.respond_to?(new_method)
self.send(new_method,*arguments,&blk)
else
super
end
end
end
|
Instance Method Details
#clear ⇒ Object
86
87
88
|
# File 'lib/redis_obj/hash.rb', line 86
def clear
redis.del(key) and self
end
|
#dec(field) ⇒ Object
Also known as:
decr
27
28
29
|
# File 'lib/redis_obj/hash.rb', line 27
def dec field
incby(field,-1)
end
|
#each(&blk) ⇒ Object
75
76
77
|
# File 'lib/redis_obj/hash.rb', line 75
def each &blk
to_a.each(&blk)
end
|
#empty? ⇒ Boolean
90
91
92
|
# File 'lib/redis_obj/hash.rb', line 90
def empty?
hlen == 0
end
|
#has_value?(val) ⇒ Boolean
100
101
102
|
# File 'lib/redis_obj/hash.rb', line 100
def has_value? val
values.include?(val)
end
|
#hdel(field) ⇒ Object
Also known as:
delete
4
5
6
|
# File 'lib/redis_obj/hash.rb', line 4
def hdel field
redis.hdel(key,field)
end
|
#hexists(field) ⇒ Object
Also known as:
key?, has_key?
50
51
52
|
# File 'lib/redis_obj/hash.rb', line 50
def hexists field
redis.hexists(key,field)
end
|
#hget(field) ⇒ Object
Also known as:
[]
56
57
58
|
# File 'lib/redis_obj/hash.rb', line 56
def hget field
redis.hget(key,field)
end
|
#hgetall ⇒ Object
Also known as:
to_h
66
67
68
|
# File 'lib/redis_obj/hash.rb', line 66
def hgetall
redis.hgetall(key)
end
|
#hkeys ⇒ Object
46
47
48
|
# File 'lib/redis_obj/hash.rb', line 46
def hkeys
redis.hkeys(key)
end
|
#hlen ⇒ Object
Also known as:
size, count, length
79
80
81
|
# File 'lib/redis_obj/hash.rb', line 79
def hlen
redis.hlen(key)
end
|
#hmget(*fields) ⇒ Object
Also known as:
values_at
32
33
34
|
# File 'lib/redis_obj/hash.rb', line 32
def hmget *fields
redis.hmget(key,*fields)
end
|
#hmset(*fields) ⇒ Object
37
38
39
|
# File 'lib/redis_obj/hash.rb', line 37
def hmset *fields
redis.hmset(key,*fields)
end
|
#hset(field, val) ⇒ Object
Also known as:
[]=
61
62
63
|
# File 'lib/redis_obj/hash.rb', line 61
def hset field, val
redis.hset(key,field,val)
end
|
#hvals ⇒ Object
Also known as:
values
41
42
43
|
# File 'lib/redis_obj/hash.rb', line 41
def hvals
redis.hvals(key)
end
|
#inc(field) ⇒ Object
Also known as:
incr
21
22
23
|
# File 'lib/redis_obj/hash.rb', line 21
def inc field
incby(field,1)
end
|
#incby(field, amt) ⇒ Object
Also known as:
hincrby, hincrbyfloat, hincbyfloat
9
10
11
12
13
14
15
|
# File 'lib/redis_obj/hash.rb', line 9
def incby field, amt
if amt.is_a?(Float)
redis.hincrbyfloat(key,field,amt)
else
redis.hincrby(key,field,amt)
end
end
|
#merge!(hsh) ⇒ Object
104
105
106
107
|
# File 'lib/redis_obj/hash.rb', line 104
def merge! hsh
redis.hmset(key,hsh.to_a.flatten)
self
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
126
127
128
129
130
|
# File 'lib/redis_obj/hash.rb', line 126
def respond_to_missing?(method, include_private = false)
return true if redis.respond_to?(method)
method = method.to_s
method[0] != 'h' && redis.respond_to?("h#{method}") or super
end
|
#to_a ⇒ Object
71
72
73
|
# File 'lib/redis_obj/hash.rb', line 71
def to_a
to_h.to_a
end
|