app/entities/lcms/engine/grades.rb in lcms-engine-0.3.0 vs app/entities/lcms/engine/grades.rb in lcms-engine-0.3.1
- old
+ new
@@ -10,35 +10,46 @@
GRADES_ABBR = %w(pk k 1 2 3 4 5 6 7 8 9 10 11 12).freeze
attr_reader :model
+ class << self
+ def grades
+ ::Lcms::Engine::Grades::GRADES
+ end
+
+ def grades_abbrevs
+ ::Lcms::Engine::Grades::GRADES_ABBR
+ end
+ end
+
def initialize(model)
@model = model
end
def list
- @list ||= if model.is_a?(Resource)
+ @list ||= case model
+ when Resource
Array.wrap model.metadata['grade']
- elsif model.is_a?(Search::Document)
+ when Search::Document
Array.wrap model.grade.presence
else
model.grade_list
- end.sort_by { |g| GRADES.index(g) }
+ end.sort_by { |g| self.class.grades.index(g) }
end
def average(abbr: true)
return nil if average_number.nil?
- avg = GRADES[average_number]
+ avg = self.class.grades[average_number]
abbr ? (grade_abbr(avg) || 'base') : avg
end
def average_number
return nil if list.empty?
- list.map { |g| GRADES.index(g) }.sum / (list.size.nonzero? || 1)
+ list.map { |g| self.class.grades.index(g) }.sum / (list.size.nonzero? || 1)
end
def grade_abbr(abbr)
grade = abbr.downcase
return 'k' if grade == 'kindergarten'
@@ -60,10 +71,10 @@
list.each_with_index do |g, idx|
abbr = grade_abbr(g).upcase
# if the current grade is subsequent we store on the same chain
- if idx.zero? || GRADES.index(g) == GRADES.index(prev) + 1
+ if idx.zero? || self.class.grades.index(g) == self.class.grades.index(prev) + 1
chain << abbr
else
# the grade is not subsequent, so we store the current chain, and create a new one
groups << chain.dup unless chain.empty?
chain = [abbr]