Class: RubyFlipper::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_flipper/feature.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *conditions, &block) ⇒ Feature

Returns a new instance of Feature.



38
39
40
41
42
43
44
45
46
# File 'lib/ruby_flipper/feature.rb', line 38

def initialize(name, *conditions, &block)
  if conditions.last.is_a?(Hash)
    opts = conditions.pop
    @description = opts[:description]
    conditions << opts[:condition]
    conditions << opts[:conditions]
  end
  @name, @conditions = name, [conditions, block].flatten.compact
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



36
37
38
# File 'lib/ruby_flipper/feature.rb', line 36

def conditions
  @conditions
end

#descriptionObject (readonly)

Returns the value of attribute description.



36
37
38
# File 'lib/ruby_flipper/feature.rb', line 36

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/ruby_flipper/feature.rb', line 36

def name
  @name
end

Class Method Details

.add(name, *conditions, &block) ⇒ Object



14
15
16
# File 'lib/ruby_flipper/feature.rb', line 14

def self.add(name, *conditions, &block)
  all[name] = new(name, *conditions, &block)
end

.allObject



6
7
8
# File 'lib/ruby_flipper/feature.rb', line 6

def self.all
  @@features ||= {}
end

.condition_met?(condition, *args) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_flipper/feature.rb', line 22

def self.condition_met?(condition, *args)
  if condition.is_a?(Symbol)
    find(condition).active?
  elsif condition.is_a?(Proc)
    RubyFlipper.silence_warnings do
      !!ConditionContext.new.instance_exec(*args, &condition)
    end
  elsif condition.respond_to?(:call)
    !!condition.call
  else
    !!condition
  end
end

.find(name) ⇒ Object



18
19
20
# File 'lib/ruby_flipper/feature.rb', line 18

def self.find(name)
  all[name] || raise(NotDefinedError, "'#{name}' is not defined")
end

.resetObject



10
11
12
# File 'lib/ruby_flipper/feature.rb', line 10

def self.reset
  @@features = nil
end

Instance Method Details

#active?(*args) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ruby_flipper/feature.rb', line 48

def active?(*args)
  conditions.map {|condition| self.class.condition_met?(condition, *args)}.all?
end