Class: Flipper::Gates::PercentageOfRandom

Inherits:
Flipper::Gate show all
Defined in:
lib/flipper/gates/percentage_of_random.rb

Constant Summary

Constants inherited from Flipper::Gate

Flipper::Gate::InstrumentationName

Instance Attribute Summary

Attributes inherited from Flipper::Gate

#feature, #instrumenter

Instance Method Summary collapse

Methods inherited from Flipper::Gate

#adapter_key, #disable, #enable, #enabled?, #initialize, #inspect, #instrument, #toggle, #toggle_class

Constructor Details

This class inherits a constructor from Flipper::Gate

Instance Method Details

#descriptionObject



29
30
31
32
33
34
35
# File 'lib/flipper/gates/percentage_of_random.rb', line 29

def description
  if enabled?
    "#{toggle.value}% of the time"
  else
    'disabled'
  end
end

#keyObject

Internal: The piece of the adapter key that is unique to the gate class.



10
11
12
# File 'lib/flipper/gates/percentage_of_random.rb', line 10

def key
  :perc_time
end

#nameObject

Internal: The name of the gate. Used for instrumentation, etc.



5
6
7
# File 'lib/flipper/gates/percentage_of_random.rb', line 5

def name
  :percentage_of_random
end

#open?(thing) ⇒ Boolean

Internal: Checks if the gate is open for a thing.

Returns true if gate open for thing, false if not.

Returns:



17
18
19
20
21
22
23
# File 'lib/flipper/gates/percentage_of_random.rb', line 17

def open?(thing)
  instrument(:open?, thing) { |payload|
    percentage = toggle.value.to_i

    rand < (percentage / 100.0)
  }
end

#protects?(thing) ⇒ Boolean

Returns:



25
26
27
# File 'lib/flipper/gates/percentage_of_random.rb', line 25

def protects?(thing)
  thing.is_a?(Flipper::Types::PercentageOfRandom)
end