Sha256: 1679a53ec0a204c3f7511c46f8f0addefbabfa29d8b93d38a4d1eceedde43cef

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 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 description(value)
        if enabled?(value)
          "#{value}% of actors"
        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

          if Types::Actor.wrappable?(thing)
            actor = Types::Actor.wrap(thing)
            key = "#{@feature_name}#{actor.value}"
            Zlib.crc32(key) % 100 < percentage
          else
            false
          end
        }
      end

      def protects?(thing)
        thing.is_a?(Flipper::Types::PercentageOfActors)
      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_actors.rb
flipper-0.6.2 lib/flipper/gates/percentage_of_actors.rb
flipper-0.6.1 lib/flipper/gates/percentage_of_actors.rb
flipper-0.6.0 lib/flipper/gates/percentage_of_actors.rb
flipper-0.5.0 lib/flipper/gates/percentage_of_actors.rb