Class: Flipper::Feature

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

Constant Summary collapse

InstrumentationName =

Private: The name of instrumentation events.

"feature_operation.#{InstrumentationNamespace}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, adapter, options = {}) ⇒ Feature

Internal: Initializes a new feature instance.

name - The Symbol or String name of the feature. adapter - The adapter that will be used to store details about this feature.

options - The Hash of options.

:instrumenter - What to use to instrument all the things.


30
31
32
33
34
# File 'lib/flipper/feature.rb', line 30

def initialize(name, adapter, options = {})
  @name = name
  @instrumenter = options.fetch(:instrumenter, Flipper::Instrumenters::Noop)
  @adapter = Adapter.wrap(adapter, :instrumenter => @instrumenter)
end

Instance Attribute Details

#adapterObject (readonly)

Private: The adapter this feature should use.



17
18
19
# File 'lib/flipper/feature.rb', line 17

def adapter
  @adapter
end

#instrumenterObject (readonly)

Private: What is being used to instrument all the things.



20
21
22
# File 'lib/flipper/feature.rb', line 20

def instrumenter
  @instrumenter
end

#nameObject (readonly)

Internal: The name of the feature.



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

def name
  @name
end

Instance Method Details

#boolean_gateObject

Private



132
133
134
# File 'lib/flipper/feature.rb', line 132

def boolean_gate
  @boolean_gate ||= gates.detect { |gate| gate.name == :boolean }
end

#conditional_gatesObject

Private



142
143
144
# File 'lib/flipper/feature.rb', line 142

def conditional_gates
  @conditional_gates ||= non_boolean_gates.select { |gate| gate.enabled? }
end

#descriptionObject

Public



120
121
122
123
124
125
126
127
128
129
# File 'lib/flipper/feature.rb', line 120

def description
  if boolean_gate.enabled?
    boolean_gate.description.capitalize
  elsif conditional_gates.any?
    fragments = conditional_gates.map(&:description)
    "Enabled for #{fragments.join(', ')}"
  else
    boolean_gate.description.capitalize
  end
end

#disable(thing = Types::Boolean.new) ⇒ Object

Public: Disable this feature for something.

Returns the result of Flipper::Gate#disable.



50
51
52
53
54
55
56
# File 'lib/flipper/feature.rb', line 50

def disable(thing = Types::Boolean.new)
  instrument(:disable, thing) { |payload|
    gate = gate_for(thing)
    payload[:gate_name] = gate.name
    gate.disable(thing)
  }
end

#enable(thing = Types::Boolean.new) ⇒ Object

Public: Enable this feature for something.

Returns the result of Flipper::Gate#enable.



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

def enable(thing = Types::Boolean.new)
  instrument(:enable, thing) { |payload|
    gate = gate_for(thing)
    payload[:gate_name] = gate.name
    gate.enable(thing)
  }
end

#enabled?(thing = nil) ⇒ Boolean

Public: Check if a feature is enabled for a thing.

Returns true if enabled, false if not.

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/flipper/feature.rb', line 61

def enabled?(thing = nil)
  instrument(:enabled?, thing) { |payload|
    gate = gates.detect { |gate| gate.open?(thing) }

    if gate.nil?
      false
    else
      payload[:gate_name] = gate.name
      true
    end
  }
end

#gate_for(thing) ⇒ Object

Internal: Find the gate that protects a thing.

thing - The object for which you would like to find a gate

Returns a Flipper::Gate. Raises Flipper::GateNotFound if no gate found for thing



93
94
95
96
# File 'lib/flipper/feature.rb', line 93

def gate_for(thing)
  gates.detect { |gate| gate.protects?(thing) } ||
    raise(GateNotFound.new(thing))
end

#gatesObject

Internal: Gates to check to see if feature is enabled/disabled

Returns an array of gates



77
78
79
80
81
82
83
84
85
# File 'lib/flipper/feature.rb', line 77

def gates
  @gates ||= [
    Gates::Boolean.new(self, :instrumenter => @instrumenter),
    Gates::Group.new(self, :instrumenter => @instrumenter),
    Gates::Actor.new(self, :instrumenter => @instrumenter),
    Gates::PercentageOfActors.new(self, :instrumenter => @instrumenter),
    Gates::PercentageOfRandom.new(self, :instrumenter => @instrumenter),
  ]
end

#inspectObject

Public: Pretty string version for debugging.



99
100
101
102
103
104
105
106
# File 'lib/flipper/feature.rb', line 99

def inspect
  attributes = [
    "name=#{name.inspect}",
    "state=#{state.inspect}",
    "adapter=#{adapter.name.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end

#instrument(operation, thing) ⇒ Object

Private



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/flipper/feature.rb', line 147

def instrument(operation, thing)
  payload = {
    :feature_name => name,
    :operation => operation,
    :thing => thing,
  }

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

#non_boolean_gatesObject

Private



137
138
139
# File 'lib/flipper/feature.rb', line 137

def non_boolean_gates
  @non_boolean_gates ||= gates - [boolean_gate]
end

#stateObject

Public



109
110
111
112
113
114
115
116
117
# File 'lib/flipper/feature.rb', line 109

def state
  if boolean_gate.enabled?
    :on
  elsif conditional_gates.any?
    :conditional
  else
    :off
  end
end