Class: AnagramSolver::FileManager

Inherits:
Object
  • Object
show all
Defined in:
lib/anagram_solver/file_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_file) ⇒ FileManager

current_file is a temp_file to read content from it and be destroyed later on when everything gets garbaged.

root_path is the path to save new files.

TODO: Make it work with instances of File, as it only works with Tempfile.



27
28
29
30
# File 'lib/anagram_solver/file_manager.rb', line 27

def initialize(current_file)
  @current_file = current_file
  @root_path    = AnagramSolver::RootPath
end

Instance Attribute Details

#current_fileObject (readonly)

Returns the value of attribute current_file.



14
15
16
# File 'lib/anagram_solver/file_manager.rb', line 14

def current_file
  @current_file
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



14
15
16
# File 'lib/anagram_solver/file_manager.rb', line 14

def root_path
  @root_path
end

Instance Method Details

#current_file_pathObject

Returns file’s root path



39
40
41
# File 'lib/anagram_solver/file_manager.rb', line 39

def current_file_path
  current_file.to_path
end

#file_exist?(_file_name_ = false) ⇒ Boolean

Checks file existence using current_file_path. However if absolute path is passed in, uses this over current_file_path.

Returns:

  • (Boolean)


63
64
65
# File 'lib/anagram_solver/file_manager.rb', line 63

def file_exist?(_file_name_=false)
  File.exists?(_file_name_ || current_file_path)
end

#new_file(new_name = false, body) ⇒ Object

Creates a new file, saves it based on root_path provided. NOTE: Ruby File#open when called with a block closes file stream for us. This is not compartible with Ruby 1.8.7.



50
51
52
53
54
55
56
# File 'lib/anagram_solver/file_manager.rb', line 50

def new_file(new_name=false, body)
  f_name = new_name || file_name_with_path
  File.open(f_name, "w") { |line|
    line.write(body)
    line
  }
end

#read_linesObject



32
33
34
# File 'lib/anagram_solver/file_manager.rb', line 32

def read_lines
  open_safely! { |f| f.rewind; f.read }
end