Class: SimpleFeatureFlags::RedisStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_feature_flags/redis_storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, file) ⇒ RedisStorage

Returns a new instance of RedisStorage.



9
10
11
12
13
14
15
# File 'lib/simple_feature_flags/redis_storage.rb', line 9

def initialize(redis, file)
  @file = file
  @redis = redis
  @mandatory_flags = []

  import_flags_from_file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



7
8
9
# File 'lib/simple_feature_flags/redis_storage.rb', line 7

def file
  @file
end

#mandatory_flagsObject (readonly)

Returns the value of attribute mandatory_flags.



7
8
9
# File 'lib/simple_feature_flags/redis_storage.rb', line 7

def mandatory_flags
  @mandatory_flags
end

#redisObject (readonly)

Returns the value of attribute redis.



7
8
9
# File 'lib/simple_feature_flags/redis_storage.rb', line 7

def redis
  @redis
end

Instance Method Details

#activate(feature) ⇒ Object Also known as: activate_globally



130
131
132
133
134
135
136
# File 'lib/simple_feature_flags/redis_storage.rb', line 130

def activate(feature)
  return false unless exists?(feature)

  redis.hset(feature.to_s, 'active', 'globally')

  true
end

#activate_for(feature, objects, object_id_method = CONFIG.default_id_method) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/simple_feature_flags/redis_storage.rb', line 148

def activate_for(feature, objects, object_id_method = CONFIG.default_id_method)
  return false unless exists?(feature)

  objects = [objects] unless objects.is_a? ::Array
  to_activate_hash = objects_to_hash(objects, object_id_method)
  active_objects_hash = active_objects(feature)

  to_activate_hash.each do |klass, ids|
    (active_objects_hash[klass] = ids) && next unless active_objects_hash[klass]

    active_objects_hash[klass].concat(ids).uniq!.sort!
  end

  redis.hset(feature.to_s, 'active_for_objects', active_objects_hash.to_json)

  true
end

#activate_for!(feature, objects, object_id_method = CONFIG.default_id_method) ⇒ Object



166
167
168
169
170
# File 'lib/simple_feature_flags/redis_storage.rb', line 166

def activate_for!(feature, objects, object_id_method = CONFIG.default_id_method)
  return false unless activate_for(feature, objects, object_id_method)

  activate_partially(feature)
end

#activate_partially(feature) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/simple_feature_flags/redis_storage.rb', line 140

def activate_partially(feature)
  return false unless exists?(feature)

  redis.hset(feature.to_s, 'active', 'partially')

  true
end

#active(feature) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/simple_feature_flags/redis_storage.rb', line 17

def active(feature)
  case redis.hget(feature.to_s, 'active')
  when 'globally'
    :globally
  when 'partially'
    :partially
  when 'true'
    true
  when 'false'
    false
  end
end

#active?(feature) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/simple_feature_flags/redis_storage.rb', line 30

def active?(feature)
  return true if active(feature)

  false
end

#active_for?(feature, object, object_id_method = CONFIG.default_id_method) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/simple_feature_flags/redis_storage.rb', line 56

def active_for?(feature, object, object_id_method = CONFIG.default_id_method)
  return false unless active?(feature)
  return true if active_globally?(feature)

  active_objects_hash = active_objects(feature)
  active_ids = active_objects_hash[object.class.to_s]

  return false unless active_ids

  active_ids.include? object.public_send(object_id_method)
end

#active_globally?(feature) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/simple_feature_flags/redis_storage.rb', line 40

def active_globally?(feature)
  ACTIVE_GLOBALLY.include? redis.hget(feature.to_s, 'active')
end

#active_objects(feature) ⇒ Object



189
190
191
192
193
# File 'lib/simple_feature_flags/redis_storage.rb', line 189

def active_objects(feature)
  ::JSON.parse(redis.hget(feature.to_s, 'active_for_objects').to_s)
rescue ::JSON::ParserError
  {}
end

#active_partially?(feature) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/simple_feature_flags/redis_storage.rb', line 48

def active_partially?(feature)
  ACTIVE_PARTIALLY.include? redis.hget(feature.to_s, 'active')
end

#add(feature, description, active = 'false') ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/simple_feature_flags/redis_storage.rb', line 224

def add(feature, description, active = 'false')
  return false if exists?(feature)

  active = if ACTIVE_GLOBALLY.include?(active)
             'globally'
           elsif ACTIVE_PARTIALLY.include?(active)
             'partially'
           else
             'false'
           end

  hash = {
    'name' => feature.to_s,
    'active' => active,
    'description' => description
  }

  redis.hset(feature.to_s, hash)
  hash
end

#allObject



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/simple_feature_flags/redis_storage.rb', line 254

def all
  keys = []
  hashes = []
  redis.scan_each(match: "*") do |key|
    next if keys.include?(key)

    keys << key
    hashes << get(key)
  end

  hashes
end

#deactivate(feature) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/simple_feature_flags/redis_storage.rb', line 181

