Class: Gitlab::Experiment::Context

Inherits:
Object
  • Object
show all
Includes:
Cookies
Defined in:
lib/gitlab/experiment/context.rb

Constant Summary collapse

DNT_REGEXP =
/^(true|t|yes|y|1|on)$/i.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiment, **initial_value) ⇒ Context

Returns a new instance of Context.



12
13
14
15
16
17
18
# File 'lib/gitlab/experiment/context.rb', line 12

def initialize(experiment, **initial_value)
  @experiment = experiment
  @value = {}
  @migrations = { merged: [], unmerged: [] }

  value(initial_value)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



54
55
56
# File 'lib/gitlab/experiment/context.rb', line 54

def method_missing(method_name, *)
  @value.include?(method_name.to_sym) ? @value[method_name.to_sym] : super
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



10
11
12
# File 'lib/gitlab/experiment/context.rb', line 10

def request
  @request
end

Instance Method Details

#freezeObject



45
46
47
48
# File 'lib/gitlab/experiment/context.rb', line 45

def freeze
  signature # finalize before freezing
  super
end

#key(key = nil) ⇒ Object



35
36
37
38
39
# File 'lib/gitlab/experiment/context.rb', line 35

def key(key = nil)
  return @key || @experiment.key_for(value) if key.nil?

  @key = @experiment.key_for(key)
end

#reinitialize(request) ⇒ Object



20
21
22
23
# File 'lib/gitlab/experiment/context.rb', line 20

def reinitialize(request)
  @signature = nil # clear memoization
  @request = request if request.respond_to?(:headers) && request.respond_to?(:cookie_jar)
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/gitlab/experiment/context.rb', line 58

def respond_to_missing?(method_name, *)
  @value.include?(method_name.to_sym) ? true : super
end

#signatureObject



50
51
52
# File 'lib/gitlab/experiment/context.rb', line 50

def signature
  @signature ||= { key: key, migration_keys: migration_keys }.compact
end

#trackable?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gitlab/experiment/context.rb', line 41

def trackable?
  !(@request && @request.headers['DNT'].to_s.match?(DNT_REGEXP))
end

#value(value = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/gitlab/experiment/context.rb', line 25

def value(value = nil)
  return @value if value.nil?

  value = value.dup # dup so we don't mutate
  reinitialize(value.delete(:request))
  key(value.delete(:sticky_to))

  @value.merge!(process_migrations(value))
end