Class: Flipflop::FeatureSet
- Inherits:
-
Object
- Object
- Flipflop::FeatureSet
- Defined in:
- lib/flipflop/feature_set.rb
Constant Summary collapse
- @@lock =
Monitor.new
Instance Attribute Summary collapse
-
#raise_strategy_errors ⇒ Object
Returns the value of attribute raise_strategy_errors.
Class Method Summary collapse
Instance Method Summary collapse
- #add(feature) ⇒ Object
- #clear!(feature_key, strategy_key) ⇒ Object
- #configure(&block) ⇒ 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.
33 34 35 36 |
# File 'lib/flipflop/feature_set.rb', line 33 def initialize @features = {} @strategies = {} end |
Instance Attribute Details
#raise_strategy_errors ⇒ Object
Returns the value of attribute raise_strategy_errors.
31 32 33 |
# File 'lib/flipflop/feature_set.rb', line 31 def raise_strategy_errors @raise_strategy_errors 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
63 64 65 66 67 68 69 70 |
# File 'lib/flipflop/feature_set.rb', line 63 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
121 122 123 124 125 126 |
# File 'lib/flipflop/feature_set.rb', line 121 def clear!(feature_key, strategy_key) strategy = strategy(strategy_key) feature = feature(feature_key) strategy.clear!(feature.key) end |
#configure(&block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/flipflop/feature_set.rb', line 38 def configure(&block) Module.new do extend Configurable instance_exec(&block) end self end |
#enabled?(feature_key) ⇒ Boolean
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/flipflop/feature_set.rb', line 81 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
94 95 96 97 98 |
# File 'lib/flipflop/feature_set.rb', line 94 def feature(feature_key) @features.fetch(feature_key) do raise FeatureError.new(feature_key, "unknown") end end |
#features ⇒ Object
100 101 102 |
# File 'lib/flipflop/feature_set.rb', line 100 def features @features.values end |
#replace ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/flipflop/feature_set.rb', line 46 def replace @@lock.synchronize do initialize yield if block_given? @features.freeze @strategies.freeze end self end |
#strategies ⇒ Object
110 111 112 |
# File 'lib/flipflop/feature_set.rb', line 110 def strategies @strategies.values end |
#strategy(strategy_key) ⇒ Object
104 105 106 107 108 |
# File 'lib/flipflop/feature_set.rb', line 104 def strategy(strategy_key) @strategies.fetch(strategy_key) do raise StrategyError.new(strategy_key, "unknown") end end |
#switch!(feature_key, strategy_key, value) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/flipflop/feature_set.rb', line 114 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
56 57 58 59 60 61 |
# File 'lib/flipflop/feature_set.rb', line 56 def test!(strategy = Strategies::TestStrategy.new) @@lock.synchronize do @strategies = { strategy.key => strategy.freeze }.freeze end strategy end |
#use(strategy) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/flipflop/feature_set.rb', line 72 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 |