File: dsl.rb

package info (click to toggle)
ruby-gitlab-experiment 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,202; makefile: 7
file content (25 lines) | stat: -rw-r--r-- 729 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

module Gitlab
  class Experiment
    module Dsl
      def self.include_in(klass, with_helper: false)
        klass.include(self).tap { |base| base.helper_method(:experiment) if with_helper }
      end

      def experiment(name, variant_name = nil, **context, &block)
        raise ArgumentError, 'name is required' if name.nil?

        context[:request] ||= request if respond_to?(:request)

        base = Configuration.base_class.constantize
        klass = base.constantize(name) || base

        instance = klass.new(name, variant_name, **context, &block)
        return instance unless block

        instance.context.frozen? ? instance.run : instance.tap(&:run)
      end
    end
  end
end