Sha256: ab957e6558c122a1898473403318d7f7cd0834c0aeafd82565cf2344d383049f

Contents?: true

Size: 763 Bytes

Versions: 1

Compression:

Stored size: 763 Bytes

Contents

require "metacrunch/file"

module Metacrunch
  class File::Destination

    DEFAULT_OPTIONS = {
      override_existing_file: false
    }

    def initialize(filename, options = {})
      @filename = ::File.expand_path(filename)
      @options = DEFAULT_OPTIONS.deep_merge(options)

      if ::File.exists?(@filename) && @options[:override_existing_file] == false
        raise "File `#{@filename}` exists but `override_existing_file` option was set to `false`"
      end

      @file = ::File.open(@filename, 'wb+')
    end

    def write(data)
      return if data.blank?

      if data.is_a?(Array)
        data.each { |row| @file.write(row) }
      else
        @file.write(data)
      end
    end

    def close
      @file.close if @file
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metacrunch-file-1.2.1 lib/metacrunch/file/destination.rb