Sha256: a9dd3bacd7d661e1cf807f4f9bc2bca97b012c99cfda385a40435d16c0769a41

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 KB

Contents


module Attributor

  class Integer < Numeric

    EXAMPLE_RANGE = 1000.freeze

    def self.native_type
      return ::Integer
    end


    def self.example(context=nil, options: {})
      validate_options(options)

      # Set default values
      if options[:min].nil? && options[:max].nil?
        min = 0
        max = EXAMPLE_RANGE
      elsif options[:min].nil?
        max = options[:max]
        min = max - EXAMPLE_RANGE
      elsif options[:max].nil?
        min = options[:min]
        max = min + EXAMPLE_RANGE
      else
        min = options[:min]
        max = options[:max]
      end

      # Generate random number on interval [min,max]
      rand(max-min+1) + min
    end

    def self.load(value, context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
      Integer(value)
    rescue TypeError
      super
    end

    def self.validate_options(options)
      if options.has_key?(:min) && options.has_key?(:max)
        # Both :max and :min must be integers
        raise AttributorException.new("Invalid range: [#{options[:min].inspect}, #{options[:max].inspect}]") if !options[:min].is_a?(::Integer) || !options[:max].is_a?(::Integer)

        # :max cannot be less than :min
        raise AttributorException.new("Invalid range: [#{options[:min].inspect}, #{options[:max].inspect}]") if options[:max] < options[:min]
      elsif !options.has_key?(:min) && options.has_key?(:max)
        # :max must be an integer
        raise AttributorException.new("Invalid range: [, #{options[:max].inspect}]") if !options[:max].is_a?(::Integer)
      elsif options.has_key?(:min) && !options.has_key?(:max)
        # :min must be an integer
        raise AttributorException.new("Invalid range: [#{options[:min].inspect},]") if !options[:min].is_a?(::Integer)
      else
        # Neither :min nor :max were given, noop
      end
      true
    end

  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
attributor-5.0.2 lib/attributor/types/integer.rb
attributor-5.0.1 lib/attributor/types/integer.rb
attributor-5.0 lib/attributor/types/integer.rb
attributor-4.2.0 lib/attributor/types/integer.rb
attributor-4.1.0 lib/attributor/types/integer.rb
attributor-4.0.1 lib/attributor/types/integer.rb
attributor-4.0.0 lib/attributor/types/integer.rb
attributor-3.0.1 lib/attributor/types/integer.rb
attributor-3.0 lib/attributor/types/integer.rb
attributor-2.6.1 lib/attributor/types/integer.rb