forked from flippercloud/flipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsl.rb
277 lines (245 loc) · 7.99 KB
/
dsl.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
require 'forwardable'
module Flipper
class DSL
extend Forwardable
# Private
attr_reader :adapter
# Private: What is being used to instrument all the things.
attr_reader :instrumenter
def_delegators :@adapter, :memoize=, :memoizing?
# Public: Returns a new instance of the DSL.
#
# adapter - The adapter that this DSL instance should use.
# options - The Hash of options.
# :instrumenter - What should be used to instrument all the things.
def initialize(adapter, options = {})
@instrumenter = options.fetch(:instrumenter, Instrumenters::Noop)
memoized = Adapters::Memoizable.new(adapter)
@adapter = memoized
@memoized_features = {}
end
# Public: Check if a feature is enabled.
#
# name - The String or Symbol name of the feature.
# args - The args passed through to the enabled check.
#
# Returns true if feature is enabled, false if not.
def enabled?(name, *args)
feature(name).enabled?(*args)
end
# Public: Enable a feature.
#
# name - The String or Symbol name of the feature.
# args - The args passed through to the feature instance enable call.
#
# Returns the result of the feature instance enable call.
def enable(name, *args)
feature(name).enable(*args)
end
# Public: Enable a feature for an actor.
#
# name - The String or Symbol name of the feature.
# actor - a Flipper::Types::Actor instance or an object that responds
# to flipper_id.
#
# Returns result of Feature#enable.
def enable_actor(name, actor)
feature(name).enable_actor(actor)
end
# Public: Enable a feature for a group.
#
# name - The String or Symbol name of the feature.
# group - a Flipper::Types::Group instance or a String or Symbol name of a
# registered group.
#
# Returns result of Feature#enable.
def enable_group(name, group)
feature(name).enable_group(group)
end
# Public: Enable a feature a percentage of time.
#
# name - The String or Symbol name of the feature.
# percentage - a Flipper::Types::PercentageOfTime instance or an object
# that responds to to_i.
#
# Returns result of Feature#enable.
def enable_percentage_of_time(name, percentage)
feature(name).enable_percentage_of_time(percentage)
end
# Public: Enable a feature for a percentage of actors.
#
# name - The String or Symbol name of the feature.
# percentage - a Flipper::Types::PercentageOfActors instance or an object
# that responds to to_i.
#
# Returns result of Feature#enable.
def enable_percentage_of_actors(name, percentage)
feature(name).enable_percentage_of_actors(percentage)
end
# Public: Disable a feature.
#
# name - The String or Symbol name of the feature.
# args - The args passed through to the feature instance enable call.
#
# Returns the result of the feature instance disable call.
def disable(name, *args)
feature(name).disable(*args)
end
# Public: Disable a feature for an actor.
#
# name - The String or Symbol name of the feature.
# actor - a Flipper::Types::Actor instance or an object that responds
# to flipper_id.
#
# Returns result of disable.
def disable_actor(name, actor)
feature(name).disable_actor(actor)
end
# Public: Disable a feature for a group.
#
# name - The String or Symbol name of the feature.
# group - a Flipper::Types::Group instance or a String or Symbol name of a
# registered group.
#
# Returns result of disable.
def disable_group(name, group)
feature(name).disable_group(group)
end
# Public: Disable a feature a percentage of time.
#
# name - The String or Symbol name of the feature.
# percentage - a Flipper::Types::PercentageOfTime instance or an object
# that responds to to_i.
#
# Returns result of disable.
def disable_percentage_of_time(name)
feature(name).disable_percentage_of_time
end
# Public: Disable a feature for a percentage of actors.
#
# name - The String or Symbol name of the feature.
# percentage - a Flipper::Types::PercentageOfActors instance or an object
# that responds to to_i.
#
# Returns result of disable.
def disable_percentage_of_actors(name)
feature(name).disable_percentage_of_actors
end
# Public: Add a feature.
#
# name - The String or Symbol name of the feature.
#
# Returns result of add.
def add(name)
feature(name).add
end
# Public: Has a feature been added in the adapter.
#
# name - The String or Symbol name of the feature.
#
# Returns true if added else false.
def exist?(name)
feature(name).exist?
end
# Public: Remove a feature.
#
# name - The String or Symbol name of the feature.
#
# Returns result of remove.
def remove(name)
feature(name).remove
end
# Public: Access a feature instance by name.
#
# name - The String or Symbol name of the feature.
#
# Returns an instance of Flipper::Feature.
def feature(name)
if !name.is_a?(String) && !name.is_a?(Symbol)
raise ArgumentError, "#{name} must be a String or Symbol"
end
@memoized_features[name.to_sym] ||= Feature.new(name, @adapter, instrumenter: instrumenter)
end
# Public: Preload the features with the given names.
#
# names - An Array of String or Symbol names of the features.
#
# Returns an Array of Flipper::Feature.
def preload(names)
features = names.map { |name| feature(name) }
@adapter.get_multi(features)
features
end
# Public: Preload all the adapters features.
#
# Returns an Array of Flipper::Feature.
def preload_all
keys = @adapter.get_all.keys
keys.map { |key| feature(key) }
end
# Public: Shortcut access to a feature instance by name.
#
# name - The String or Symbol name of the feature.
#
# Returns an instance of Flipper::Feature.
alias_method :[], :feature
# Public: Shortcut for getting a boolean type instance.
#
# value - The true or false value for the boolean.
#
# Returns a Flipper::Types::Boolean instance.
def boolean(value = true)
Types::Boolean.new(value)
end
# Public: Even shorter shortcut for getting a boolean type instance.
#
# value - The true or false value for the boolean.
#
# Returns a Flipper::Types::Boolean instance.
alias_method :bool, :boolean
# Public: Access a flipper group by name.
#
# name - The String or Symbol name of the feature.
#
# Returns an instance of Flipper::Group.
def group(name)
Flipper.group(name)
end
# Public: Wraps an object as a flipper actor.
#
# thing - The object that you would like to wrap.
#
# Returns an instance of Flipper::Types::Actor.
# Raises ArgumentError if thing does not respond to `flipper_id`.
def actor(thing)
Types::Actor.new(thing)
end
# Public: Shortcut for getting a percentage of time instance.
#
# number - The percentage of time that should be enabled.
#
# Returns Flipper::Types::PercentageOfTime.
def time(number)
Types::PercentageOfTime.new(number)
end
alias_method :percentage_of_time, :time
# Public: Shortcut for getting a percentage of actors instance.
#
# number - The percentage of actors that should be enabled.
#
# Returns Flipper::Types::PercentageOfActors.
def actors(number)
Types::PercentageOfActors.new(number)
end
alias_method :percentage_of_actors, :actors
# Public: Returns a Set of the known features for this adapter.
#
# Returns Set of Flipper::Feature instances.
def features
adapter.features.map { |name| feature(name) }.to_set
end
def import(flipper)
adapter.import(flipper.adapter)
end
end
end