Class: Shipeasy::SDK::Eval::Assignment

Inherits:
Object
  • Object
show all
Defined in:
lib/shipeasy/sdk/eval.rb

Overview

The result of universe(name).assign(user) — a unit's standing in a universe (a mutual-exclusion pool, so it lands in at most one experiment). Never raises: an un-enrolled unit still resolves get to the universe defaults (or the caller's fallback). Reading is side-effect free — the single exposure is logged once by assign() when the unit is enrolled.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, group, params, on_expose = nil) ⇒ Assignment

params is already merged (universeDefaults ⊕ variantOverride) when enrolled; defaults-only (or {}) when not. on_expose fires the single exposure the first time an enrolled param is read (nil when not enrolled — nothing to expose); deduped downstream.



367
368
369
370
371
372
373
# File 'lib/shipeasy/sdk/eval.rb', line 367

def initialize(name, group, params, on_expose = nil)
  @name      = name
  @group     = group
  @params    = params || {}
  @on_expose = on_expose
  @exposed   = false
end

Instance Attribute Details

#groupObject (readonly)

The assigned variant/group name, or nil when not enrolled.



361
362
363
# File 'lib/shipeasy/sdk/eval.rb', line 361

def group
  @group
end

#nameObject (readonly)

The experiment the unit landed in, or nil when not enrolled.



359
360
361
# File 'lib/shipeasy/sdk/eval.rb', line 359

def name
  @name
end

Instance Method Details

#enrolled?Boolean

True iff the unit is enrolled in an experiment in this universe. Reading it does NOT log an exposure (only get of a param does).

Returns:

  • (Boolean)


377
378
379
# File 'lib/shipeasy/sdk/eval.rb', line 377

def enrolled?
  !@group.nil?
end

#get(field, fallback = nil, exposure: true) ⇒ Object

Read a resolved param: the assigned variant's override, else the universe default, else fallback. Works even when not enrolled (the variant layer is absent, so you get universeDefault ?? fallback). Looks up both string and symbol keys.

Exposure is logged on read (spec step 7): the first enrolled read fires the single exposure; pass exposure: false to read without logging (peek).



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/shipeasy/sdk/eval.rb', line 389

def get(field, fallback = nil, exposure: true)
  if exposure && !@exposed && @on_expose
    @exposed = true
    @on_expose.call
  end
  if @params.key?(field)
    @params[field]
  elsif field.respond_to?(:to_s) && @params.key?(field.to_s)
    @params[field.to_s]
  elsif field.respond_to?(:to_sym) && @params.key?(field.to_sym)
    @params[field.to_sym]
  else
    fallback
  end
end