Class: Conflow::Redis::ValueField

Inherits:
Field
  • Object
show all
Defined in:
lib/conflow/redis/value_field.rb

Overview

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

Returns Redis response.

Parameters:

  • other (Object)

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

Returns:

  • (String)

    Redis response



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

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

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



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

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

#nil?Boolean

Returns true if object does not exist in Redis, else otherwise.

Returns:

  • (Boolean)

    true if object does not exist in Redis, else otherwise



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

def nil?
  value.nil?
end

#overwrite(value) ⇒ String

Note:

value must be serializable through JSON.dump

Returns Redis response.

Parameters:

  • value (Object)

    new value to be saved

Returns:

  • (String)

    Redis response



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

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

#to_sString? Also known as: to_str

Returns String representation of value.

Returns:

  • (String, nil)

    String representation of value



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

def to_s
  value&.to_s
end

#valueObject

Returns JSON-parsed value present in Redis.

Returns:

  • (Object)

    JSON-parsed value present in Redis



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

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