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

      # Internal: The piece of the adapter key that is unique to the gate class.
      def key
        :perc_time
      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

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

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

      # Internal: The piece of the adapter key that is unique to the gate class.
      def key
        :perc_time
      end

      def enable(thing)
        adapter.write adapter_key, thing.value
        true
      end

      def disable(thing)
        adapter.delete adapter_key
        true
      end

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

      def value
        adapter.read adapter_key
      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

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

Remove toggle from % of random
Remove toggle from % of random
