Class: Metacrunch::File::FileDestination

Inherits:
Object
  • Object
show all
Defined in:
lib/metacrunch/file/file_destination.rb

Direct Known Subclasses

Destination

Constant Summary collapse

DEFAULT_OPTIONS =
{
  override_existing_file: false
}

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ FileDestination

Returns a new instance of FileDestination.



10
11
12
13
14
15
16
17
18
19
# File 'lib/metacrunch/file/file_destination.rb', line 10

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

Instance Method Details

#closeObject



31
32
33
# File 'lib/metacrunch/file/file_destination.rb', line 31

def close
  @file.close if @file
end

#write(data) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/metacrunch/file/file_destination.rb', line 21

def write(data)
  return if data.blank?

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