Sha256: fb34c1393d1fe18732088d8d83b9e6703ef810d61ae5a21ca4b1a73397491fd4

Contents?: true

Size: 1.1 KB

Versions: 32

Compression:

Stored size: 1.1 KB

Contents

module Kafo
  module DataTypes
    class Float < DataType
      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

      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

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

      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
    end

    DataType.register_type('Float', Float)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
kafo-7.1.0 lib/kafo/data_types/float.rb
kafo-7.0.0 lib/kafo/data_types/float.rb
kafo-6.5.0 lib/kafo/data_types/float.rb
kafo-6.4.1 lib/kafo/data_types/float.rb
kafo-6.4.0 lib/kafo/data_types/float.rb
kafo-6.3.0 lib/kafo/data_types/float.rb
kafo-6.2.1 lib/kafo/data_types/float.rb
kafo-6.2.0 lib/kafo/data_types/float.rb
kafo-6.1.2 lib/kafo/data_types/float.rb
kafo-6.1.1 lib/kafo/data_types/float.rb
kafo-6.1.0 lib/kafo/data_types/float.rb
kafo-6.0.0 lib/kafo/data_types/float.rb
kafo-5.1.0 lib/kafo/data_types/float.rb
kafo-5.0.1 lib/kafo/data_types/float.rb
kafo-5.0.0 lib/kafo/data_types/float.rb
kafo-4.1.0 lib/kafo/data_types/float.rb
kafo-4.0.1 lib/kafo/data_types/float.rb
kafo-4.0.0 lib/kafo/data_types/float.rb
kafo-3.0.0 lib/kafo/data_types/float.rb
kafo-2.1.0 lib/kafo/data_types/float.rb