Class: CSVMagic::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_magic/file_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileHandler

Returns a new instance of FileHandler.



6
7
8
# File 'lib/csv_magic/file_handler.rb', line 6

def initialize()
  @path = ::File.join(::Rails.root.to_s, "tmp")
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/csv_magic/file_handler.rb', line 4

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/csv_magic/file_handler.rb', line 4

def path
  @path
end

Instance Method Details

#file_pathObject



25
26
27
# File 'lib/csv_magic/file_handler.rb', line 25

def file_path
  ::File.join(@path, @filename)
end

#load_file(filename) ⇒ Object



16
17
18
19
# File 'lib/csv_magic/file_handler.rb', line 16

def load_file(filename)
  @filename = filename
  ::File.exist?(file_path)
end

#remove_fileObject



21
22
23
# File 'lib/csv_magic/file_handler.rb', line 21

def remove_file
  ::File.delete(file_path)
end

#save_temp_file(tempfile) ⇒ Object



10
11
12
13
14
# File 'lib/csv_magic/file_handler.rb', line 10

def save_temp_file(tempfile)
  @filename = unique_filename
  ::FileUtils.copy_file(tempfile.path, file_path)
  ::File.exists?(file_path)
end

#unique_filenameObject



29
30
31
32
# File 'lib/csv_magic/file_handler.rb', line 29

def unique_filename
  t = ::Time.now.strftime("%Y%m%d")
  "#{t}-#{$$}-#{rand(0x100000000000000).to_s(36)}"
end