Sha256: 86dfbe8558f6f88eec5cc18a2a4b6baecad6db6a6c361aaff4f3eed005a0cc17

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'zlib'

module Flipper
  module Gates
    class PercentageOfActors < Gate
      # Internal: The name of the gate. Used for instrumentation, etc.
      def name
        :percentage_of_actors
      end

      # Internal: Name converted to value safe for adapter.
      def key
        :percentage_of_actors
      end

      def data_type
        :integer
      end

      def enabled?(value)
        value > 0
      end

      # Private: this constant is used to support up to 3 decimal places
      # in percentages.
      SCALING_FACTOR = 1_000
      private_constant :SCALING_FACTOR

      # Internal: Checks if the gate is open for a thing.
      #
      # Returns true if gate open for thing, false if not.
      def open?(context)
        return false if context.thing.nil?

        id = "#{context.feature_name}#{context.thing.value}"
        Zlib.crc32(id) % (100 * SCALING_FACTOR) < context.values.percentage_of_actors * SCALING_FACTOR
      end

      def protects?(thing)
        thing.is_a?(Types::PercentageOfActors)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flipper-0.27.1 lib/flipper/gates/percentage_of_actors.rb
flipper-0.27.0 lib/flipper/gates/percentage_of_actors.rb
flipper-0.26.2 lib/flipper/gates/percentage_of_actors.rb
flipper-0.26.1 lib/flipper/gates/percentage_of_actors.rb