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.
Class Method Summary collapse
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.
25 26 27 |
# File 'app/entities/lcms/engine/grades.rb', line 25 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 |
Class Method Details
.grades ⇒ Object
16 17 18 |
# File 'app/entities/lcms/engine/grades.rb', line 16 def grades ::Lcms::Engine::Grades::GRADES end |
.grades_abbrevs ⇒ Object
20 21 22 |
# File 'app/entities/lcms/engine/grades.rb', line 20 def grades_abbrevs ::Lcms::Engine::Grades::GRADES_ABBR end |
Instance Method Details
#average(abbr: true) ⇒ Object
40 41 42 43 44 45 |
# File 'app/entities/lcms/engine/grades.rb', line 40 def average(abbr: true) return nil if average_number.nil? avg = self.class.grades[average_number] abbr ? (grade_abbr(avg) || 'base') : avg end |
#average_number ⇒ Object
47 48 49 50 51 |
# File 'app/entities/lcms/engine/grades.rb', line 47 def average_number return nil if list.empty? list.map { |g| self.class.grades.index(g) }.sum / (list.size.nonzero? || 1) end |
#grade_abbr(abbr) ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/entities/lcms/engine/grades.rb', line 53 def grade_abbr(abbr) grade = abbr.downcase return 'k' if grade == 'kindergarten' return 'pk' if grade == 'prekindergarten' grade[/\d+/] end |
#list ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'app/entities/lcms/engine/grades.rb', line 29 def list @list ||= case model when Resource Array.wrap model.['grade'] when Search::Document Array.wrap model.grade.presence else model.grade_list end.sort_by { |g| self.class.grades.index(g) } end |
#range ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/entities/lcms/engine/grades.rb', line 67 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? || 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] 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
61 62 63 64 65 |
# File 'app/entities/lcms/engine/grades.rb', line 61 def to_str return '' unless list.any? "Grade #{range}" end |