Class: Feature
- Inherits:
-
Object
show all
- Defined in:
- lib/feature/shared.rb,
lib/feature.rb,
lib/feature/gitaly.rb,
lib/feature/definition.rb
Overview
This file can contain only simple constructs as it is shared between:
-
`Pure Ruby`: `bin/feature-flag`
-
`GitLab Rails`: `lib/feature/definition.rb`
Defined Under Namespace
Modules: Shared
Classes: Definition, FlipperFeature, FlipperGate, Gitaly, Target
Constant Summary
collapse
- InvalidFeatureFlagError =
rubocop:disable Lint/InheritException
Class.new(Exception)
Class Method Summary
collapse
-
.all ⇒ Object
-
.disable(key, thing = false) ⇒ Object
-
.disable_group(key, group) ⇒ Object
-
.disable_percentage_of_actors(key) ⇒ Object
-
.disable_percentage_of_time(key) ⇒ Object
-
.disabled?(key, thing = nil, type: :development, default_enabled: false) ⇒ Boolean
-
.enable(key, thing = true) ⇒ Object
-
.enable_group(key, group) ⇒ Object
-
.enable_percentage_of_actors(key, percentage) ⇒ Object
-
.enable_percentage_of_time(key, percentage) ⇒ Object
-
.enabled?(key, thing = nil, type: :development, default_enabled: false) ⇒ Boolean
use `default_enabled: true` to default the flag to being `enabled` unless set explicitly.
-
.get(key) ⇒ Object
-
.persisted_name?(feature_name) ⇒ Boolean
-
.persisted_names ⇒ Object
-
.register_definitions ⇒ Object
-
.register_feature_groups ⇒ Object
This method is called from config/initializers/flipper.rb and can be used to register Flipper groups.
-
.register_hot_reloader ⇒ Object
-
.remove(key) ⇒ Object
-
.reset ⇒ Object
Class Method Details
.all ⇒ Object
26
27
28
|
# File 'lib/feature.rb', line 26
def all
flipper.features.to_a
end
|
.disable(key, thing = false) ⇒ Object
89
90
91
|
# File 'lib/feature.rb', line 89
def disable(key, thing = false)
get(key).disable(thing)
end
|
.disable_group(key, group) ⇒ Object
97
98
99
|
# File 'lib/feature.rb', line 97
def disable_group(key, group)
get(key).disable_group(group)
end
|
.disable_percentage_of_actors(key) ⇒ Object
113
114
115
|
# File 'lib/feature.rb', line 113
def disable_percentage_of_actors(key)
get(key).disable_percentage_of_actors
end
|
.disable_percentage_of_time(key) ⇒ Object
105
106
107
|
# File 'lib/feature.rb', line 105
def disable_percentage_of_time(key)
get(key).disable_percentage_of_time
end
|
.disabled?(key, thing = nil, type: :development, default_enabled: false) ⇒ Boolean
80
81
82
83
|
# File 'lib/feature.rb', line 80
def disabled?(key, thing = nil, type: :development, default_enabled: false)
thing.nil? ? !enabled?(key, type: type, default_enabled: default_enabled) : !enabled?(key, thing, type: type, default_enabled: default_enabled)
end
|
.enable(key, thing = true) ⇒ Object
85
86
87
|
# File 'lib/feature.rb', line 85
def enable(key, thing = true)
get(key).enable(thing)
end
|
.enable_group(key, group) ⇒ Object
93
94
95
|
# File 'lib/feature.rb', line 93
def enable_group(key, group)
get(key).enable_group(group)
end
|
.enable_percentage_of_actors(key, percentage) ⇒ Object
109
110
111
|
# File 'lib/feature.rb', line 109
def enable_percentage_of_actors(key, percentage)
get(key).enable_percentage_of_actors(percentage)
end
|
.enable_percentage_of_time(key, percentage) ⇒ Object
101
102
103
|
# File 'lib/feature.rb', line 101
def enable_percentage_of_time(key, percentage)
get(key).enable_percentage_of_time(percentage)
end
|
.enabled?(key, thing = nil, type: :development, default_enabled: false) ⇒ Boolean
use `default_enabled: true` to default the flag to being `enabled` unless set explicitly. The default is `disabled` TODO: remove the `default_enabled:` and read it from the `defintion_yaml` check: gitlab.com/gitlab-org/gitlab/-/issues/30228
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/feature.rb', line 57
def enabled?(key, thing = nil, type: :development, default_enabled: false)
if check_feature_flags_definition?
if thing && !thing.respond_to?(:flipper_id)
raise InvalidFeatureFlagError,
"The thing '#{thing.class.name}' for feature flag '#{key}' needs to include `FeatureGate` or implement `flipper_id`"
end
Feature::Definition.valid_usage!(key, type: type, default_enabled: default_enabled)
end
return default_enabled unless Gitlab::Database.exists?
feature = get(key)
!default_enabled || Feature.persisted_name?(feature.name) ? feature.enabled?(thing) : true
end
|
.get(key) ⇒ Object
30
31
32
|
# File 'lib/feature.rb', line 30
def get(key)
flipper.feature(key)
end
|
.persisted_name?(feature_name) ⇒ Boolean
46
47
48
49
50
51
|
# File 'lib/feature.rb', line 46
def persisted_name?(feature_name)
persisted_names.include?(feature_name.to_s)
end
|
.persisted_names ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/feature.rb', line 34
def persisted_names
return [] unless Gitlab::Database.exists?
flipper.adapter.features
end
|
.register_definitions ⇒ Object
134
135
136
137
138
|
# File 'lib/feature.rb', line 134
def register_definitions
return unless check_feature_flags_definition?
Feature::Definition.load_all!
end
|
.register_feature_groups ⇒ Object
131
132
|
# File 'lib/feature.rb', line 131
def register_feature_groups
end
|
.register_hot_reloader ⇒ Object
.remove(key) ⇒ Object
117
118
119
120
121
|
# File 'lib/feature.rb', line 117
def remove(key)
return unless persisted_name?(key)
get(key).remove
end
|