Class: Flip::FeatureSet
- Inherits:
-
Object
- Object
- Flip::FeatureSet
- Defined in:
- lib/flip/feature_set.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
writeonly
Sets the default for definitions which fall through the strategies.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(definition) ⇒ Object
Adds a feature definition to the set.
-
#add_strategy(strategy) ⇒ Object
Adds a strategy for determing feature status.
- #default_for(definition) ⇒ Object
- #definitions ⇒ Object
-
#initialize ⇒ FeatureSet
constructor
A new instance of FeatureSet.
-
#on?(key) ⇒ Boolean
Whether the given feature is switched on.
- #strategies ⇒ Object
- #strategy(klass) ⇒ Object
Constructor Details
#initialize ⇒ FeatureSet
Returns a new instance of FeatureSet.
16 17 18 19 20 |
# File 'lib/flip/feature_set.rb', line 16 def initialize @definitions = Hash.new { |_, k| raise "No feature declared with key #{k.inspect}" } @strategies = Hash.new { |_, k| raise "No strategy named #{k}" } @default = false end |
Instance Attribute Details
#default=(value) ⇒ Object (writeonly)
Sets the default for definitions which fall through the strategies. Accepts boolean or a Proc to be called.
14 15 16 |
# File 'lib/flip/feature_set.rb', line 14 def default=(value) @default = value end |
Class Method Details
.instance ⇒ Object
4 5 6 |
# File 'lib/flip/feature_set.rb', line 4 def self.instance @instance ||= self.new end |
.reset ⇒ Object
8 9 10 |
# File 'lib/flip/feature_set.rb', line 8 def self.reset @instance = nil end |
Instance Method Details
#<<(definition) ⇒ Object
Adds a feature definition to the set.
30 31 32 |
# File 'lib/flip/feature_set.rb', line 30 def << definition @definitions[definition.key] = definition end |
#add_strategy(strategy) ⇒ Object
Adds a strategy for determing feature status.
35 36 37 38 |
# File 'lib/flip/feature_set.rb', line 35 def add_strategy(strategy) strategy = strategy.new if strategy.is_a? Class @strategies[strategy.name] = strategy end |
#default_for(definition) ⇒ Object
44 45 46 |
# File 'lib/flip/feature_set.rb', line 44 def default_for(definition) @default.is_a?(Proc) ? @default.call(definition) : @default end |
#definitions ⇒ Object
48 49 50 |
# File 'lib/flip/feature_set.rb', line 48 def definitions @definitions.values end |
#on?(key) ⇒ Boolean
Whether the given feature is switched on.
23 24 25 26 27 |
# File 'lib/flip/feature_set.rb', line 23 def on? key d = @definitions[key] @strategies.each_value { |s| return s.on?(d) if s.knows?(d) } default_for d end |
#strategies ⇒ Object
52 53 54 |
# File 'lib/flip/feature_set.rb', line 52 def strategies @strategies.values end |
#strategy(klass) ⇒ Object
40 41 42 |
# File 'lib/flip/feature_set.rb', line 40 def strategy(klass) @strategies[klass] end |