Class: Downgrade::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/downgrade/switch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ Switch

Returns a new instance of Switch.



5
6
7
8
# File 'lib/downgrade/switch.rb', line 5

def initialize(scope)
  @scope = scope
  @cache_second = 5
end

Instance Attribute Details

#cache_secondObject

Returns the value of attribute cache_second.



3
4
5
# File 'lib/downgrade/switch.rb', line 3

def cache_second
  @cache_second
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/downgrade/switch.rb', line 3

def scope
  @scope
end

Class Method Details

.cache_storeObject



31
32
33
# File 'lib/downgrade/switch.rb', line 31

def cache_store
  @cache_store ||= ActiveSupport::Cache::MemoryStore.new
end

Instance Method Details

#is_on?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/downgrade/switch.rb', line 18

def is_on?
  Downgrade::Switch.cache_store.fetch(cache_key, expires_in: self.cache_second) do
    Downgrade.redis.get(cache_key).present?
  end
end

#turn_offObject



14
15
16
# File 'lib/downgrade/switch.rb', line 14

def turn_off
  Downgrade.redis.del(cache_key)
end

#turn_on(ttl = 600) ⇒ Object



10
11
12
# File 'lib/downgrade/switch.rb', line 10

def turn_on(ttl = 600)
  Downgrade.redis.set(cache_key, 1, ex: ttl)
end