class Flipper::Feature

Constants

InstrumentationName

Private: The name of feature instrumentation events.

Attributes

adapter[R]

Private: The adapter this feature should use.

instrumenter[R]

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

key[R]

Public: Name converted to value safe for adapter.

name[R]

Public: The name of the feature.

Public Class Methods

new(name, adapter, options = {}) click to toggle source

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.
# File lib/flipper/feature.rb, line 32
def initialize(name, adapter, options = {})
  @name = name
  @key = name.to_s
  @instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
  @adapter = adapter
end

Public Instance Methods

actors_value() click to toggle source

Public: Get the adapter value for the actors gate.

Returns Set of String flipper_id's.

# File lib/flipper/feature.rb, line 263
def actors_value
  gate_values.actors
end
add() click to toggle source

Public: Adds this feature.

Returns the result of Adapter#add.

# File lib/flipper/feature.rb, line 74
def add
  instrument(:add) { adapter.add(self) }
end
boolean_value() click to toggle source

Public: Get the adapter value for the boolean gate.

Returns true or false.

# File lib/flipper/feature.rb, line 270
def boolean_value
  gate_values.boolean
end
clear() click to toggle source

Public: Clears all gate values for this feature.

Returns the result of Adapter#clear.

# File lib/flipper/feature.rb, line 95
def clear
  instrument(:clear) { adapter.clear(self) }
end
conditional?() click to toggle source

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

# File lib/flipper/feature.rb, line 229
def conditional?
  state == :conditional
end
disable(thing = false) click to toggle source

Public: Disable this feature for something.

Returns the result of Adapter#disable.

# File lib/flipper/feature.rb, line 58
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) click to toggle source

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.

# File lib/flipper/feature.rb, line 168
def disable_actor(actor)
  disable Types::Actor.wrap(actor)
end
disable_group(group) click to toggle source

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.

# File lib/flipper/feature.rb, line 178
def disable_group(group)
  disable Types::Group.wrap(group)
end
disable_percentage_of_actors() click to toggle source

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.

# File lib/flipper/feature.rb, line 198
def disable_percentage_of_actors
  disable Types::PercentageOfActors.new(0)
end
disable_percentage_of_time() click to toggle source

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.

# File lib/flipper/feature.rb, line 188
def disable_percentage_of_time
  disable Types::PercentageOfTime.new(0)
end
disabled_gate_names() click to toggle source

Public: Get the names of the disabled gates.

Returns an Array of gate names.

# File lib/flipper/feature.rb, line 313
def disabled_gate_names
  disabled_gates.map(&:name)
end
disabled_gates() click to toggle source

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

Returns an Array of Flipper::Gate instances.

# File lib/flipper/feature.rb, line 306
def disabled_gates
  gates - enabled_gates
end
disabled_groups() click to toggle source

Public: Get groups not enabled for this feature.

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

# File lib/flipper/feature.rb, line 249
def disabled_groups
  Flipper.groups - enabled_groups
end
enable(thing = true) click to toggle source

Public: Enable this feature for something.

Returns the result of Adapter#enable.

# File lib/flipper/feature.rb, line 42
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) click to toggle source

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.

# File lib/flipper/feature.rb, line 128
def enable_actor(actor)
  enable Types::Actor.wrap(actor)
end
enable_group(group) click to toggle source

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.

# File lib/flipper/feature.rb, line 138
def enable_group(group)
  enable Types::Group.wrap(group)
end
enable_percentage_of_actors(percentage) click to toggle source

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.

# File lib/flipper/feature.rb, line 158
def enable_percentage_of_actors(percentage)
  enable Types::PercentageOfActors.wrap(percentage)
end
enable_percentage_of_time(percentage) click to toggle source

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.

# File lib/flipper/feature.rb, line 148
def enable_percentage_of_time(percentage)
  enable Types::PercentageOfTime.wrap(percentage)
end
enabled?(thing = nil) click to toggle source

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

