Class: Conflow::Redis::HashField Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if equal.

Parameters:

Returns:

  • (Boolean)

    true if equal



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

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

#each(&block) ⇒ Enumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterates over hash.

Returns:

  • (Enumerator)

    if no block given

See Also:



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

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

#merge(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merges hashes, similar to Hash#merge

Examples:

field.merge(smart: true, degree: :none)

Parameters:

  • hash (Hash)

    hash of keys and values to be merged

Returns:

  • (String)

    Redis response



16
17
18
# File 'lib/conflow/redis/hash_field.rb', line 16

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

#overwrite(new_hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Replaces currently stored hash with one given as param

Examples:

field.overwrite(smart: true, degree: :none)

Parameters:

  • new_hash (Hash)

    hash of keys and values to be stored

Returns:

  • (String)

    Redis response



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

#to_hString Also known as: to_hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates Ruby Hash based on soted values. Keys will be symbolized and values JSON-parsed

Examples:

field.to_h #=> { smart: true, degree: "none" }

Returns:

  • (String)

    Redis response



40
41
42
43
44
# File 'lib/conflow/redis/hash_field.rb', line 40

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

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns string representation of the hash.

Returns:

  • (String)

    string representation of the hash



64
65
66
# File 'lib/conflow/redis/hash_field.rb', line 64

def to_s
  to_h.to_s
end