Class: Gitlab::Redis::Boolean

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/redis/boolean.rb

Constant Summary collapse

LABEL =
"_b"
DELIMITER =
":"
TRUE_STR =
"1"
FALSE_STR =
"0"
BooleanError =
Class.new(StandardError)
NotABooleanError =
Class.new(BooleanError)
NotAnEncodedBooleanStringError =
Class.new(BooleanError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Boolean

Returns a new instance of Boolean.


37
38
39
# File 'lib/gitlab/redis/boolean.rb', line 37

def initialize(value)
  @value = value
end

Class Method Details

.decode(value) ⇒ Boolean

Decode a boolean string

Parameters:

  • value (String)

    the stored boolean string

Returns:

Raises:


63
64
65
66
67
68
69
70
71
# File 'lib/gitlab/redis/boolean.rb', line 63

def decode(value)
  raise NotAnEncodedBooleanStringError.new(value.class) unless value.is_a?(String)

  label, bool_str = *value.split(DELIMITER, 2)

  raise NotAnEncodedBooleanStringError.new(label) unless label == LABEL

  from_string(bool_str)
end

.encode(value) ⇒ String

Turn a boolean into a string for storage in Redis

Parameters:

  • value (Boolean)

    true or false

Returns:

  • (String)

    the encoded boolean

Raises:


52
53
54
55
56
# File 'lib/gitlab/redis/boolean.rb', line 52

def encode(value)
  raise NotABooleanError.new(value) unless bool?(value)

  [LABEL, to_string(value)].join(DELIMITER)
end

.false?(encoded_value) ⇒ Boolean

Decode a boolean string, then test if it's false

Parameters:

  • value (String)

    the stored boolean string

Returns:

  • (Boolean)

    is the value false?

Raises:


87
88
89
# File 'lib/gitlab/redis/boolean.rb', line 87

def false?(encoded_value)
  !true?(encoded_value)
end

.true?(encoded_value) ⇒ Boolean

Decode a boolean string, then test if it's true

Parameters:

  • value (String)

    the stored boolean string

Returns:

Raises:


78
79
80
# File 'lib/gitlab/redis/boolean.rb', line 78

def true?(encoded_value)
  decode(encoded_value)
end

Instance Method Details

#to_sString

Returns the encoded boolean.

Returns:

  • (String)

    the encoded boolean


42
43
44
# File 'lib/gitlab/redis/boolean.rb', line 42

def to_s
  self.class.encode(@value)
end