Class: Verdict::Assignment

Inherits:
Object
  • Object
show all
Defined in:
lib/verdict/assignment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiment, subject_identifier, group, originally_created_at, temporary = false) ⇒ Assignment

Returns a new instance of Assignment.



5
6
7
8
9
10
11
12
# File 'lib/verdict/assignment.rb', line 5

def initialize(experiment, subject_identifier, group, originally_created_at, temporary = false)
  @experiment         = experiment
  @subject_identifier = subject_identifier
  @group              = group
  @returning          = !originally_created_at.nil?
  @created_at         = originally_created_at || Time.now.utc
  @temporary          = temporary
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/verdict/assignment.rb', line 3

def created_at
  @created_at
end

#experimentObject (readonly)

Returns the value of attribute experiment.



3
4
5
# File 'lib/verdict/assignment.rb', line 3

def experiment
  @experiment
end

#groupObject (readonly)

Returns the value of attribute group.



3
4
5
# File 'lib/verdict/assignment.rb', line 3

def group
  @group
end

#subject_identifierObject (readonly)

Returns the value of attribute subject_identifier.



3
4
5
# File 'lib/verdict/assignment.rb', line 3

def subject_identifier
  @subject_identifier
end

Instance Method Details

#===(other) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/verdict/assignment.rb', line 61

def ===(other)
  case other
    when nil; !qualified?
    when Verdict::Assignment; other.group === group
    when Verdict::Group; other === group
    when Symbol, String; qualified? ? group.handle == other.to_s : false
    else false
  end
end

#as_json(options = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/verdict/assignment.rb', line 46

def as_json(options = {})
  {
    experiment: experiment.handle,
    subject:    subject_identifier,
    qualified:  qualified?,
    returning:  returning?,
    group:      qualified? ? group.handle : nil,
    created_at: created_at.utc.strftime('%FT%TZ')
  }
end

#handleObject



38
39
40
# File 'lib/verdict/assignment.rb', line 38

def handle
  qualified? ? group.handle : nil
end

#permanent?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/verdict/assignment.rb', line 22

def permanent?
  !@temporary
end

#qualified?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/verdict/assignment.rb', line 18

def qualified?
  !group.nil?
end

#returningObject



30
31
32
# File 'lib/verdict/assignment.rb', line 30

def returning
  self.class.new(@experiment, @subject_identifier, @group, @created_at)
end

#returning?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/verdict/assignment.rb', line 34

def returning?
  @returning
end

#subjectObject



14
15
16
# File 'lib/verdict/assignment.rb', line 14

def subject
  @subject ||= experiment.fetch_subject(subject_identifier)
end

#temporary?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/verdict/assignment.rb', line 26

def temporary?
  @temporary
end

#to_json(options = {}) ⇒ Object



57
58
59
# File 'lib/verdict/assignment.rb', line 57

def to_json(options = {})
  as_json(options).to_json
end

#to_symObject



42
43
44
# File 'lib/verdict/assignment.rb', line 42

def to_sym
  qualified? ? group.to_sym : nil
end