summaryrefslogtreecommitdiff
path: root/lib/gitlab/redis/boolean.rb
blob: 9b0b20fc2be864dfa3d801aa8d889a88d2aae78f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true

# A serializer for boolean values being stored in Redis.
#
# This is to ensure that booleans are stored in a consistent and
# testable way when being stored as strings in Redis.
#
# Examples:
#
#     bool = Gitlab::Redis::Boolean.new(true)
#     bool.to_s == "_b:1"
#
#     Gitlab::Redis::Boolean.encode(true)
#     => "_b:1"
#
#     Gitlab::Redis::Boolean.decode("_b:1")
#     => true
#
#     Gitlab::Redis::Boolean.true?("_b:1")
#     => true
#
#     Gitlab::Redis::Boolean.true?("_b:0")
#     => false

module Gitlab
  module Redis
    class Boolean
      LABEL = "_b"
      DELIMITER = ":"
      TRUE_STR = "1"
      FALSE_STR = "0"

      BooleanError = Class.new(StandardError)
      NotABooleanError = Class.new(BooleanError)
      NotAnEncodedBooleanStringError = Class.new(BooleanError)

      def initialize(value)
        @value = value
      end

      # @return [String] the encoded boolean
      def to_s
        self.class.encode(@value)
      end

      class << self
        # Turn a boolean into a string for storage in Redis
        #
        # @param value [Boolean] true or false
        # @return [String] the encoded boolean
        # @raise [NotABooleanError] if the value isn't true or false
        def encode(value)
          raise NotABooleanError.new(value) unless bool?(value)

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

        # Decode a boolean string
        #
        # @param value [String] the stored boolean string
        # @return [Boolean] true or false
        # @raise [NotAnEncodedBooleanStringError] if the provided value isn't an encoded boolean
        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

        # Decode a boolean string, then test if it's true
        #
        # @param value [String] the stored boolean string
        # @return [Boolean] is the value true?
        # @raise [NotAnEncodedBooleanStringError] if the provided value isn't an encoded boolean
        def true?(encoded_value)
          decode(encoded_value)
        end

        # Decode a boolean string, then test if it's false
        #
        # @param value [String] the stored boolean string
        # @return [Boolean] is the value false?
        # @raise [NotAnEncodedBooleanStringError] if the provided value isn't an encoded boolean
        def false?(encoded_value)
          !true?(encoded_value)
        end

        private

        def bool?(value)
          [true, false].include?(value)
        end

        def to_string(bool)
          bool ? TRUE_STR : FALSE_STR
        end

        def from_string(str)
          raise NotAnEncodedBooleanStringError.new(str) unless [TRUE_STR, FALSE_STR].include?(str)

          str == TRUE_STR
        end
      end
    end
  end
end