Class: Flipflop::FeatureSet
- Inherits:
-
Object
- Object
- Flipflop::FeatureSet
- Defined in:
- lib/flipflop/feature_set.rb
Constant Summary collapse
- @@lock =
Monitor.new
Class Method Summary collapse
Instance Method Summary collapse
- #add(feature) ⇒ Object
- #clear!(feature_key, strategy_key) ⇒ Object
- #configure ⇒ Object
- #enabled?(feature_key) ⇒ Boolean
- #feature(feature_key) ⇒ Object
- #features ⇒ Object
-
#initialize ⇒ FeatureSet
constructor
A new instance of FeatureSet.
- #replace ⇒ Object
- #strategies ⇒ Object
- #strategy(strategy_key) ⇒ Object
- #switch!(feature_key, strategy_key, value) ⇒ Object
- #test!(strategy = Strategies::TestStrategy.new) ⇒ Object
- #use(strategy) ⇒ Object
Constructor Details
#initialize ⇒ FeatureSet
Returns a new instance of FeatureSet.
31 32 33 34 |
# File 'lib/flipflop/feature_set.rb', line 31 def initialize @features = {} @strategies = {} end |
Class Method Details
.current ⇒ Object
24 25 26 |
# File 'lib/flipflop/feature_set.rb', line 24 def current @current or @@lock.synchronize { @current ||= new } end |
Instance Method Details
#add(feature) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/flipflop/feature_set.rb', line 61 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) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/flipflop/feature_set.rb', line 119 def clear!(feature_key, strategy_key) strategy = strategy(strategy_key) feature = feature(feature_key) strategy.clear!(feature.key) end |
#configure ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/flipflop/feature_set.rb', line 36 def configure Module.new do extend Configurable instance_exec(&Proc.new) end self end |
#enabled?(feature_key) ⇒ Boolean
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/flipflop/feature_set.rb', line 79 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) ⇒ Object
92 93 94 95 96 |
# File 'lib/flipflop/feature_set.rb', line 92 def feature(feature_key) @features.fetch(feature_key) do raise FeatureError.new(feature_key, "unknown") end end |
#features ⇒ Object
98 99 100 |
# File 'lib/flipflop/feature_set.rb', line 98 def features @features.values end |
#replace ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/flipflop/feature_set.rb', line 44 def replace @@lock.synchronize do initialize yield if block_given? @features.freeze @strategies.freeze end self end |
#strategies ⇒ Object
108 109 110 |
# File 'lib/flipflop/feature_set.rb', line 108 def strategies @strategies.values end |
#strategy(strategy_key) ⇒ Object
102 103 104 105 106 |
# File 'lib/flipflop/feature_set.rb', line 102 def strategy(strategy_key) @strategies.fetch(strategy_key) do raise StrategyError.new(strategy_key, "unknown") end end |
#switch!(feature_key, strategy_key, value) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/flipflop/feature_set.rb', line 112 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) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/flipflop/feature_set.rb', line 54 def test!(strategy = Strategies::TestStrategy.new) @@lock.synchronize do @strategies = { strategy.key => strategy.freeze }.freeze end strategy end |
#use(strategy) ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/flipflop/feature_set.rb', line 70 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 |