Class: Conflow::Redis::HashField

Inherits:
Field
  • Object
show all
Includes:
Enumerable
Defined in:
lib/conflow/redis/hash_field.rb

Overview

Represents Redis hash. It’s methods mirror most used Hash methods.

Instance Attribute Summary

Attributes inherited from Field

#key

Instance Method Summary collapse

Methods inherited from Field

#initialize

Constructor Details

This class inherits a constructor from Conflow::Redis::Field

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/conflow/redis/hash_field.rb', line 53

def ==(other)
  case other
  when Hash      then to_h == other
  when HashField then key == other.key || to_h == other.to_h
  else super
  end
end

#[](field) ⇒ Object



9
10
11
12
# File 'lib/conflow/redis/hash_field.rb', line 9

def [](field)
  value = command(:hget, [key, field])
  value ? JSON.parse(value) : value
end

#[]=(field, value) ⇒ Object



14
15
16
# File 'lib/conflow/redis/hash_field.rb', line 14

def []=(field, value)
  command :hset, [key, field, JSON.dump(value)]
end

#delete(*fields) ⇒ Object



22
23
24
# File 'lib/conflow/redis/hash_field.rb', line 22

def delete(*fields)
  command :hdel, [key, fields]
end

#each(&block) ⇒ Object



49
50
51
# File 'lib/conflow/redis/hash_field.rb', line 49

def each(&block)
  to_h.each(&block)
end

#keysObject



35
36
37
# File 'lib/conflow/redis/hash_field.rb', line 35

def keys
  command(:hkeys, [key]).map(&:to_sym)
end

#merge(hash) ⇒ Object



18
19
20
# File 'lib/conflow/redis/hash_field.rb', line 18

def merge(hash)
  command :hmset, [key, hash.flatten]
end

#overwrite(new_hash) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/conflow/redis/hash_field.rb', line 26

def overwrite(new_hash)
  redis.with do |conn|
    conn.pipelined do
      conn.del(key)
      conn.hmset(key, prepare_hash(new_hash).flatten)
    end
  end
end

#sizeObject



39
40
41
# File 'lib/conflow/redis/hash_field.rb', line 39

def size
  command :hlen, [key]
end

#to_hObject Also known as: to_hash



43
44
45
46
47
# File 'lib/conflow/redis/hash_field.rb', line 43

def to_h
  command(:hgetall, [key]).each_with_object({}) do |(key, value), hash|
    hash[key.to_sym] = JSON.parse(value)
  end
end

#to_sObject



61
62
63
# File 'lib/conflow/redis/hash_field.rb', line 61

def to_s
  to_h.to_s
end