Class: Kafo::DataTypes::Float

Inherits:
Kafo::DataType show all
Defined in:
lib/kafo/data_types/float.rb

Instance Method Summary collapse

Methods inherited from Kafo::DataType

#condition_value, #dump_default, #multivalued?, new_from_string, parse_hash, register_type, split_arguments, types, unregister_type

Constructor Details

#initialize(min = :default, max = :default) ⇒ Float

Returns a new instance of Float.



4
5
6
7
# File 'lib/kafo/data_types/float.rb', line 4

def initialize(min = :default, max = :default)
  @min = (min.to_s == 'default') ? :infinite : min.to_i
  @max = (max.to_s == 'default') ? :infinite : max.to_i
end

Instance Method Details

#to_sObject



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kafo/data_types/float.rb', line 9

def to_s
  if @min != :infinite && @max == :infinite
    "float (at least #{@min})"
  elsif @min == :infinite && @max != :infinite
    "float (up to #{@max})"
  elsif @min != :infinite && @max != :infinite
    "float (between #{@min} and #{@max})"
  else
    "float"
  end
end

#typecast(value) ⇒ Object



21
22
23
# File 'lib/kafo/data_types/float.rb', line 21

def typecast(value)
  value.to_s =~ /\d+/ ? value.to_f : value
end

#valid?(input, errors = []) ⇒ Boolean

Returns:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kafo/data_types/float.rb', line 25

def valid?(input, errors = [])
  unless input.is_a?(::Float)
    errors << "#{input.inspect} is not a valid float"
    return false
  end

  errors << "#{input} must be at least #{@min}" if @min != :infinite && input < @min
  errors << "#{input} must be up to #{@max}" if @max != :infinite && input > @max

  return errors.empty?
end