Class: FormKeeper::Constraint::Int

Inherits:
Base
  • Object
show all
Defined in:
lib/formkeeper.rb

Instance Method Summary collapse

Instance Method Details

#validate(value, arg) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/formkeeper.rb', line 125

def validate(value, arg)
  result = value =~ /^\-?[[:digit:]]+$/
  if result and arg.kind_of?(Hash)
    if arg.has_key?(:gt)
      unless arg[:gt].kind_of?(Fixnum)
        raise ArgumentError.new ':gt should be Fixnum'
      end
      return false unless value.to_i > arg[:gt];
    end
    if arg.has_key?(:gte)
      unless arg[:gte].kind_of?(Fixnum)
        raise ArgumentError.new ':gte should be Fixnum'
      end
      return false unless value.to_i >= arg[:gte];
    end
    if arg.has_key?(:lt)
      unless arg[:lt].kind_of?(Fixnum)
        raise ArgumentError.new ':lt should be Fixnum'
      end
      return false unless value.to_i < arg[:lt];
    end
    if arg.has_key?(:lte)
      unless arg[:lte].kind_of?(Fixnum)
        raise ArgumentError.new ':lte should be Fixnum'
      end
      return false unless value.to_i <= arg[:lte];
    end
    if arg.has_key?(:between)
      unless arg[:between].kind_of?(Range)
        raise ArgumentError.new ':between should be Range'
      end
      return false unless arg[:between].include?(value.to_i);
    end
    true
  else
    result = !result if !arg
    !!result
  end
end