Class: Conflow::Redis::ValueField Private

Inherits:
Field
  • Object
show all
Defined in:
lib/conflow/redis/value_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 single value (Redis String). Values are serialized as JSON in order to preserve type.

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:

  • other (Object)

    Object to compare value with. Handles Strings, Numerics, Symbols and other Conflow::Redis::ValueField objects

Returns:

  • (Boolean)

    true if equal



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

def ==(other)
  case other
  when String, Numeric then value == other
  when Symbol          then value.to_sym == other
  when ValueField      then key == other.key || to_s == other.to_s
  else super
  end
end

#default(value) ⇒ 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.

Note:

value must be serializable through JSON.dump

Returns Redis response.

Parameters:

  • value (Object)

    value to be assigned to field (unless key already holds value)

Returns:

  • (String)

    Redis response



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

def default(value)
  command :set, [key, JSON.dump(value), nx: true]
end

#nil?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 object does not exist in Redis, else otherwise.

Returns:

  • (Boolean)

    true if object does not exist in Redis, else otherwise



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

def nil?
  value.nil?
end

#overwrite(value) ⇒ 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.

Note:

value must be serializable through JSON.dump

Returns Redis response.

Parameters:

  • value (Object)

    new value to be saved

Returns:

  • (String)

    Redis response



11
12
13
# File 'lib/conflow/redis/value_field.rb', line 11

def overwrite(value)
  command :set, [key, JSON.dump(value)]
end

#to_sString? Also known as: to_str

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 value.

Returns:

  • (String, nil)

    String representation of value



40
41
42
# File 'lib/conflow/redis/value_field.rb', line 40

def to_s
  value&.to_s
end

#valueObject

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 JSON-parsed value present in Redis.

Returns:

  • (Object)

    JSON-parsed value present in Redis



45
46
47
48
# File 'lib/conflow/redis/value_field.rb', line 45

def value
  result = command(:get, [key])
  result && JSON.parse(result)
end