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.



10
11
12
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 10

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’)



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

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)


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

def active?(toggle_name)
  toggle_info = get_toggle_info(toggle_name)
  return false unless 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’)



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

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’)



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

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)


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

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

#listObject



77
78
79
80
81
82
83
84
85
# File 'lib/chirrin-chirrion/database_adapters/redis_adapter.rb', line 77

def list
  toggles = redis_database.hgetall(TOGGLES_HASH_KEY)

  toggles.map do |toggle_name, toggle_info|
    toggle_details = JSON.parse(toggle_info).merge('name' => toggle_name)

    OpenStruct.new(toggle_details)
  end
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’)



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

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

  true
end