Class: Familia::HashKey

Inherits:
RedisType show all
Defined in:
lib/familia/types/hashkey.rb

Instance Attribute Summary

Attributes inherited from RedisType

#dump_method, #keystring, #load_method, #opts, #parent

Instance Method Summary collapse

Methods inherited from RedisType

#class?, #db, inherited, #initialize, #parent?, #parent_class?, #parent_instance?, #redis, #rediskey, register, #ttl, #uri, valid_keys_only

Methods included from RedisType::Serialization

#from_redis, #multi_from_redis, #multi_from_redis_with_nil, #to_redis, #update_expiration

Methods included from RedisType::Commands

#delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #realttl, #rename, #renamenx, #type

Methods included from Base

add_feature

Constructor Details

This class inherits a constructor from Familia::RedisType

Instance Method Details

#[](field) ⇒ Object Also known as: get



31
32
33
# File 'lib/familia/types/hashkey.rb', line 31

def [](field)
  from_redis redis.hget(rediskey, field)
end

#[]=(field, val) ⇒ Object Also known as: put, store

return [Integer] Returns 1 if the field is new and added, 0 if the

field already existed and the value was updated.


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/familia/types/hashkey.rb', line 16

def []=(field, val)
  ret = redis.hset rediskey, field, to_redis(val)
  update_expiration
  ret
rescue TypeError => e
  Familia.le "[hset]= #{e.message}"
  Familia.ld "[hset]= #{rediskey} #{field}=#{val}" if Familia.debug
  echo :hset, caller(1..1).first if Familia.debug # logs via echo to redis and back
  klass = val.class
  msg = "Cannot store #{field} => #{val.inspect} (#{klass}) in #{rediskey}"
  raise e.class, msg
end

#decrement(field, by = 1) ⇒ Object Also known as: decr, decrby



82
83
84
# File 'lib/familia/types/hashkey.rb', line 82

def decrement(field, by = 1)
  increment field, -by
end

#delete(field) ⇒ Object Also known as: remove, rem, del



69
70
71
# File 'lib/familia/types/hashkey.rb', line 69

def delete(field)
  redis.hdel rediskey, field
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/familia/types/hashkey.rb', line 10

def empty?
  size.zero?
end

#fetch(field, default = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/familia/types/hashkey.rb', line 36

def fetch(field, default = nil)
  ret = self[field]
  if ret.nil?
    raise IndexError, "No such index for: #{field}" if default.nil?

    default
  else
    ret
  end
end

#has_key?(field) ⇒ Boolean Also known as: include?, member?

Returns:

  • (Boolean)


63
64
65
# File 'lib/familia/types/hashkey.rb', line 63

def has_key?(field)
  redis.hexists rediskey, field
end

#hgetallObject Also known as: all



56
57
58
59
60
# File 'lib/familia/types/hashkey.rb', line 56

def hgetall
  # TODO: Use from_redis. Also name `all` is confusing with
  # Onetime::Customer.all which returns all customers.
  redis.hgetall rediskey
end

#increment(field, by = 1) ⇒ Object Also known as: incr, incrby



76
77
78
# File 'lib/familia/types/hashkey.rb', line 76

def increment(field, by = 1)
  redis.hincrby(rediskey, field, by).to_i
end

#keysObject



47
48
49
# File 'lib/familia/types/hashkey.rb', line 47

def keys
  redis.hkeys rediskey
end

#sizeObject Also known as: length



5
6
7
# File 'lib/familia/types/hashkey.rb', line 5

def size
  redis.hlen rediskey
end

#update(hsh = {}) ⇒ Object Also known as: merge!

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
# File 'lib/familia/types/hashkey.rb', line 88

def update(hsh = {})
  raise ArgumentError, 'Argument to bulk_set must be a hash' unless hsh.is_a?(Hash)

  data = hsh.inject([]) { |ret, pair| ret << [pair[0], to_redis(pair[1])] }.flatten

  ret = redis.hmset(rediskey, *data)
  update_expiration
  ret
end

#valuesObject



51
52
53
54
# File 'lib/familia/types/hashkey.rb', line 51

def values
  el = redis.hvals(rediskey)
  multi_from_redis(*el)
end

#values_at(*fields) ⇒ Object



99
100
101
102
# File 'lib/familia/types/hashkey.rb', line 99

def values_at *fields
  el = redis.hmget(rediskey, *fields.flatten.compact)
  multi_from_redis(*el)
end