Returns true if enabled, false if not.

# File lib/flipper/feature.rb, line 102
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_names() click to toggle source

Public: Get the names of the enabled gates.

Returns an Array of gate names.

# File lib/flipper/feature.rb, line 299
def enabled_gate_names
  enabled_gates.map(&:name)
end
enabled_gates() click to toggle source

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

Returns an Array of Flipper::Gate instances.

# File lib/flipper/feature.rb, line 291
def enabled_gates
  values = gate_values
  gates.select { |gate| gate.enabled?(values[gate.key]) }
end
enabled_groups() click to toggle source

Public: Get groups enabled for this feature.

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

# File lib/flipper/feature.rb, line 241
def enabled_groups
  groups_value.map { |name| Flipper.group(name) }.to_set
end
Also aliased as: groups
exist?() click to toggle source

Public: Does this feature exist in the adapter.

Returns true if exists in adapter else false.

# File lib/flipper/feature.rb, line 81
def exist?
  instrument(:exist?) { adapter.features.include?(key) }
end
gate(name) click to toggle source

Public: Find a gate by name.

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

# File lib/flipper/feature.rb, line 354
def gate(name)
  gates.detect { |gate| gate.name == name.to_sym }
end
gate_for(thing) click to toggle source

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

# File lib/flipper/feature.rb, line 364
def gate_for(thing)
  gates.detect { |gate| gate.protects?(thing) } || raise(GateNotFound, thing)
end
gate_values() click to toggle source

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

# File lib/flipper/feature.rb, line 234
def gate_values
  GateValues.new(adapter.get(self))
end
gates() click to toggle source

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

Returns an array of gates

# File lib/flipper/feature.rb, line 341
def gates
  @gates ||= [
    Gates::Boolean.new,
    Gates::Actor.new,
    Gates::PercentageOfActors.new,
    Gates::PercentageOfTime.new,
    Gates::Group.new,
  ]
end
groups()
Alias for: enabled_groups
groups_value() click to toggle source

Public: Get the adapter value for the groups gate.

Returns Set of String group names.

# File lib/flipper/feature.rb, line 256
def groups_value
  gate_values.groups
end
inspect() click to toggle source

Public: Pretty string version for debugging.

# File lib/flipper/feature.rb, line 328
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?() click to toggle source

Public: Is the feature fully disabled.

# File lib/flipper/feature.rb, line 223
def off?
  state == :off
end
on?() click to toggle source

Public: Is the feature fully enabled.

# File lib/flipper/feature.rb, line 218
def on?
  state == :on
end
percentage_of_actors_value() click to toggle source

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.

# File lib/flipper/feature.rb, line 277
def percentage_of_actors_value
  gate_values.percentage_of_actors
end
percentage_of_time_value() click to toggle source

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.

# File lib/flipper/feature.rb, line 284
def percentage_of_time_value
  gate_values.percentage_of_time
end
remove() click to toggle source

Public: Removes this feature.

Returns the result of Adapter#remove.

# File lib/flipper/feature.rb, line 88
def remove
  instrument(:remove) { adapter.remove(self) }
end
state() click to toggle source

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

# File lib/flipper/feature.rb, line 203
def state
  values = gate_values
  boolean = gate(:boolean)
  non_boolean_gates = gates - [boolean]

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

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

# File lib/flipper/feature.rb, line 323
def to_param
  to_s
end
to_s() click to toggle source

Public: Returns the string representation of the feature.

# File lib/flipper/feature.rb, line 318
def to_s
  name.to_s
end

Private Instance Methods

instrument(operation) { |payload| ... } click to toggle source

Private: Instrument a feature operation.

# File lib/flipper/feature.rb, line 371
def instrument(operation)
  @instrumenter.instrument(InstrumentationName) do |payload|
    payload[:feature_name] = name
    payload[:operation] = operation
    payload[:result] = yield(payload) if block_given?
  end
end