Class: RedisObj::Hash

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/redis_obj/hash.rb

Instance Attribute Summary

Attributes inherited from Base

#key, #redis

Instance Method Summary collapse

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)
    # If its a method available to redis just pass it along with the key
    # as the first argument
    redis.__send__(method,key,*arguments,&blk)
  else
    # If redis responds to the method prefixed with an h pass it along
    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

#clearObject



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

Decriment by one



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

Returns:

  • (Boolean)


90
91
92
# File 'lib/redis_obj/hash.rb', line 90

def empty?
  hlen == 0
end

#has_value?(val) ⇒ Boolean

Returns:

  • (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

#hgetallObject Also known as: to_h



66
67
68
# File 'lib/redis_obj/hash.rb', line 66

def hgetall
  redis.hgetall(key)
end

#hkeysObject



46
47
48
# File 'lib/redis_obj/hash.rb', line 46

def hkeys
  redis.hkeys(key)
end

#hlenObject 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

#hvalsObject 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

Increment by one



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

Returns:

  • (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_aObject



71
72
73
# File 'lib/redis_obj/hash.rb', line 71

def to_a
  to_h.to_a
end