/
flipper.rb
73 lines (66 loc) 路 1.86 KB
/
flipper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Flipper
# Private: The namespace for all instrumented events.
InstrumentationNamespace = :flipper
# Public: Start here. Given an adapter returns a handy DSL to all the flipper
# goodness. To see supported options, check out dsl.rb.
def self.new(adapter, options = {})
DSL.new(adapter, options)
end
# 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.registry(:admins) { |thing|
# thing.respond_to?(:admin?) && thing.admin?
# }
#
# Returns a Flipper::Group.
# Raises Flipper::DuplicateGroup if the group is already registered.
def self.register(name, &block)
group = Types::Group.new(name, &block)
groups.add(group.name, group)
group
rescue Registry::DuplicateKey
raise DuplicateGroup, %Q{Group #{name.inspect} has already been registered}
end
# Public: Clears the group registry.
#
# Returns nothing.
def self.unregister_groups
groups.clear
end
# Internal: Fetches a group by name.
#
# name - The Symbol name of the group.
#
# Examples
#
# Flipper.group(:admins)
#
# Returns the Flipper::Group if group registered.
# Raises Flipper::GroupNotRegistered if group is not registered.
def self.group(name)
groups.get(name)
rescue Flipper::Registry::KeyNotFound => e
raise GroupNotRegistered, "Group #{e.key.inspect} has not been registered"
end
# Internal: Registry of all groups.
def self.groups
@groups ||= Registry.new
end
# Internal: Change the groups registry.
def self.groups=(registry)
@groups = registry
end
end
require 'flipper/adapter'
require 'flipper/dsl'
require 'flipper/errors'
require 'flipper/feature'
require 'flipper/gate'
require 'flipper/registry'
require 'flipper/type'