Class: Bodhi::PrecisionValidator
- Defined in:
- lib/bodhi-slam/validators/precision.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
-
#initialize(number) ⇒ PrecisionValidator
constructor
A new instance of PrecisionValidator.
- #to_options ⇒ Object
- #validate(record, attribute, value) ⇒ Object
Methods inherited from Validator
constantize, #to_sym, #underscore
Constructor Details
#initialize(number) ⇒ PrecisionValidator
Returns a new instance of PrecisionValidator.
5 6 7 8 9 10 |
# File 'lib/bodhi-slam/validators/precision.rb', line 5 def initialize(number) if number.nil? raise ArgumentError.new("Expected :number to not be nil") end @number = number end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
3 4 5 |
# File 'lib/bodhi-slam/validators/precision.rb', line 3 def number @number end |
Instance Method Details
#to_options ⇒ Object
26 27 28 |
# File 'lib/bodhi-slam/validators/precision.rb', line 26 def {self.to_sym => @number} end |
#validate(record, attribute, value) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/bodhi-slam/validators/precision.rb', line 12 def validate(record, attribute, value) unless value.nil? if value.is_a?(Array) unless value.empty? record.errors.add(attribute, "must contain only values with #{@number} decimal points") unless value.delete_if{ |v| decimals(v) == @number }.empty? end else record.errors.add(attribute, "must have #{@number} decimal points") if decimals(value) != @number end end end |