Class: Trafaret::Numeric

Inherits:
Validator show all
Defined in:
lib/trafaret/numeric.rb

Direct Known Subclasses

Float, Integer

Constant Summary collapse

UNDEFINED =
'Undefined'.freeze
INTEGER =
'Integer'.freeze
FLOAT =
'Float'.freeze

Instance Attribute Summary

Attributes inherited from Validator

#converters, #options

Instance Method Summary collapse

Methods inherited from Validator

#&, #===, #add, #call, #convert, #failure, #initialize, #perform_convert, #prepare, #to, #|

Constructor Details

This class inherits a constructor from Trafaret::Validator

Instance Method Details

#num_class_nameObject



11
12
13
# File 'lib/trafaret/numeric.rb', line 11

def num_class_name
  UNDEFINED
end

#try_convert(arg) ⇒ Object



7
8
9
# File 'lib/trafaret/numeric.rb', line 7

def try_convert(arg)
  raise 'type method need to be implemented'
end

#validate(data) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trafaret/numeric.rb', line 15

def validate(data)
  val = try_convert(data)
  return failure("Not an #{num_class_name}") unless val
  return failure('Too big') if @options[:lt] && val >= @options[:lt]
  return failure('Too big') if @options[:lte] && val > @options[:lte]
  return failure('Too small') if @options[:gt] && val <= @options[:gt]
  return failure('Too small') if @options[:gte] && val < @options[:gte]
  if @converters.empty?
    val
  else
    data
  end
end