Class: ThisFeature::Adapters::Memory

Inherits:
Base
  • Object
show all
Defined in:
lib/this_feature/adapters/memory.rb

Instance Method Summary collapse

Methods inherited from Base

#control?

Constructor Details

#initialize(context_key_method: nil) ⇒ Memory

Returns a new instance of Memory.



5
6
7
# File 'lib/this_feature/adapters/memory.rb', line 5

def initialize(context_key_method: nil)
  @context_key_method = context_key_method
end

Instance Method Details

#clearObject



9
10
11
# File 'lib/this_feature/adapters/memory.rb', line 9

def clear
  storage.clear
end

#off!(flag_name, context: nil, data: {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/this_feature/adapters/memory.rb', line 43

def off!(flag_name, context: nil, data: {})
  storage[flag_name] ||= {}

  return storage[flag_name][:global] = false if context.nil?

  storage[flag_name][:contexts] ||= {}
  storage[flag_name][:contexts][context_key(context)] = false
end

#off?(flag_name, context: nil, data: {}) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/this_feature/adapters/memory.rb', line 30

def off?(flag_name, context: nil, data: {})
  !on?(flag_name, context: context, data: data)
end

#on!(flag_name, context: nil, data: {}) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/this_feature/adapters/memory.rb', line 34

def on!(flag_name, context: nil, data: {})
  storage[flag_name] ||= {}

  return storage[flag_name][:global] = true if context.nil?

  storage[flag_name][:contexts] ||= {}
  storage[flag_name][:contexts][context_key(context)] = true
end

#on?(flag_name, context: nil, data: {}) ⇒ Boolean

Returns:

  • (Boolean)


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

def on?(flag_name, context: nil, data: {})
  return false unless present?(flag_name)

  flag_data = storage[flag_name]

  return true if flag_data[:global]
  return false if context.nil?

  flag_data[:contexts] ||= {}

  !!flag_data[:contexts][context_key(context)]
end

#present?(flag_name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/this_feature/adapters/memory.rb', line 13

def present?(flag_name)
  !storage[flag_name].nil?
end

#storageObject



52
53
54
# File 'lib/this_feature/adapters/memory.rb', line 52

def storage
  @storage ||= {}
end