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 62 63 64 |
# File 'lib/flipflop/feature_set.rb', line 57 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 |
#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
75 76 77 78 79 80 81 82 83 |
# File 'lib/flipflop/feature_set.rb', line 75 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
85 86 87 88 89 |
# File 'lib/flipflop/feature_set.rb', line 85 def feature(feature) @features.fetch(feature) do raise FeatureError.new(feature, "unknown") end end |
#features ⇒ Object
91 92 93 |
# File 'lib/flipflop/feature_set.rb', line 91 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
101 102 103 |
# File 'lib/flipflop/feature_set.rb', line 101 def strategies @strategies.values end |
#strategy(strategy) ⇒ Object
95 96 97 98 99 |
# File 'lib/flipflop/feature_set.rb', line 95 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
66 67 68 69 70 71 72 73 |
# File 'lib/flipflop/feature_set.rb', line 66 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 |