Class: BarsoomUtils::FeatureToggle

Inherits:
Object
  • Object
show all
Defined in:
lib/barsoom_utils/feature_toggle.rb

Constant Summary collapse

REDIS_KEY =

We (counter-intuitively, perhaps) store disabled toggles, so we can assume them to be enabled by default in dev, staging and tests.

"disabled_feature_toggles"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listObject



34
35
36
# File 'lib/barsoom_utils/feature_toggle.rb', line 34

def self.list
  redis.smembers(REDIS_KEY).sort
end

.off?(feature, controller_or_view = nil, redis: self.redis) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/barsoom_utils/feature_toggle.rb', line 22

def self.off?(feature, controller_or_view = nil, redis: self.redis)
  new(feature, controller_or_view: controller_or_view, redis: redis).off?
end

.on?(feature, controller_or_view = nil, redis: self.redis) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/barsoom_utils/feature_toggle.rb', line 18

def self.on?(feature, controller_or_view = nil, redis: self.redis)
  new(feature, controller_or_view: controller_or_view, redis: redis).on?
end

.redisObject



14
15
16
# File 'lib/barsoom_utils/feature_toggle.rb', line 14

def self.redis
  @redis || $redis
end

.redis=(redis) ⇒ Object



10
11
12
# File 'lib/barsoom_utils/feature_toggle.rb', line 10

def self.redis=(redis)
  @redis = redis
end

.turn_off(feature, redis: self.redis) ⇒ Object



30
31
32
# File 'lib/barsoom_utils/feature_toggle.rb', line 30

def self.turn_off(feature, redis: self.redis)
  new(feature, redis: redis).turn_off
end

.turn_on(feature, redis: self.redis) ⇒ Object



26
27
28
# File 'lib/barsoom_utils/feature_toggle.rb', line 26

def self.turn_on(feature, redis: self.redis)
  new(feature, redis: redis).turn_on
end

Instance Method Details

#off?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/barsoom_utils/feature_toggle.rb', line 48

def off?
  not on?
end

#on?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/barsoom_utils/feature_toggle.rb', line 40

def on?
  if has_param_override?
    on_according_to_param?
  else
    on_according_to_redis?
  end
end

#turn_offObject



52
53
54
55
56
57
58
# File 'lib/barsoom_utils/feature_toggle.rb', line 52

def turn_off
  if redis.respond_to?(:sadd?)
    redis.sadd? REDIS_KEY, feature_name
  else
    redis.sadd REDIS_KEY, feature_name
  end
end

#turn_onObject



60
61
62
63
64
65
66
# File 'lib/barsoom_utils/feature_toggle.rb', line 60

def turn_on
  if redis.respond_to?(:srem?)
    redis.srem? REDIS_KEY, feature_name
  else
    redis.srem REDIS_KEY, feature_name
  end
end