Class: Verdict::Storage::BaseStorage

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

Instance Method Summary collapse

Instance Method Details

#cleanup(experiment_or_scope, options = {}) ⇒ Object

Deletes all assignments (and any other stored data) for the given experiment



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/verdict/storage/base_storage.rb', line 49

def cleanup(experiment_or_scope, options = {})
  if experiment_or_scope.is_a?(Symbol) || experiment_or_scope.is_a?(String)
    Verdict.default_logger.warn(
      "Passing a scope string/symbol to #{self.class}#cleanup is deprecated, " \
      'pass a Verdict::Experiment instance instead.'
    )
    clear(experiment_or_scope, options)
  else
    clear(experiment_or_scope.handle.to_s, options)
  end
end

#remove_assignment(experiment, subject) ⇒ Object

Should remove the subject from storage, so it will be reassigned later.



31
32
33
34
# File 'lib/verdict/storage/base_storage.rb', line 31

def remove_assignment(experiment, subject)
  subject_identifier = experiment.retrieve_subject_identifier(subject)
  remove(experiment.handle.to_s, "assignment_#{subject_identifier}")
end

#retrieve_assignment(experiment, subject) ⇒ Object

Should do a fast lookup of an assignment of the subject for the given experiment.

  • Should return nil if not found in store

  • Should return an Assignment instance otherwise.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/verdict/storage/base_storage.rb', line 18

def retrieve_assignment(experiment, subject)
  subject_identifier = experiment.retrieve_subject_identifier(subject)
  if value = get(experiment.handle.to_s, "assignment_#{subject_identifier}")
    hash = JSON.parse(value)
    experiment.subject_assignment(
      subject,
      experiment.group(hash['group']),
      Time.xmlschema(hash['created_at'])
    )
  end
end

#retrieve_start_timestamp(experiment) ⇒ Object

Retrieves the start timestamp of the experiment



37
38
39
40
41
# File 'lib/verdict/storage/base_storage.rb', line 37

def retrieve_start_timestamp(experiment)
  if timestamp = get(experiment.handle.to_s, 'started_at')
    Time.parse(timestamp)
  end
end

#store_assignment(assignment) ⇒ Object

Should store the assignments to allow quick lookups.

  • Assignments should be unique on the combination of ‘assignment.experiment.handle` and `assignment.subject_identifier`.

  • The main property to store is ‘group.handle`

  • Should return true if stored successfully.



10
11
12
13
# File 'lib/verdict/storage/base_storage.rb', line 10

def store_assignment(assignment)
  hash = { group: assignment.handle, created_at: assignment.created_at.strftime('%FT%TZ') }
  set(assignment.experiment.handle.to_s, "assignment_#{assignment.subject_identifier}", JSON.dump(hash))
end

#store_start_timestamp(experiment, timestamp) ⇒ Object

Stores the timestamp on which the experiment was started



44
45
46
# File 'lib/verdict/storage/base_storage.rb', line 44

def store_start_timestamp(experiment, timestamp)
  set(experiment.handle.to_s, 'started_at', timestamp.utc.strftime('%FT%TZ'))
end