Class: FormKeeper::Constraint::Uint

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

Instance Method Summary collapse

Instance Method Details

#validate(value, arg) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/formkeeper.rb', line 167

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