Sha256: e3fdc616542f2ca4d6faeb05215ce2e610751965deb3f1f83cdeaeaf50985c7e

Contents?: true

Size: 1.08 KB

Versions: 46

Compression:

Stored size: 1.08 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

      # Internal: Checks if the gate is open for a thing.
      #
      # Returns true if gate open for thing, false if not.
      def open?(context)
        percentage = context.values[key]

        if Types::Actor.wrappable?(context.thing)
          actor = Types::Actor.wrap(context.thing)
          id = "#{context.feature_name}#{actor.value}"
          # this is to support up to 3 decimal places in percentages
          scaling_factor = 1_000
          Zlib.crc32(id) % (100 * scaling_factor) < percentage * scaling_factor
        else
          false
        end
      end

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

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
flipper-0.12.0 lib/flipper/gates/percentage_of_actors.rb
flipper-0.11.0 lib/flipper/gates/percentage_of_actors.rb
flipper-0.11.0.rc1 lib/flipper/gates/percentage_of_actors.rb
flipper-0.11.0.beta9 lib/flipper/gates/percentage_of_actors.rb
flipper-0.11.0.beta8 lib/flipper/gates/percentage_of_actors.rb
flipper-0.11.0.beta7 lib/flipper/gates/percentage_of_actors.rb