Class: AWS::Record::CountValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/aws/record/validators/count.rb

Constant Summary collapse

ACCEPTED_OPTIONS =
[
  :exactly, :within, :minimum, :maximum,
  :too_many, :too_few, :wrong_number,
  :on, :if, :unless,
]

Instance Attribute Summary

Attributes inherited from Validator

#attribute_names, #options

Instance Method Summary collapse

Methods inherited from Validator

#initialize, #validate

Constructor Details

This class inherits a constructor from AWS::Record::Validator

Instance Method Details

#setup(record_class) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/aws/record/validators/count.rb', line 28

def setup record_class
  ensure_at_least_one(:within, :exactly, :minimum, :maximum)
  ensure_exclusive(:within, :exactly, [:minimum, :maximum])
  ensure_type(Range, :within)
  ensure_type(Integer, :exactly, :minimum, :maximum)
  ensure_type(String, :too_many, :too_few, :wrong_number)
end

#validate_attribute(record, attribute_name, value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aws/record/validators/count.rb', line 36

def validate_attribute record, attribute_name, value

  count = case value
  when nil then 0
  when String then 1
  when Enumerable then value.count
  else 1
  end

  if exact = options[:exactly]
    unless count == exact
      record.errors.add(attribute_name, wrong_number(exact, count))
    end
  end

  if within = options[:within]
    if count < within.first
      record.errors.add(attribute_name, too_few(within.first, count))
    end
    if count > within.last
      record.errors.add(attribute_name, too_many(within.last, count))
    end
  end

  if min = options[:minimum]
    if count < min
      record.errors.add(attribute_name, too_few(min, count))
    end
  end

  if max = options[:maximum]
    if count > max
      record.errors.add(attribute_name, too_many(max, count))
    end
  end

end