Sha256: 80884461e4d86bafe23582ff44f1496eea7607d27926f4abb7b275748e47cba7

Contents?: true

Size: 1.7 KB

Versions: 16

Compression:

Stored size: 1.7 KB

Contents

require File.expand_path('../example_setup', __FILE__)

require 'flipper'

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

# Register group
Flipper.register(:team_actor) do |actor|
  actor.is_a?(TeamActor) && actor.allowed?
end

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

  def initialize(id)
    @id = id
  end

  # Must respond to flipper_id
  alias_method :flipper_id, :id
end

class Team
  attr_reader :name

  def initialize(name, members)
    @name = name
    @members = members
  end

  def id
    @name
  end

  def member?(actor)
    @members.include?(actor)
  end
end

class TeamActor
  def initialize(team, actor)
    @team = team
    @actor = actor
  end

  def allowed?
    @team.member?(@actor)
  end

  def flipper_id
    "TeamActor:#{@team.id}:#{@actor.id}"
  end
end

jnunemaker = User.new(1)
jbarnette = User.new(2)
aroben = User.new(3)

core_app = Team.new(:core_app, [jbarnette, jnunemaker])
feature_flags = Team.new(:feature_flags, [aroben, jnunemaker])

core_nunes = TeamActor.new(core_app, jnunemaker)
core_roben = TeamActor.new(core_app, aroben)

if stats.enabled?(core_nunes)
  puts "stats are enabled for jnunemaker"
else
  puts "stats are NOT enabled for jnunemaker"
end

if stats.enabled?(core_roben)
  puts "stats are enabled for aroben"
else
  puts "stats are NOT enabled for aroben"
end

puts "enabling team_actor group"
stats.enable_group :team_actor

if stats.enabled?(core_nunes)
  puts "stats are enabled for jnunemaker"
else
  puts "stats are NOT enabled for jnunemaker"
end

if stats.enabled?(core_roben)
  puts "stats are enabled for aroben"
else
  puts "stats are NOT enabled for aroben"
end

Version data entries

16 entries across 16 versions & 1 rubygems

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