class Bodhi::PrecisionValidator
Attributes
number[R]
Public Class Methods
new(number)
click to toggle source
# 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
Public Instance Methods
to_options()
click to toggle source
# File lib/bodhi-slam/validators/precision.rb, line 26 def to_options {self.to_sym => @number} end
validate(record, attribute, value)
click to toggle source
# 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
Private Instance Methods
decimals(a)
click to toggle source
# File lib/bodhi-slam/validators/precision.rb, line 31 def decimals(a) num = 0 while(a != a.to_i) num += 1 a *= 10 end num end