Sha256: 3b968e67d38ac5dfa8b224ecab831004af39af688aa53b41044ef7a4013b5536

Contents?: true

Size: 1.8 KB

Versions: 14

Compression:

Stored size: 1.8 KB

Contents

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

require 'flipper'
require 'flipper/adapters/memory'

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

# Register group
Flipper.register(:enabled_team_member) do |actor, context|
  combos = context.actors_value.map { |flipper_id| flipper_id.split(";", 2) }
  team_names = combos.select { |class_name, id| class_name == "Team" }.map { |class_name, id| id }
  teams = team_names.map { |name| Team.find(name) }
  teams.any? { |team| team.member?(actor) }
end

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

  def initialize(id)
    @id = id
  end

  def flipper_id
    "User;#{@id}"
  end
end

class Team
  attr_reader :name

  def self.all
    @all ||= {}
  end

  def self.find(name)
    all.fetch(name.to_s)
  end

  def initialize(name, members)
    @name = name.to_s
    @members = members
    self.class.all[@name] = self
  end

  def id
    @name
  end

  def member?(actor)
    @members.map(&:id).include?(actor.id)
  end

  def flipper_id
    "Team:#{@name}"
  end
end

jnunemaker = User.new("jnunemaker")
jbarnette = User.new("jbarnette")
aroben = User.new("aroben")

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

stats.enable_actor jbarnette

actors = [jbarnette, jnunemaker, aroben]

actors.each do |actor|
  if stats.enabled?(actor)
    puts "stats are enabled for #{actor.id}"
  else
    puts "stats are NOT enabled for #{actor.id}"
  end
end

puts "enabling team_actor group"
stats.enable_actor core_app
stats.enable_group :enabled_team_member

actors.each do |actor|
  if stats.enabled?(actor)
    puts "stats are enabled for #{actor.id}"
  else
    puts "stats are NOT enabled for #{actor.id}"
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
flipper-0.15.0 examples/group_dynamic_lookup.rb
flipper-0.14.0 examples/group_dynamic_lookup.rb
flipper-0.13.0 examples/group_dynamic_lookup.rb
flipper-0.13.0.beta1 examples/group_dynamic_lookup.rb
flipper-0.12.2 examples/group_dynamic_lookup.rb
flipper-0.12.1 examples/group_dynamic_lookup.rb
flipper-0.12.0 examples/group_dynamic_lookup.rb
flipper-0.11.0 examples/group_dynamic_lookup.rb
flipper-0.11.0.rc1 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta9 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta8 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta7 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta6 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta5 examples/group_dynamic_lookup.rb