Class: Flipper::Feature

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

InstrumentationName =

Private: The name of feature instrumentation events.

"feature_operation.#{InstrumentationNamespace}".freeze

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.


33
34
35
36
37
38
# File 'lib/flipper/feature.rb', line 33

def initialize(name, adapter, options = {})
  @name = name
  @key = name.to_s
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Private: The adapter this feature should use.



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

def adapter
  @adapter
end

#instrumenterObject (readonly)

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



23
24
25
# File 'lib/flipper/feature.rb', line 23

def instrumenter
  @instrumenter
end

#keyObject (readonly)

Public: Name converted to value safe for adapter.



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

def key
  @key
end

#nameObject (readonly)

Public: The name of the feature.



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

def name
  @name
end

Instance Method Details

#actors_valueObject

Public: Get the adapter value for the actors gate.

Returns Set of String flipper_id’s.



264
265
266
# File 'lib/flipper/feature.rb', line 264

def actors_value
  gate_values.actors
end

#addObject

Public: Adds this feature.

Returns the result of Adapter#add.



75
76
77
# File 'lib/flipper/feature.rb', line 75

def add
  instrument(:add) { adapter.add(self) }
end

#boolean_valueObject

Public: Get the adapter value for the boolean gate.

Returns true or false.



271
272
273
# File 'lib/flipper/feature.rb', line 271

def boolean_value
  gate_values.boolean
end

#clearObject

Public: Clears all gate values for this feature.

Returns the result of Adapter#clear.



96
97
98
# File 'lib/flipper/feature.rb', line 96

def clear
  instrument(:clear) { adapter.clear(self) }
end

#conditional?Boolean

Public: Is the feature conditionally enabled for a given actor, group, percentage of actors or percentage of the time.

Returns:

  • (Boolean)


230
231
232
# File 'lib/flipper/feature.rb', line 230

def conditional?
  state == :conditional
end

#disable(thing = false) ⇒ Object

Public: Disable this feature for something.

Returns the result of Adapter#disable.



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

def disable(thing = false)
  instrument(:disable) do |payload|
    adapter.add self

    gate = gate_for(thing)
    wrapped_thing = gate.wrap(thing)
    payload[:gate_name] = gate.name
    payload[:thing] = wrapped_thing

    adapter.disable self, gate, wrapped_thing
  end
end

#disable_actor(actor) ⇒ Object

Public: Disables a feature for an actor.

actor - a Flipper::Types::Actor instance or an object that responds

to flipper_id.

Returns result of disable.



169
170
171
# File 'lib/flipper/feature.rb', line 169

def disable_actor(actor)
  disable Types::Actor.wrap(actor)
end

#disable_group(group) ⇒ Object

Public: Disables a feature for a group.

group - a Flipper::Types::Group instance or a String or Symbol name of a

registered group.

Returns result of disable.



179
180
181
# File 'lib/flipper/feature.rb', line 179

def disable_group(group)
  disable Types::Group.wrap(group)
end

#disable_percentage_of_actorsObject

Public: Disables a feature for a percentage of actors.

percentage - a Flipper::Types::PercentageOfTime instance or an object that

responds to to_i.

Returns result of disable.



199
200
201
# File 'lib/flipper/feature.rb', line 199

def disable_percentage_of_actors
  disable Types::PercentageOfActors.new(0)
end

#disable_percentage_of_timeObject

Public: Disables a feature a percentage of time.

percentage - a Flipper::Types::PercentageOfTime instance or an object that

responds to to_i.

Returns result of disable.



189
190
191
# File 'lib/flipper/feature.rb', line 189

def disable_percentage_of_time
  disable Types::PercentageOfTime.new(0)
end

#disabled_gate_namesObject

Public: Get the names of the disabled gates.

Returns an Array of gate names.



314
315
316
# File 'lib/flipper/feature.rb', line 314

def disabled_gate_names
  disabled_gates.map(&:name)
end

#disabled_gatesObject

Public: Get the gates that have not been enabled for the feature.

Returns an Array of Flipper::Gate instances.



307
308
309
# File 'lib/flipper/feature.rb', line 307

def disabled_gates
  gates - enabled_gates
end

#disabled_groupsObject

Public: Get groups not enabled for this feature.

Returns Set of Flipper::Types::Group instances.



250
251
252
# File 'lib/flipper/feature.rb', line 250

def disabled_groups
  Flipper.groups - enabled_groups
end

#enable(thing = true) ⇒ Object

Public: Enable this feature for something.

Returns the result of Adapter#enable.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/flipper/feature.rb', line 43

def enable(thing = true)
  instrument(:enable) do |payload|
    adapter.add self

    gate = gate_for(thing)
    wrapped_thing = gate.wrap(thing)
    payload[:gate_name] = gate.name
    payload[:thing] = wrapped_thing

    adapter.enable self, gate, wrapped_thing
  end
end

#enable_actor(actor) ⇒ Object

Public: Enables a feature for an actor.

actor - a Flipper::Types::Actor instance or an object that responds

to flipper_id.

Returns result of enable.



129
130
131
# File 'lib/flipper/feature.rb', line 129

def enable_actor(actor)
  enable Types::Actor.wrap(actor)
end

#enable_group(group) ⇒ Object

Public: Enables a feature for a group.

group - a Flipper::Types::Group instance or a String or Symbol name of a

registered group.

Returns result of enable.



139
140
141
# File 'lib/flipper/feature.rb', line 139

def enable_group(group)
  enable Types::Group.wrap(group)
end

#enable_percentage_of_actors(percentage) ⇒ Object

Public: Enables a feature for a percentage of actors.

percentage - a Flipper::Types::PercentageOfTime instance or an object that

responds to to_i.

