module Flipper

Simple class for turning a flipper_id into an actor that can be based to Flipper::Feature#enabled?.

rubocop:disable Metrics/ModuleLength

Constants

InstrumentationNamespace

Private: The namespace for all instrumented events.

VERSION

Public Instance Methods

configuration() click to toggle source

Public: Returns Flipper::Configuration instance.

# File lib/flipper.rb, line 28
def configuration
  @configuration ||= Configuration.new
end
configuration=(configuration) click to toggle source

Public: Sets Flipper::Configuration instance.

# File lib/flipper.rb, line 33
def configuration=(configuration)
  # need to reset flipper instance if configuration changes
  self.instance = nil
  @configuration = configuration
end
configure() { |configuration| ... } click to toggle source

Public: Configure flipper.

Flipper.configure do |config|
  config.default { ... }
end

Yields Flipper::Configuration instance.

# File lib/flipper.rb, line 23
def configure
  yield configuration if block_given?
end
group(name) click to toggle source

Public: Fetches a group by name.

name - The Symbol name of the group.

Examples

Flipper.group(:admins)

Returns Flipper::Group.

# File lib/flipper.rb, line 127
def group(name)
  groups_registry.get(name) || Types::Group.new(name)
end
group_exists?(name) click to toggle source

Public: Check if a group exists

Returns boolean

# File lib/flipper.rb, line 114
def group_exists?(name)
  groups_registry.key?(name)
end
group_names() click to toggle source

Public: Returns a Set of symbols where each symbol is a registered group name. If you just want the names, this is more efficient than doing `Flipper.groups.map(&:name)`.

# File lib/flipper.rb, line 100
def group_names
  groups_registry.keys.to_set
end
groups() click to toggle source

Public: Returns a Set of registered Types::Group instances.

# File lib/flipper.rb, line 93
def groups
  groups_registry.values.to_set
end
groups_registry() click to toggle source

Internal: Registry of all groups_registry.

# File lib/flipper.rb, line 132
def groups_registry
  @groups_registry ||= Registry.new
end
groups_registry=(registry) click to toggle source

Internal: Change the #groups_registry registry.

# File lib/flipper.rb, line 137
def groups_registry=(registry)
  @groups_registry = registry
end
instance() click to toggle source

Public: Default per thread flipper instance if configured. You should not need to use this directly as most of the Flipper::DSL methods are delegated from Flipper module itself. Instead of doing #instance.enabled?(:search), you can use Flipper.enabled?(:search) for the same result.

Returns Flipper::DSL instance.

# File lib/flipper.rb, line 45
def instance
  Thread.current[:flipper_instance] ||= configuration.default
end
instance=(flipper) click to toggle source

Public: Set the flipper instance. It is most common to use the Flipper::Configuration#default to set this instance, but for things like the test environment, this writer is actually useful.

# File lib/flipper.rb, line 52
def instance=(flipper)
  Thread.current[:flipper_instance] = flipper
end
new(adapter, options = {}) click to toggle source

Public: Start here. Given an adapter returns a handy DSL to all the flipper goodness. To see supported options, check out dsl.rb.

# File lib/flipper.rb, line 12
def new(adapter, options = {})
  DSL.new(adapter, options)
end
register(name, &block) click to toggle source

Public: Use this to register a group by name.

name - The Symbol name of the group. block - The block that should be used to determine if the group matches a

given thing.

Examples

Flipper.register(:admins) { |thing|
  thing.respond_to?(:admin?) && thing.admin?
}

Returns a Flipper::Group. Raises Flipper::DuplicateGroup if the group is already registered.

# File lib/flipper.rb, line 84
def register(name, &block)
  group = Types::Group.new(name, &block)
  groups_registry.add(group.name, group)
  group
rescue Registry::DuplicateKey
  raise DuplicateGroup, "Group #{name.inspect} has already been registered"
end
unregister_groups() click to toggle source

Public: Clears the group registry.

Returns nothing.

# File lib/flipper.rb, line 107
def unregister_groups
  groups_registry.clear
end