class Segmentor::Session

Session is a single segmentation session. It can be draft, in progress, or active. at any point, there is only 1 active session per segment is allowed while draft and in progress, there can be multiple sessions in progress is when a source is still generating targets and they are being saved. draft is when a session has the targets, but they are not the ones used for notification. This can be used to test a new source code for example.

Constants

ACTIVE
ARCHIVED
DRAFT
FAILED
IN_PROGRESS

Public Instance Methods

mark_as_active!() click to toggle source
# File lib/segmentor/session.rb, line 36
def mark_as_active!
  # only draft sessions can be made active
  raise ::Segmentor::Errors::SessionStateError, 'only draft sessions can be made active' unless status == DRAFT

  self.status = ACTIVE
  save!
end
mark_as_archived!() click to toggle source
# File lib/segmentor/session.rb, line 55
def mark_as_archived!
  # only active and failed sessions can be archived
  unless status == ACTIVE || status == FAILED
    raise ::Segmentor::Errors::SessionStateError, 'only active and failed sessions can be archived'
  end

  self.status = ARCHIVED
  save!
end
mark_as_draft!() click to toggle source
# File lib/segmentor/session.rb, line 44
def mark_as_draft!
  # archived and failed sessions cannot be turned into draft
  if status == ARCHIVED || status == FAILED
    raise ::Segmentor::Errors::SessionStateError, 'failed and archived sessions cannot be turned into draft'
  end

  self.status = DRAFT
  self.reason = nil
  save!
end
mark_as_failed!(reason) click to toggle source
# File lib/segmentor/session.rb, line 65
def mark_as_failed!(reason)
  # archived sessions cannot be marked as failed
  raise ::Segmentor::Errors::SessionStateError, 'archived sessions cannot be marked as failed' if status == ARCHIVED

  self.status = FAILED
  self.reason = reason
  save!
end

Private Instance Methods

call_segment_callbacks() click to toggle source
# File lib/segmentor/session.rb, line 92
def call_segment_callbacks
  segment.send(segment.class._after_session_change, self) if segment.class._after_session_change.present?
end
failed?() click to toggle source
# File lib/segmentor/session.rb, line 88
def failed?
  status == FAILED
end
generate_session_id() click to toggle source
# File lib/segmentor/session.rb, line 83
def generate_session_id
  # generate a session id
  self.session_id ||= SecureRandom.uuid
end
only_one_active_session_per_segment() click to toggle source
# File lib/segmentor/session.rb, line 76
def only_one_active_session_per_segment
  # only one active session per segment is allowed
  if status == ACTIVE && ::Segmentor::Session.where(status: ACTIVE, segment_id: segment_id).count.positive?
    raise ::Segmentor::Errors::SessionStateError, 'only one active session per segment is allowed'
  end
end