Class: Flipper::Gates::PercentageOfActors

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

Constant Summary

Constants inherited from Flipper::Gate

Flipper::Gate::InstrumentationName

Instance Attribute Summary

Attributes inherited from Flipper::Gate

#feature_name, #instrumenter

Instance Method Summary collapse

Methods inherited from Flipper::Gate

#disable, #enable, #initialize, #inspect, #instrument, #wrap

Constructor Details

This class inherits a constructor from Flipper::Gate

Instance Method Details

#data_typeObject



16
17
18
# File 'lib/flipper/gates/percentage_of_actors.rb', line 16

def data_type
  :integer
end

#description(value) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/flipper/gates/percentage_of_actors.rb', line 20

def description(value)
  if enabled?(value)
    "#{value}% of actors"
  else
    'disabled'
  end
end

#enabled?(value) ⇒ Boolean

Returns:



28
29
30
# File 'lib/flipper/gates/percentage_of_actors.rb', line 28

def enabled?(value)
  !value.nil? && value.to_i > 0
end

#keyObject

Internal: Name converted to value safe for adapter.



12
13
14
# File 'lib/flipper/gates/percentage_of_actors.rb', line 12

def key
  :percentage_of_actors
end

#nameObject

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



7
8
9
# File 'lib/flipper/gates/percentage_of_actors.rb', line 7

def name
  :percentage_of_actors
end

#open?(thing, value) ⇒ Boolean

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

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

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flipper/gates/percentage_of_actors.rb', line 35

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

    if Types::Actor.wrappable?(thing)
      actor = Types::Actor.wrap(thing)
      key = "#{@feature_name}#{actor.value}"
      Zlib.crc32(key) % 100 < percentage
    else
      false
    end
  }
end

#protects?(thing) ⇒ Boolean

Returns:



49
50
51
# File 'lib/flipper/gates/percentage_of_actors.rb', line 49

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