Class: Flip::FeatureSet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFeatureSet

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

.instanceObject



4
5
6
# File 'lib/flip/feature_set.rb', line 4

def self.instance
  @instance ||= self.new
end

.resetObject



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

#definitionsObject



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.

Returns:

  • (Boolean)


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

#strategiesObject



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