Sha256: ec16d991cc80b9c374cd32ab7d5981f3397b49ca6fabec0f55515bbb30d01a8d

Contents?: true

Size: 1018 Bytes

Versions: 5

Compression:

Stored size: 1018 Bytes

Contents

module Mutations
  class FloatFilter < AdditionalFilter
    @default_options = {
      :nils => false,       # true allows an explicit nil to be valid. Overrides any other options
      :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
      
      # Now check if it's empty:
      return [data, :empty] if data == ""

      # Ensure it's the correct data type (Float)
      if !data.is_a?(Float)
        if data.is_a?(String) && data =~ /^[-+]?\d*\.?\d+/
          data = data.to_f
        elsif data.is_a?(Integer)
          data = data.to_f
        else
          return [data, :float]
        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

5 entries across 5 versions & 1 rubygems

Version Path
mutations-0.9.1 lib/mutations/float_filter.rb
mutations-0.9.0 lib/mutations/float_filter.rb
mutations-0.8.3 lib/mutations/float_filter.rb
mutations-0.8.2 lib/mutations/float_filter.rb
mutations-0.8.1 lib/mutations/float_filter.rb