Class: Feature
- Inherits:
-
Object
show all
- Defined in:
- lib/feature/shared.rb,
lib/feature.rb,
lib/feature/gitaly.rb,
lib/feature/logger.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: ActiveSupportCacheStoreAdapter, Definition, FlipperFeature, FlipperGate, Gitaly, Logger, Target
Constant Summary
collapse
- InvalidFeatureFlagError =
rubocop:disable Lint/InheritException
Class.new(Exception)
Class Method Summary
collapse
-
.all ⇒ Object
-
.disable(key, thing = false) ⇒ 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_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
-
.logger ⇒ 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
30
31
32
|
# File 'lib/feature.rb', line 30
def all
flipper.features.to_a
end
|
.disable(key, thing = false) ⇒ Object
94
95
96
97
|
# File 'lib/feature.rb', line 94
def disable(key, thing = false)
log(key: key, action: __method__, thing: thing)
get(key).disable(thing)
end
|
.disable_percentage_of_actors(key) ⇒ Object
114
115
116
117
|
# File 'lib/feature.rb', line 114
def disable_percentage_of_actors(key)
log(key: key, action: __method__)
get(key).disable_percentage_of_actors
end
|
.disable_percentage_of_time(key) ⇒ Object
104
105
106
107
|
# File 'lib/feature.rb', line 104
def disable_percentage_of_time(key)
log(key: key, action: __method__)
get(key).disable_percentage_of_time
end
|
.disabled?(key, thing = nil, type: :development, default_enabled: false) ⇒ Boolean
84
85
86
87
|
# File 'lib/feature.rb', line 84
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
89
90
91
92
|
# File 'lib/feature.rb', line 89
def enable(key, thing = true)
log(key: key, action: __method__, thing: thing)
get(key).enable(thing)
end
|
.enable_percentage_of_actors(key, percentage) ⇒ Object
109
110
111
112
|
# File 'lib/feature.rb', line 109
def enable_percentage_of_actors(key, percentage)
log(key: key, action: __method__, percentage: percentage)
get(key).enable_percentage_of_actors(percentage)
end
|
.enable_percentage_of_time(key, percentage) ⇒ Object
99
100
101
102
|
# File 'lib/feature.rb', line 99
def enable_percentage_of_time(key, percentage)
log(key: key, action: __method__, percentage: 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/feature.rb', line 61
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
34
35
36
|
# File 'lib/feature.rb', line 34
def get(key)
flipper.feature(key)
end
|
.logger ⇒ Object
147
148
149
|
# File 'lib/feature.rb', line 147
def logger
@logger ||= Feature::Logger.build
end
|
.persisted_name?(feature_name) ⇒ Boolean
50
51
52
53
54
55
|
# File 'lib/feature.rb', line 50
def persisted_name?(feature_name)
persisted_names.include?(feature_name.to_s)
end
|
.persisted_names ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/feature.rb', line 38
def persisted_names
return [] unless Gitlab::Database.exists?
flipper.adapter.features
end
|
.register_definitions ⇒ Object
.register_feature_groups ⇒ Object
134
135
|
# File 'lib/feature.rb', line 134
def register_feature_groups
end
|
.register_hot_reloader ⇒ Object
.remove(key) ⇒ Object
119
120
121
122
123
124
|
# File 'lib/feature.rb', line 119
def remove(key)
return unless persisted_name?(key)
log(key: key, action: __method__)
get(key).remove
end
|