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

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



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

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

Instance Attribute Details

#only_assignedObject (readonly)

Returns the value of attribute only_assigned.



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

def only_assigned
  @only_assigned
end

#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



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

def freeze
  signature # finalize before freezing
  super
end

#key(key = nil) ⇒ Object



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

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)


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

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

#signatureObject



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

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

#trackable?Boolean

Returns:

  • (Boolean)


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

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
34
# 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
  @only_assigned = value.delete(:only_assigned)
  reinitialize(value.delete(:request))
  key(value.delete(:sticky_to))

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