Class: Eaternet::Lives_1_0::LegendGroup::ComprehensiveValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/eaternet/lives_1_0.rb

Overview

Check that all the items in this LegendGroup:

1. Are of the class, Legend
2. Cover the range of scores from 0-100 without overlap

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, legends) ⇒ Object


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/eaternet/lives_1_0.rb', line 213

def validate_each(record, attribute, legends)
  scores = (0..100).to_a
  legends.each do |legend|
    unless legend.class == Legend
      record.errors.add attribute, 'must be a Legend'
      return
    end
    range = (legend.minimum_score..legend.maximum_score)
    range.each do |score|
      if scores.delete(score).nil?
        unless score == legend.minimum_score || score == legend.maximum_score
          record.errors.add attribute, 'may not overlap'
          return
        end
      end
    end
  end
  unless scores.empty?
    record.errors.add attribute, 'do not cover entire span from 0–100'
  end
end