def deactivate(feature)
  return false unless exists?(feature)

  redis.hset(feature.to_s, 'active', 'false')

  true
end

#deactivate!(feature) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/simple_feature_flags/redis_storage.rb', line 172

def deactivate!(feature)
  return false unless exists?(feature)

  redis.hset(feature.to_s, 'active', 'false')
  redis.hset(feature.to_s, 'active_for_objects', '')

  true
end

#deactivate_for(feature, objects, object_id_method = CONFIG.default_id_method) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/simple_feature_flags/redis_storage.rb', line 195

def deactivate_for(feature, objects, object_id_method = CONFIG.default_id_method)
  return false unless exists?(feature)

  active_objects_hash = active_objects(feature)

  objects_to_deactivate_hash = objects_to_hash(objects, object_id_method)

  objects_to_deactivate_hash.each do |klass, ids_to_remove|
    active_ids = active_objects_hash[klass]
    next unless active_ids

    active_ids.reject! { |id| ids_to_remove.include? id }
  end

  redis.hset(feature.to_s, 'active_for_objects', active_objects_hash.to_json)

  true
end

#description(feature) ⇒ Object



78
79
80
# File 'lib/simple_feature_flags/redis_storage.rb', line 78

def description(feature)
  redis.hget(feature.to_s, 'description')
end

#exists?(feature) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/simple_feature_flags/redis_storage.rb', line 72

def exists?(feature)
  return false if [nil, ''].include? redis.hget(feature.to_s, 'name')

  true
end

#get(feature) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/simple_feature_flags/redis_storage.rb', line 214

def get(feature)
  return unless exists?(feature)

  hash = redis.hgetall(feature.to_s)
  hash['mandatory'] = mandatory_flags.include?(feature.to_s)
  hash['active_for_objects'] = ::JSON.parse(hash['active_for_objects']) rescue {}

  hash
end

#inactive?(feature) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/simple_feature_flags/redis_storage.rb', line 36

def inactive?(feature)
  !active?(feature)
end

#inactive_for?(feature, object, object_id_method = CONFIG.default_id_method) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/simple_feature_flags/redis_storage.rb', line 68

def inactive_for?(feature, object, object_id_method = CONFIG.default_id_method)
  !active_for?(feature, object, object_id_method)
end

#inactive_globally?(feature) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/simple_feature_flags/redis_storage.rb', line 44

def inactive_globally?(feature)
  !active_globally?(feature)
end

#inactive_partially?(feature) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/simple_feature_flags/redis_storage.rb', line 52

def inactive_partially?(feature)
  !active_partially?(feature)
end

#namespaced_redisObject



267
268
269
# File 'lib/simple_feature_flags/redis_storage.rb', line 267

def namespaced_redis
  redis
end

#remove(feature) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/simple_feature_flags/redis_storage.rb', line 245

def remove(feature)
  return false unless exists?(feature)

  removed = get(feature)
  redis.del(feature.to_s)

  removed
end

#when_active(feature) ⇒ Object



82
83
84
85
86
# File 'lib/simple_feature_flags/redis_storage.rb', line 82

def when_active(feature)
  return unless active?(feature)

  yield
end

#when_active_for(feature, object, object_id_method = CONFIG.default_id_method) ⇒ Object



118
119
120
121
122
# File 'lib/simple_feature_flags/redis_storage.rb', line 118

def when_active_for(feature, object, object_id_method = CONFIG.default_id_method)
  return unless active_for?(feature, object, object_id_method)

  yield
end

#when_active_globally(feature) ⇒ Object



94
95
96
97
98
# File 'lib/simple_feature_flags/redis_storage.rb', line 94

def when_active_globally(feature)
  return unless active_globally?(feature)

  yield
end

#when_active_partially(feature) ⇒ Object



106
107
108
109
110
# File 'lib/simple_feature_flags/redis_storage.rb', line 106

def when_active_partially(feature)
  return unless active_partially?(feature)

  yield
end

#when_inactive(feature) ⇒ Object



88
89
90
91
92
# File 'lib/simple_feature_flags/redis_storage.rb', line 88

def when_inactive(feature)
  return unless inactive?(feature)

  yield
end

#when_inactive_for(feature, object, object_id_method = CONFIG.default_id_method) ⇒ Object



124
125
126
127
128
# File 'lib/simple_feature_flags/redis_storage.rb', line 124

def when_inactive_for(feature, object, object_id_method = CONFIG.default_id_method)
  return unless inactive_for?(feature, object, object_id_method)

  yield
end

#when_inactive_globally(feature) ⇒ Object



100
101
102
103
104
# File 'lib/simple_feature_flags/redis_storage.rb', line 100

def when_inactive_globally(feature)
  return unless inactive_globally?(feature)

  yield
end

#when_inactive_partially(feature) ⇒ Object



112
113
114
115
116
# File 'lib/simple_feature_flags/redis_storage.rb', line 112

def when_inactive_partially(feature)
  return unless inactive_partially?(feature)

  yield
end