Class: Dipswitch::Features

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dipswitch/features.rb

Overview

A Collection of Features with some helper methods to make the overall determination of a feature with multiple gates is enabled or not

Instance Method Summary collapse

Constructor Details

#initializeFeatures

Returns a new instance of Features.



10
11
12
# File 'lib/dipswitch/features.rb', line 10

def initialize
  @features = {}
end

Instance Method Details

#add(name, &block) ⇒ Object



18
19
20
21
# File 'lib/dipswitch/features.rb', line 18

def add(name, &block)
  @features[name] ||= []
  @features[name] << Feature.new(name, block)
end

#clear!Object



14
15
16
# File 'lib/dipswitch/features.rb', line 14

def clear!
  @features = {}
end

#eachObject



43
44
45
46
47
# File 'lib/dipswitch/features.rb', line 43

def each
  to_a.each do |f|
    yield f
  end
end

#for(*args) ⇒ Object



31
32
33
34
35
36
# File 'lib/dipswitch/features.rb', line 31

def for(*args)
  to_a.reduce({}) do |acc, feature|
    acc[feature.name] = true if feature.on?(*args)
    acc
  end
end

#on?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/dipswitch/features.rb', line 23

def on?(name, *args)
  Array(@features[name.to_sym]).any? {|feature| feature.on?(*args) }
end

#to_aObject



39
40
41
# File 'lib/dipswitch/features.rb', line 39

def to_a
  @features.values.flatten
end

#with(name, *args, &block) ⇒ Object



27
28
29
# File 'lib/dipswitch/features.rb', line 27

def with(name, *args, &block)
  block.call if on?(name, *args)
end