class Flipflop::FeatureSet

Attributes

raise_strategy_errors[R]

Public Class Methods

current() click to toggle source
# File lib/flipflop/feature_set.rb, line 24
def current
  @current or @@lock.synchronize { @current ||= new }
end
new() click to toggle source
# File lib/flipflop/feature_set.rb, line 33
def initialize
  @raise_strategy_errors = (ENV["RACK_ENV"] || ENV["RAILS_ENV"]) != "test"
  @features = {}
  @strategies = {}
end

Public Instance Methods

add(feature) click to toggle source
# File lib/flipflop/feature_set.rb, line 64
def add(feature)
  @@lock.synchronize do
    if @features.has_key?(feature.key)
      raise FeatureError.new(feature.key, "already defined")
    end
    @features[feature.key] = feature.freeze
  end
end
clear!(feature_key, strategy_key) click to toggle source
# File lib/flipflop/feature_set.rb, line 122
def clear!(feature_key, strategy_key)
  strategy = strategy(strategy_key)
  feature = feature(feature_key)

  strategy.clear!(feature.key)
end
configure() click to toggle source
# File lib/flipflop/feature_set.rb, line 39
def configure
  Module.new do
    extend Configurable
    instance_exec(&Proc.new)
  end
  self
end
enabled?(feature_key) click to toggle source
# File lib/flipflop/feature_set.rb, line 82
def enabled?(feature_key)
  FeatureCache.current.fetch(feature_key) do
    feature = feature(feature_key)

    result = @strategies.each_value.inject(nil) do |status, strategy|
      break status unless status.nil?
      strategy.enabled?(feature_key)
    end

    result.nil? ? feature.default : result
  end
end
feature(feature_key) click to toggle source
# File lib/flipflop/feature_set.rb, line 95
def feature(feature_key)
  @features.fetch(feature_key) do
    raise FeatureError.new(feature_key, "unknown")
  end
end
features() click to toggle source
# File lib/flipflop/feature_set.rb, line 101
def features
  @features.values
end
replace() { || ... } click to toggle source
# File lib/flipflop/feature_set.rb, line 47
def replace
  @@lock.synchronize do
    initialize
    yield if block_given?
    @features.freeze
    @strategies.freeze
  end
  self
end
strategies() click to toggle source
# File lib/flipflop/feature_set.rb, line 111
def strategies
  @strategies.values
end
strategy(strategy_key) click to toggle source
# File lib/flipflop/feature_set.rb, line 105
def strategy(strategy_key)
  @strategies.fetch(strategy_key) do
    raise StrategyError.new(strategy_key, "unknown")
  end
end
switch!(feature_key, strategy_key, value) click to toggle source
# File lib/flipflop/feature_set.rb, line 115
def switch!(feature_key, strategy_key, value)
  strategy = strategy(strategy_key)
  feature = feature(feature_key)

  strategy.switch!(feature.key, value)
end
test!(strategy = Strategies::TestStrategy.new) click to toggle source
# File lib/flipflop/feature_set.rb, line 57
def test!(strategy = Strategies::TestStrategy.new)
  @@lock.synchronize do
    @strategies = { strategy.key => strategy.freeze }.freeze
  end
  strategy
end
use(strategy) click to toggle source
# File lib/flipflop/feature_set.rb, line 73
def use(strategy)
  @@lock.synchronize do
    if @strategies.has_key?(strategy.key)
      raise StrategyError.new(strategy.name, "(#{strategy.class}) already defined with identical options")
    end
    @strategies[strategy.key] = strategy.freeze
  end
end