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?, #setup

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



47
48
49
50
51
52
53
54
# File 'lib/this_feature/adapters/memory.rb', line 47

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
33
34
35
36
# File 'lib/this_feature/adapters/memory.rb', line 30

def off?(flag_name, context: nil, data: {})
  on_result = on?(flag_name, context: context)

  return if on_result.nil?

  !on_result
end

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



38
39
40
41
42
43
44
45
# File 'lib/this_feature/adapters/memory.rb', line 38

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



56
57
58
# File 'lib/this_feature/adapters/memory.rb', line 56

def storage
  @storage ||= {}
end