Sha256: f7fc35f89d22350e1744c029803600e0faa9413f5501dcb71ebffaf80cd03149

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

# This example shows how to setup a group that enables a feature for a
# percentage of actors. It could be combined with other logic to enable a
# feature for actors in a particular location or on a particular plan, but only
# for a percentage of them. The percentage is a constant, but could easily be
# plucked from memcached, redis, mysql or whatever.
require File.expand_path('../example_setup', __FILE__)
require 'flipper'

adapter = Flipper::Adapters::Memory.new
flipper = Flipper.new(adapter)
stats = flipper[:stats]

# Some class that represents what will be trying to do something
class User
  attr_reader :id

  def initialize(id)
    @id = id
  end

  # Must respond to flipper_id
  def flipper_id
    "User;#{@id}"
  end
end

PERCENTAGE = 50
Flipper.register(:experimental) do |actor|
  if actor.respond_to?(:flipper_id)
    Zlib.crc32(actor.flipper_id.to_s) % 100 < PERCENTAGE
  else
    false
  end
end

# enable the experimental group
flipper[:stats].enable_group :experimental

# create a bunch of fake users and see how many are enabled
total = 10_000
users = (1..total).map { |n| User.new(n) }
enabled = users.map { |user|
  flipper[:stats].enabled?(user) ? true : nil
}.compact

# show the results
actual = (enabled.size / total.to_f * 100).round
puts "percentage: #{actual} vs hoped for: #{PERCENTAGE}"

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
flipper-0.20.4 examples/percentage_of_actors_group.rb
flipper-0.20.3 examples/percentage_of_actors_group.rb
flipper-0.20.2 examples/percentage_of_actors_group.rb
flipper-0.20.1 examples/percentage_of_actors_group.rb
flipper-0.20.0 examples/percentage_of_actors_group.rb
flipper-0.20.0.beta3 examples/percentage_of_actors_group.rb
flipper-0.20.0.beta2 examples/percentage_of_actors_group.rb
flipper-0.20.0.beta1 examples/percentage_of_actors_group.rb
flipper-0.19.1 examples/percentage_of_actors_group.rb
flipper-0.19.0 examples/percentage_of_actors_group.rb
flipper-0.18.0 examples/percentage_of_actors_group.rb
flipper-0.17.2 examples/percentage_of_actors_group.rb
flipper-0.17.1 examples/percentage_of_actors_group.rb
flipper-0.16.2 examples/percentage_of_actors_group.rb
flipper-0.16.1 examples/percentage_of_actors_group.rb
flipper-0.16.0 examples/percentage_of_actors_group.rb