Sha256: b4e13b288c6100a4438b9a54d6fdee2795636aa98d87a57b6af3fa5651689c13

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

class TestTrack::ABConfiguration
  include TestTrack::RequiredOptions

  def initialize(opts)
    @split_name = require_option!(opts, :split_name).to_s
    true_variant = require_option!(opts, :true_variant, allow_nil: true)
    @split_registry = require_option!(opts, :split_registry, allow_nil: true)
    raise ArgumentError, "unknown opts: #{opts.keys.to_sentence}" if opts.present?

    @true_variant = true_variant.to_s if true_variant

    raise ArgumentError, "unknown split: #{split_name}" if @split_registry && !split
  end

  def variants
    @variants ||= build_variant_hash
  end

  private

  def build_variant_hash
    airbrake_because_ab("configures split with more than 2 variants") if split_variants && split_variants.size > 2
    { true: true_variant, false: false_variant }
  end

  def true_variant
    @true_variant ||= true
  end

  def false_variant
    @false_variant ||= non_true_variants.present? ? non_true_variants.sort.first : false
  end

  attr_reader :split_name, :split_registry

  def split
    split_registry && split_registry[split_name]
  end

  def split_variants
    @split_variants ||= split.keys if split_registry
  end

  def non_true_variants
    split_variants - [true_variant.to_s] if split_variants
  end

  def airbrake_because_ab(msg)
    msg = "A/B for \"#{split_name}\" #{msg}"
    Rails.logger.error(msg)
    Airbrake.notify_or_ignore(msg)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
test_track_rails_client-0.9.6 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.5 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.4 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.3 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.2 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.1 app/models/test_track/ab_configuration.rb
test_track_rails_client-0.9.0 app/models/test_track/ab_configuration.rb