Returns result of enable.



159
160
161
# File 'lib/flipper/feature.rb', line 159

def enable_percentage_of_actors(percentage)
  enable Types::PercentageOfActors.wrap(percentage)
end

#enable_percentage_of_time(percentage) ⇒ Object

Public: Enables a feature a percentage of time.

percentage - a Flipper::Types::PercentageOfTime instance or an object that

responds to to_i.

Returns result of enable.



149
150
151
# File 'lib/flipper/feature.rb', line 149

def enable_percentage_of_time(percentage)
  enable Types::PercentageOfTime.wrap(percentage)
end

#enabled?(thing = nil) ⇒ Boolean

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

Returns true if enabled, false if not.

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/flipper/feature.rb', line 103

def enabled?(thing = nil)
  instrument(:enabled?) do |payload|
    values = gate_values
    thing = gate(:actor).wrap(thing) unless thing.nil?
    payload[:thing] = thing
    context = FeatureCheckContext.new(
      feature_name: @name,
      values: values,
      thing: thing
    )

    if open_gate = gates.detect { |gate| gate.open?(context) }
      payload[:gate_name] = open_gate.name
      true
    else
      false
    end
  end
end

#enabled_gate_namesObject

Public: Get the names of the enabled gates.

Returns an Array of gate names.



300
301
302
# File 'lib/flipper/feature.rb', line 300

def enabled_gate_names
  enabled_gates.map(&:name)
end

#enabled_gatesObject

Public: Get the gates that have been enabled for the feature.

Returns an Array of Flipper::Gate instances.



292
293
294
295
# File 'lib/flipper/feature.rb', line 292

def enabled_gates
  values = gate_values
  gates.select { |gate| gate.enabled?(values[gate.key]) }
end

#enabled_groupsObject Also known as: groups

Public: Get groups enabled for this feature.

Returns Set of Flipper::Types::Group instances.



242
243
244
# File 'lib/flipper/feature.rb', line 242

def enabled_groups
  groups_value.map { |name| Flipper.group(name) }.to_set
end

#exist?Boolean

Public: Does this feature exist in the adapter.

Returns true if exists in adapter else false.

Returns:

  • (Boolean)


82
83
84
# File 'lib/flipper/feature.rb', line 82

def exist?
  instrument(:exist?) { adapter.features.include?(key) }
end

#gate(name) ⇒ Object

Public: Find a gate by name.

Returns a Flipper::Gate if found, nil if not.



355
356
357
# File 'lib/flipper/feature.rb', line 355

def gate(name)
  gates.detect { |gate| gate.name == name.to_sym }
end

#gate_for(thing) ⇒ Object

Public: 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



365
366
367
# File 'lib/flipper/feature.rb', line 365

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

#gate_valuesObject

Public: Returns the raw gate values stored by the adapter.



235
236
237
# File 'lib/flipper/feature.rb', line 235

def gate_values
  GateValues.new(adapter.get(self))
end

#gatesObject

Public: Get all the gates used to determine enabled/disabled for the feature.

Returns an array of gates



342
343
344
345
346
347
348
349
350
# File 'lib/flipper/feature.rb', line 342

def gates
  @gates ||= [
    Gates::Boolean.new,
    Gates::Actor.new,
    Gates::PercentageOfActors.new,
    Gates::PercentageOfTime.new,
    Gates::Group.new,
  ]
end

#groups_valueObject

Public: Get the adapter value for the groups gate.

Returns Set of String group names.



257
258
259
# File 'lib/flipper/feature.rb', line 257

def groups_value
  gate_values.groups
end

#inspectObject

Public: Pretty string version for debugging.



329
330
331
332
333
334
335
336
337
# File 'lib/flipper/feature.rb', line 329

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

#off?Boolean

Public: Is the feature fully disabled.

Returns:

  • (Boolean)


224
225
226
# File 'lib/flipper/feature.rb', line 224

def off?
  state == :off
end

#on?Boolean

Public: Is the feature fully enabled.

Returns:

  • (Boolean)


219
220
221
# File 'lib/flipper/feature.rb', line 219

def on?
  state == :on
end

#percentage_of_actors_valueObject

Public: Get the adapter value for the percentage of actors gate.

Returns Integer greater than or equal to 0 and less than or equal to 100.



278
279
280
# File 'lib/flipper/feature.rb', line 278

def percentage_of_actors_value
  gate_values.percentage_of_actors
end

#percentage_of_time_valueObject

Public: Get the adapter value for the percentage of time gate.

Returns Integer greater than or equal to 0 and less than or equal to 100.



285
286
287
# File 'lib/flipper/feature.rb', line 285

def percentage_of_time_value
  gate_values.percentage_of_time
end

#removeObject

Public: Removes this feature.

Returns the result of Adapter#remove.



89
90
91
# File 'lib/flipper/feature.rb', line 89

def remove
  instrument(:remove) { adapter.remove(self) }
end

#stateObject

Public: Returns state for feature (:on, :off, or :conditional).



204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/flipper/feature.rb', line 204

def state
  values = gate_values
  boolean = gate(:boolean)
  non_boolean_gates = gates - [boolean]

  if values.boolean || values.percentage_of_actors == 100 || values.percentage_of_time == 100
    :on
  elsif non_boolean_gates.detect { |gate| gate.enabled?(values[gate.key]) }
    :conditional
  else
    :off
  end
end

#to_paramObject

Public: Identifier to be used in the url (a rails-ism).



324
325
326
# File 'lib/flipper/feature.rb', line 324

def to_param
  to_s
end

#to_sObject

Public: Returns the string representation of the feature.



319
320
321
# File 'lib/flipper/feature.rb', line 319

def to_s
  name.to_s
end