Class: Lcms::Engine::Grades
- Inherits:
-
Object
- Object
- Lcms::Engine::Grades
- Defined in:
- app/entities/lcms/engine/grades.rb
Overview
Value object for abstracting Grades info from the Resource Model
Constant Summary collapse
- GRADES =
['prekindergarten', 'kindergarten', 'grade 1', 'grade 2', 'grade 3', 'grade 4', 'grade 5', 'grade 6', 'grade 7', 'grade 8', 'grade 9', 'grade 10', 'grade 11', 'grade 12'].freeze
- GRADES_ABBR =
%w(pk k 1 2 3 4 5 6 7 8 9 10 11 12).freeze
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #average(abbr: true) ⇒ Object
- #average_number ⇒ Object
- #grade_abbr(abbr) ⇒ Object
-
#initialize(model) ⇒ Grades
constructor
A new instance of Grades.
- #list ⇒ Object
- #range ⇒ Object
- #to_str ⇒ Object
Constructor Details
#initialize(model) ⇒ Grades
Returns a new instance of Grades.
15 16 17 |
# File 'app/entities/lcms/engine/grades.rb', line 15 def initialize(model) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
13 14 15 |
# File 'app/entities/lcms/engine/grades.rb', line 13 def model @model end |
Instance Method Details
#average(abbr: true) ⇒ Object
29 30 31 32 33 34 |
# File 'app/entities/lcms/engine/grades.rb', line 29 def average(abbr: true) return nil if average_number.nil? avg = GRADES[average_number] abbr ? (grade_abbr(avg) || 'base') : avg end |
#average_number ⇒ Object
36 37 38 39 40 |
# File 'app/entities/lcms/engine/grades.rb', line 36 def average_number return nil if list.empty? list.map { |g| GRADES.index(g) }.sum / (list.size.nonzero? || 1) end |
#grade_abbr(abbr) ⇒ Object
42 43 44 45 46 47 48 |
# File 'app/entities/lcms/engine/grades.rb', line 42 def grade_abbr(abbr) grade = abbr.downcase return 'k' if grade == 'kindergarten' return 'pk' if grade == 'prekindergarten' grade[/\d+/] end |
#list ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'app/entities/lcms/engine/grades.rb', line 19 def list @list ||= if model.is_a?(Resource) Array.wrap model.['grade'] elsif model.is_a?(Search::Document) Array.wrap model.grade.presence else model.grade_list end.sort_by { |g| GRADES.index(g) } end |
#range ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/entities/lcms/engine/grades.rb', line 56 def range groups = [] # hold each groups of subsequent grades chain chain = [] # current chain of grades prev = nil # previous grade 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 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] end prev = g end groups << chain.dup unless chain.empty? # finally we grab only the first and last from each chain to make the range pairs groups.map { |c| c.size < 2 ? c.first : "#{c.first}-#{c.last}" }.join(', ') end |
#to_str ⇒ Object
50 51 52 53 54 |
# File 'app/entities/lcms/engine/grades.rb', line 50 def to_str return '' unless list.any? "Grade #{range}" end |