Class: Tuning::Validations::CountValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- Tuning::Validations::CountValidator
- Defined in:
- lib/tuning/validations/count.rb
Constant Summary collapse
- MESSAGES =
{ is: :wrong_count, minimum: :too_few, maximum: :too_many }
- CHECKS =
{ is: :==, minimum: :>=, maximum: :<= }
Instance Method Summary collapse
-
#initialize(options) ⇒ CountValidator
constructor
A new instance of CountValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ CountValidator
Returns a new instance of CountValidator.
9 10 11 12 13 14 15 |
# File 'lib/tuning/validations/count.rb', line 9 def initialize() if range = ([:in] || [:within]) [:minimum] = range.min [:maximum] = range.max end super end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tuning/validations/count.rb', line 17 def validate_each(record, attribute, value) if value.respond_to?(:size) count = value.size CHECKS.each do |name, operator| if .has_key?(name) other = [name] unless count.send(operator, other) record.errors.add attribute, MESSAGES[name], count: other end end end else record.errors.add attribute, :uncountable end end |