Sha256: 18f2f919dc41b8a0d8385ad3956c5632e00a70d26909ba72a219fc29d6079676

Contents?: true

Size: 909 Bytes

Versions: 6

Compression:

Stored size: 909 Bytes

Contents

module Mutations
  class IntegerFilter < InputFilter
    @default_options = {
      nils: false,       # true allows an explicit nil to be valid. Overrides any other options
      # TODO: add strict
      min: nil,          # lowest value, inclusive
      max: nil           # highest value, inclusive
    }
    
    def filter(data)
      
      # Handle nil case
      if data.nil?
        return [nil, nil] if options[:nils]
        return [nil, :nils]
      end
      
      # Ensure it's the correct data type (Fixnum)
      if !data.is_a?(Fixnum)
        if data.is_a?(String) && data =~ /^-?\d/
          data = data.to_i
        else
          return [data, :integer]
        end
      end
      
      return [data, :min] if options[:min] && data < options[:min]
      return [data, :max] if options[:max] && data > options[:max]
      
      # We win, it's valid!
      [data, nil]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mutations-0.5.9 lib/mutations/integer_filter.rb
mutations-0.5.4 lib/mutations/integer_filter.rb
mutations-0.5.3 lib/mutations/integer_filter.rb
mutations-0.5.2 lib/mutations/integer_filter.rb
mutations-0.5.1 lib/mutations/integer_filter.rb
mutations-0.5.0 lib/mutations/integer_filter.rb