Sha256: 4d0730a577faa5f67cce0d8f6dd662ffd18d8cc102b88019c936d61d42e7805e

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Mutations
  class FileFilter < AdditionalFilter
    @default_options = {
      :nils => false,       # true allows an explicit nil to be valid. Overrides any other options
      :upload => false,     # if true, also checks the file is has original_filename and content_type methods.
      :size => nil          # An integer value like 1_000_000 limits the size of the file to 1M bytes
    }

    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 the data responds to each of the methods
      methods = [:read, :size]
      methods.concat([:original_filename, :content_type]) if options[:upload]
      methods.each do |method|
        return [data, :file] unless data.respond_to?(method)
      end

      if options[:size].is_a?(Integer)
        return [data, :size] if data.size > options[:size]
      end

      # 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/file_filter.rb
mutations-0.9.0 lib/mutations/file_filter.rb
mutations-0.8.3 lib/mutations/file_filter.rb
mutations-0.8.2 lib/mutations/file_filter.rb
mutations-0.8.1 lib/mutations/file_filter.rb