Sha256: ff73ea4a2196dada60ef46e697615b7eced1114f495461e14d44d4d1e3da53f3

Contents?: true

Size: 1.8 KB

Versions: 6

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

6 entries across 6 versions & 1 rubygems

Version Path
flipper-0.11.0.beta4 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta3 examples/group_dynamic_lookup.rb
flipper-0.11.0.beta1 examples/group_dynamic_lookup.rb
flipper-0.10.2 examples/group_dynamic_lookup.rb
flipper-0.10.1 examples/group_dynamic_lookup.rb
flipper-0.10.0 examples/group_dynamic_lookup.rb