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
- #configure ⇒ Object
- #enabled?(feature) ⇒ Boolean
- #feature(feature) ⇒ Object
- #features ⇒ Object
-
#initialize ⇒ FeatureSet
constructor
A new instance of FeatureSet.
- #reset! ⇒ Object
- #strategies ⇒ Object
- #strategy(strategy) ⇒ Object
- #test!(strategy = Strategies::TestStrategy.new) ⇒ Object
- #use(strategy) ⇒ Object
Constructor Details
#initialize ⇒ FeatureSet
Returns a new instance of FeatureSet.
25 26 27 28 |
# File 'lib/flipflop/feature_set.rb', line 25 def initialize @features = {} @strategies = {} end |
Class Method Details
.current ⇒ Object
18 19 20 |
# File 'lib/flipflop/feature_set.rb', line 18 def current @current or @@lock.synchronize { @current ||= new } end |
Instance Method Details
#add(feature) ⇒ Object
57 58 59 60 61 |
# File 'lib/flipflop/feature_set.rb', line 57 def add(feature) @@lock.synchronize do @features[feature.key] = feature.freeze end end |
#configure ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/flipflop/feature_set.rb', line 30 def configure @@lock.synchronize do initialize Module.new do extend Configurable instance_exec(&Proc.new) end @features.freeze @strategies.freeze end self end |
#enabled?(feature) ⇒ Boolean
69 70 71 72 73 74 75 76 77 |
# File 'lib/flipflop/feature_set.rb', line 69 def enabled?(feature) FeatureCache.current.fetch(feature) do result = @strategies.each_value.inject(nil) do |status, strategy| break status unless status.nil? strategy.enabled?(feature) end result.nil? ? feature(feature).default : result end end |
#feature(feature) ⇒ Object
79 80 81 82 83 |
# File 'lib/flipflop/feature_set.rb', line 79 def feature(feature) @features.fetch(feature) do raise FeatureError.new(feature, "unknown") end end |
#features ⇒ Object
85 86 87 |
# File 'lib/flipflop/feature_set.rb', line 85 def features @features.values end |
#reset! ⇒ Object
43 44 45 46 47 48 |
# File 'lib/flipflop/feature_set.rb', line 43 def reset! @@lock.synchronize do initialize end self end |
#strategies ⇒ Object
95 96 97 |
# File 'lib/flipflop/feature_set.rb', line 95 def strategies @strategies.values end |
#strategy(strategy) ⇒ Object
89 90 91 92 93 |
# File 'lib/flipflop/feature_set.rb', line 89 def strategy(strategy) @strategies.fetch(strategy) do raise StrategyError.new(strategy, "unknown") end end |
#test!(strategy = Strategies::TestStrategy.new) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/flipflop/feature_set.rb', line 50 def test!(strategy = Strategies::TestStrategy.new) @@lock.synchronize do @strategies = { strategy.key => strategy.freeze }.freeze end strategy end |
#use(strategy) ⇒ Object
63 64 65 66 67 |
# File 'lib/flipflop/feature_set.rb', line 63 def use(strategy) @@lock.synchronize do @strategies[strategy.key] = strategy.freeze end end |