Class: ThisFeature::Adapters::Memory

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

Instance Method Summary collapse

Constructor Details

#initialize(context_key_method: nil) ⇒ Memory

Returns a new instance of Memory.



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

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

Instance Method Details

#clearObject



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

def clear
  storage.clear
end

#control?(flag_name, **kwargs) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/this_feature/adapters/memory.rb', line 33

def control?(flag_name, **kwargs)
  !present?(flag_name)
end

#enable_treatment!(flag_name, treatment: nil, context: nil) ⇒ Object



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

def enable_treatment!(flag_name, treatment: nil, context: nil)
  return false if treatment.nil? || flag_name.nil? || context.nil?

  storage[flag_name] ||= {}
  storage[flag_name][:treatment_contexts] ||= {}
  storage[flag_name][:treatment_contexts][context_key(context)] = treatment
end

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



66
67
68
69
70
71
72
73
# File 'lib/this_feature/adapters/memory.rb', line 66

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: {}, record: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



57
58
59
60
61
62
63
64
# File 'lib/this_feature/adapters/memory.rb', line 57

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: {}, record: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  flag_data = storage[flag_name]

  context_registered = flag_data[:contexts]&.key?(context_key(context))

  return !!flag_data[:global] if !context || (context && !context_registered)

  flag_data[:contexts] ||= {}
  !!flag_data[:contexts][context_key(context)]
end

#present?(flag_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#storageObject



75
76
77
# File 'lib/this_feature/adapters/memory.rb', line 75

def storage
  @storage ||= {}
end

#treatment_value(flag_name, context: nil, data: {}, record: nil) ⇒ Object



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

def treatment_value(flag_name, context: nil, data: {}, record: nil)
  return 'control' if !present?(flag_name) || context.nil?

  flag_data = storage[flag_name][:treatment_contexts]
  context_registered = flag_data&.key?(context_key(context))

  return 'control' if !flag_data || !context_registered

  flag_data ||= {}
  flag_data[context_key(context)]
end