Sha256: 47b54abb5143d0f641aed57d5ebc1d6012d111866989c5a69ec473067b2dcb31

Contents?: true

Size: 984 Bytes

Versions: 5

Compression:

Stored size: 984 Bytes

Contents

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

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

      def data_type
        :integer
      end

      def description(value)
        if enabled?(value)
          "#{value}% of the time"
        else
          'disabled'
        end
      end

      def enabled?(value)
        !value.nil? && value.to_i > 0
      end

      # Internal: Checks if the gate is open for a thing.
      #
      # Returns true if gate open for thing, false if not.
      def open?(thing, value)
        instrument(:open?, thing) { |payload|
          percentage = value.to_i

          rand < (percentage / 100.0)
        }
      end

      def protects?(thing)
        thing.is_a?(Flipper::Types::PercentageOfRandom)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
flipper-0.6.3 lib/flipper/gates/percentage_of_random.rb
flipper-0.6.2 lib/flipper/gates/percentage_of_random.rb
flipper-0.6.1 lib/flipper/gates/percentage_of_random.rb
flipper-0.6.0 lib/flipper/gates/percentage_of_random.rb
flipper-0.5.0 lib/flipper/gates/percentage_of_random.rb