Class: Flipper::Gate

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/flipper/gate.rb

Constant Summary collapse

InstrumentationName =

Private: The name of instrumentation events.

"gate_operation.#{InstrumentationNamespace}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(feature, options = {}) ⇒ Gate

Public



21
22
23
24
# File 'lib/flipper/gate.rb', line 21

def initialize(feature, options = {})
  @feature = feature
  @instrumenter = options.fetch(:instrumenter, Flipper::Instrumenters::Noop)
end

Instance Attribute Details

#featureObject (readonly)

Private



13
14
15
# File 'lib/flipper/gate.rb', line 13

def feature
  @feature
end

#instrumenterObject (readonly)

Private: What is used to instrument all the things.



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

def instrumenter
  @instrumenter
end

Instance Method Details

#adapter_keyObject

Internal: The key where details about this gate can be retrieved from the adapter.



39
40
41
# File 'lib/flipper/gate.rb', line 39

def adapter_key
  @key ||= Key.new(@feature.name, key)
end

#disable(thing) ⇒ Object

Internal: Disable this gate for a thing.

Returns the result of Flipper::Toggle#disable.



77
78
79
# File 'lib/flipper/gate.rb', line 77

def disable(thing)
  toggle.disable(thing)
end

#enable(thing) ⇒ Object

Internal: Enable this gate for a thing.

Returns the result of Flipper::Toggle#enable.



70
71
72
# File 'lib/flipper/gate.rb', line 70

def enable(thing)
  toggle.enable(thing)
end

#enabled?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/flipper/gate.rb', line 81

def enabled?
  toggle.enabled?
end

#inspectObject

Public: Pretty string version for debugging.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flipper/gate.rb', line 86

def inspect
  attributes = [
    "feature=#{feature.name.inspect}",
    "description=#{description.inspect}",
    "adapter=#{adapter.name.inspect}",
    "adapter_key=#{adapter_key.inspect}",
    "toggle_class=#{toggle_class.inspect}",
    "toggle_value=#{toggle.value.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end

#instrument(operation, thing) ⇒ Object

Private



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flipper/gate.rb', line 99

def instrument(operation, thing)
  payload = {
    :thing => thing,
    :operation => operation,
    :gate_name => name,
    :feature_name => @feature.name,
  }

  @instrumenter.instrument(InstrumentationName, payload) {
    payload[:result] = yield(payload) if block_given?
  }
end

#keyObject

Private: The piece of the adapter key that is unique to the gate class. Implemented in subclass.



33
34
35
# File 'lib/flipper/gate.rb', line 33

def key
  raise 'Not implemented'
end

#nameObject

Public: The name of the gate. Implemented in subclass.



27
28
29
# File 'lib/flipper/gate.rb', line 27

def name
  raise 'Not implemented'
end

#open?(thing) ⇒ Boolean

Internal: Check if a gate is open for a thing. Implemented in subclass.

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/flipper/gate.rb', line 56

def open?(thing)
  false
end

#protects?(thing) ⇒ Boolean

Internal: Check if a gate is protects a thing. Implemented in subclass.

Returns true if gate protects thing, false if not.

Returns:

  • (Boolean)


63
64
65
# File 'lib/flipper/gate.rb', line 63

def protects?(thing)
  false
end

#toggleObject

Internal: The toggle to use to enable/disable this gate.



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

def toggle
  @toggle ||= toggle_class.new(self)
end

#toggle_classObject

Internal: The toggle class to use for this gate.



44
45
46
# File 'lib/flipper/gate.rb', line 44

def toggle_class
  Toggles::Value
end