Class: Flipflop::FeatureSet

Inherits:
Object
  • Object
show all
Defined in:
lib/flipflop/feature_set.rb

Constant Summary collapse

@@lock =
Monitor.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeatureSet

Returns a new instance of FeatureSet.


33
34
35
36
37
# File 'lib/flipflop/feature_set.rb', line 33

def initialize
  @raise_strategy_errors = (ENV["RACK_ENV"] || ENV["RAILS_ENV"]) != "test"
  @features = {}
  @strategies = {}
end

Instance Attribute Details

#raise_strategy_errorsObject (readonly)

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

.currentObject


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


64
65
66
67
68
69
70
71
# 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) ⇒ Object


122
123
124
125
126
127
# 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

#configureObject


39
40
41
42
43
44
45
# 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) ⇒ Boolean

Returns:

  • (Boolean)

82
83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object


95
96
97
98
99
# 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

#featuresObject


101
102
103
# File 'lib/flipflop/feature_set.rb', line 101

def features
  @features.values
end

#replaceObject


47
48
49
50
51
52
53
54
55
# 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

#strategiesObject


111
112
113
# File 'lib/flipflop/feature_set.rb', line 111

def strategies
  @strategies.values
end

#strategy(strategy_key) ⇒ Object


105
106
107
108
109
# 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) ⇒ Object


115
116
117
118
119
120
# 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) ⇒ Object


57
58
59
60
61
62
# 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) ⇒ Object


73
74
75
76
77
78
79
80
# 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