Sha256: 6cb7e8b3c327bf4cc6cab545002ee694468d5ed23333981b5295593bd11ce806

Contents?: true

Size: 1.14 KB

Versions: 12

Compression:

Stored size: 1.14 KB

Contents

module Kafo
  module DataTypes
    class Integer < 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
          "integer (at least #{@min})"
        elsif @min == :infinite && @max != :infinite
          "integer (up to #{@max})"
        elsif @min != :infinite && @max != :infinite
          "integer (between #{@min} and #{@max})"
        else
          "integer"
        end
      end

      def typecast(value)
        Integer(value)
      rescue TypeError, ArgumentError
        value
      end

      def valid?(input, errors = [])
        unless input.is_a?(::Integer)
          errors << "#{input.inspect} is not a valid integer"
          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('Integer', Integer)
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
kafo-7.6.0 lib/kafo/data_types/integer.rb
kafo-7.5.1 lib/kafo/data_types/integer.rb
kafo-7.5.0 lib/kafo/data_types/integer.rb
kafo-7.4.0 lib/kafo/data_types/integer.rb
kafo-7.3.0 lib/kafo/data_types/integer.rb
kafo-7.2.0 lib/kafo/data_types/integer.rb
kafo-7.1.0 lib/kafo/data_types/integer.rb
kafo-7.0.0 lib/kafo/data_types/integer.rb
kafo-6.5.0 lib/kafo/data_types/integer.rb
kafo-6.4.1 lib/kafo/data_types/integer.rb
kafo-6.4.0 lib/kafo/data_types/integer.rb
kafo-6.3.0 lib/kafo/data_types/integer.rb