Class: Gitlab::Redis::Boolean
- Inherits:
-
Object
- Object
- Gitlab::Redis::Boolean
- 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
-
.decode(value) ⇒ Boolean
Decode a boolean string.
-
.encode(value) ⇒ String
Turn a boolean into a string for storage in Redis.
-
.false?(encoded_value) ⇒ Boolean
Decode a boolean string, then test if it’s false.
-
.true?(encoded_value) ⇒ Boolean
Decode a boolean string, then test if it’s true.
Instance Method Summary collapse
-
#initialize(value) ⇒ Boolean
constructor
A new instance of Boolean.
-
#to_s ⇒ String
The encoded boolean.
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
63 64 65 66 67 68 69 70 71 |
# File 'lib/gitlab/redis/boolean.rb', line 63 def decode(value) raise NotAnEncodedBooleanStringError, value.class unless value.is_a?(String) label, bool_str = *value.split(DELIMITER, 2) raise NotAnEncodedBooleanStringError, label unless label == LABEL from_string(bool_str) end |
.encode(value) ⇒ String
Turn a boolean into a string for storage in Redis
52 53 54 55 56 |
# File 'lib/gitlab/redis/boolean.rb', line 52 def encode(value) raise NotABooleanError, 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
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
78 79 80 |
# File 'lib/gitlab/redis/boolean.rb', line 78 def true?(encoded_value) decode(encoded_value) end |
Instance Method Details
#to_s ⇒ String
Returns the encoded boolean.
42 43 44 |
# File 'lib/gitlab/redis/boolean.rb', line 42 def to_s self.class.encode(@value) end |