Sha256: 66dfdb539711e2a0fb35afaa7658ace58fbb4b70cc1e55613690f41eeba7d307

Contents?: true

Size: 1.42 KB

Versions: 25

Compression:

Stored size: 1.42 KB

Contents

class ExamRegistration::AuthorizationCriterion
  attr_reader :value

  def initialize(value)
    @value = value
  end

  def type
    self.class.name.demodulize.underscore
  end

  def as_json
    { type: type, value: value }
  end

  def ensure_valid!
    raise "Invalid criterion value #{value} for #{type}" unless valid?
  end

  def process_request!(authorization_request)
    authorization_request.update! status: authorization_status_for(authorization_request)
  end

  def authorization_status_for(authorization_request)
    meets_authorization_criteria?(authorization_request) ? :approved : :rejected
  end

  def self.parse(type, value)
    parse_criterion_type(type, value)
  end

  def self.parse_criterion_type(type, value)
    "ExamRegistration::AuthorizationCriterion::#{type.camelize}".constantize.new(value)
  rescue
    raise "Invalid criterion type #{type}"
  end
end

class ExamRegistration::AuthorizationCriterion::None < ExamRegistration::AuthorizationCriterion
  def initialize(_)
    @value = nil
  end

  def valid?
    !value
  end

  def meets_authorization_criteria?(_authorization_request)
    true
  end
end

class ExamRegistration::AuthorizationCriterion::PassedExercises < ExamRegistration::AuthorizationCriterion
  def valid?
    value.positive?
  end

  def meets_authorization_criteria?(authorization_request)
    authorization_request.user.passed_submissions_count_in(authorization_request.organization) >= value
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
mumuki-domain-9.10.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.9.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.8.1 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.8.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.7.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.6.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.5.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.4.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.3.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.2.1 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.2.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.1.2 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.1.1 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.1.0 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.6 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.5 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.4 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.3 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.2 app/models/exam_registration/authorization_criterion.rb
mumuki-domain-9.0.0 app/models/exam_registration/authorization_criterion.rb