Module: Gitlab::Experiment::BaseInterface

Extended by:
ActiveSupport::Concern
Included in:
Gitlab::Experiment
Defined in:
lib/gitlab/experiment/base_interface.rb

Instance Method Summary collapse

Instance Method Details

#behaviorsObject



85
86
87
# File 'lib/gitlab/experiment/base_interface.rb', line 85

def behaviors
  @_behaviors ||= public_behaviors_with_deprecations(registered_behavior_callbacks)
end

#flipper_idObject

Deprecated.


124
125
126
127
# File 'lib/gitlab/experiment/base_interface.rb', line 124

def flipper_id
  Configuration.deprecated(:flipper_id, 'instead use `id` or use a custom rollout strategy', version: '0.7.0')
  "Experiment;#{id}"
end

#idObject Also known as: to_param



75
76
77
# File 'lib/gitlab/experiment/base_interface.rb', line 75

def id
  "#{name}:#{context.key}"
end

#initialize(name = nil, variant_name = nil, **context) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/experiment/base_interface.rb', line 48

def initialize(name = nil, variant_name = nil, **context)
  raise ArgumentError, 'name is required' if name.blank? && self.class.base?

  @_name = self.class.experiment_name(name, suffix: false)
  @_context = Context.new(self, **context)
  @_assigned_variant_name = cache_variant(variant_name) { nil } if variant_name.present?

  yield self if block_given?
end

#inspectObject



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

def inspect
  "#<#{self.class.name || 'AnonymousClass'}:#{format('0x%016X', __id__)} name=#{name} context=#{context.value}>"
end

#public_behaviors_with_deprecations(behaviors) ⇒ Object

Deprecated.


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/gitlab/experiment/base_interface.rb', line 90

def public_behaviors_with_deprecations(behaviors)
  named_variants = %w[control candidate]
  public_methods.each_with_object(behaviors) do |name, behaviors|
    name = name.to_s # fixes compatibility for ruby 2.6.x
    next unless name.end_with?('_behavior')

    behavior_name = name.sub(/_behavior$/, '')
    registration = named_variants.include?(behavior_name) ? behavior_name : "variant :#{behavior_name}"

    Configuration.deprecated("      using a public `\#{name}` method is deprecated and will be removed from {{release}}, instead register variants using:\n\n      class \#{self.class.name} < \#{Configuration.base_class}\n        \#{registration}\n\n        private\n\n        def \#{name}\n          #...\n        end\n      end\n    MESSAGE\n\n    behaviors[behavior_name] ||= -> { send(name) } # rubocop:disable GitlabSecurity/PublicSend\n  end\nend\n", version: '0.7.0', stack: 2)

#run(variant_name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab/experiment/base_interface.rb', line 62

def run(variant_name)
  behaviors.freeze
  context.freeze

  block = behaviors[variant_name]
  raise BehaviorMissingError, "the `#{variant_name}` variant hasn't been registered" if block.nil?

  result = block.call
  publish(result) if enabled?

  result
end

#session_idObject

Deprecated.


118
119
120
121
# File 'lib/gitlab/experiment/base_interface.rb', line 118

def session_id
  Configuration.deprecated(:session_id, 'instead use `id` or use a custom rollout strategy', version: '0.7.0')
  id
end

#try(name = nil, &block) ⇒ Object

Deprecated.


137
138
139
140
141
142
143
144
145
# File 'lib/gitlab/experiment/base_interface.rb', line 137

def try(name = nil, &block)
  if name.present?
    Configuration.deprecated(:try, "instead use `variant(:#{name})`", version: '0.7.0')
    variant(name, &block)
  else
    Configuration.deprecated(:try, 'instead use `candidate`', version: '0.7.0')
    candidate(&block)
  end
end

#use(&block) ⇒ Object

Deprecated.


130
131
132
133
134
# File 'lib/gitlab/experiment/base_interface.rb', line 130

def use(&block)
  Configuration.deprecated(:use, 'instead use `control`', version: '0.7.0')

  control(&block)
end

#variant_namesObject



81
82
83
# File 'lib/gitlab/experiment/base_interface.rb', line 81

def variant_names
  @_variant_names ||= behaviors.keys.map(&:to_sym) - [:control]
end