Sha256: 8f58d367c6e4e92b0db694f89bf8e362966074cde0e4b7483f0ce9f57d54249c

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 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)
        Typecast.to_integer(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?(thing, value, options = {})
        feature_name = options.fetch(:feature_name)
        percentage = Typecast.to_integer(value)

        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

2 entries across 2 versions & 1 rubygems

Version Path
flipper-0.7.0.beta2 lib/flipper/gates/percentage_of_actors.rb
flipper-0.7.0.beta1 lib/flipper/gates/percentage_of_actors.rb