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.



275
276
277
278
279
280
281
# File 'lib/shipeasy/sdk/eval.rb', line 275

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.



269
270
271
# File 'lib/shipeasy/sdk/eval.rb', line 269

def group
  @group
end

#nameObject (readonly)

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



267
268
269
# File 'lib/shipeasy/sdk/eval.rb', line 267

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)


285
286
287
# File 'lib/shipeasy/sdk/eval.rb', line 285

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).



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/shipeasy/sdk/eval.rb', line 297

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