Class: ChirrinChirrion::DatabaseAdapters::RedisAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/chirrin-chirrion/database_adapters/redis_adapter.rb

Constant Summary collapse

TOGGLES_HASH_KEY =
'chirrin-chirrion-toggles'

Instance Method Summary collapse

Constructor Details

#initialize(redis_database) ⇒ RedisAdapter

Returns a new instance of RedisAdapter.


8
9
10
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 8

def initialize(redis_database)
  @redis_database = redis_database
end

Instance Method Details

#activate!(toggle_name) ⇒ Object

Makes a toggle, existent or not, active:

redis_adapter.activate!(‘my_feature’)


40
41
42
43
44
45
46
47
48
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 40

def activate!(toggle_name)
  toggle_info = get_toggle_info(toggle_name)
  raise ChirrinChirrion::Errors::ToggleNotFound, "The toggle #{toggle_name} was not found" unless toggle_info
  toggle_info['active'] = true

  redis_database.hset(TOGGLES_HASH_KEY, toggle_name, toggle_info.to_json)

  true
end

#active?(toggle_name) ⇒ Boolean

Returns:

  • (Boolean)

64
65
66
67
68
69
70
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 64

def active?(toggle_name)
  toggle_info = redis_database.hget(TOGGLES_HASH_KEY, toggle_name)
  return false unless toggle_info
  toggle_info = JSON.parse(toggle_info)

  toggle_info['active'].eql?(true)
end

#add_toggle(toggle_name, toggle_info = {}) ⇒ Object

Adds a toggle to the database:

redis_adapter.add_toggle(‘my_active_feature’, true, description: ‘What other people must know to understand what this toggle activates’) redis_adapter.add_toggle(‘my_inactive_feature’, ‘What other people must know to understand what this toggle activates’) redis_adapter.add_toggle(‘my_inactive_feature’)


18
19
20
21
22
23
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 18

def add_toggle(toggle_name, toggle_info = {})
  toggle_info[:active] ||= false
  redis_database.hset(TOGGLES_HASH_KEY, toggle_name, toggle_info.to_json)

  true
end

#inactivate!(toggle_name) ⇒ Object

Makes a toggle, existent or not, iactive:

redis_adapter.inactivate!(‘my_feature’)


54
55
56
57
58
59
60
61
62
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 54

def inactivate!(toggle_name)
  toggle_info = get_toggle_info(toggle_name)
  raise ChirrinChirrion::Errors::ToggleNotFound, "The toggle #{toggle_name} was not found" unless toggle_info
  toggle_info['active'] = false

  redis_database.hset(TOGGLES_HASH_KEY, toggle_name, toggle_info.to_json)

  true
end

#inactive?(toggle_name) ⇒ Boolean

Returns:

  • (Boolean)

72
73
74
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 72

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

#remove_toggle(toggle_name) ⇒ Object

Removes a toggle from the database:

redis_adapter.remove_toggle(‘my_active_feature’) redis_adapter.remove_toggle(‘my_inactive_feature’)


30
31
32
33
34
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 30

def remove_toggle(toggle_name)
  redis_database.hdel(TOGGLES_HASH_KEY, toggle_name)

  true
end