Sha256: 679f64055ee8e41406f60ff041d6820920c08b70ba54d1166d5b790e2ecd4f27

Contents?: true

Size: 700 Bytes

Versions: 1

Compression:

Stored size: 700 Bytes

Contents

class CsvUploader

  attr_reader :dir

  def initialize(dir)
    self.class.mkdir dir
    @dir = dir
  end

  class << self
    def create(dir = 'public/upload')
      CsvUploader.new dir
    end

    def mkdir(dir)
      FileUtils.mkpath dir unless File.directory?(dir)
    end
  end

  def save(stream)
    file = save_stream(stream)
    yield file if block_given?
  end

  protected
  def save_stream(stream)
    if stream.is_a? Hash
      return write_stream(stream[:tempfile], stream[:filename])
    end
    write_stream(stream, stream.original_filename) if stream.respond_to? :original_filename
  end

  def write_stream(stream, filename)
    CsvStream.write(stream, @dir, filename)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_uploader_tool-0.1.0 lib/csv_uploader_tool/csv_uploader.rb