Class: AnagramSolver::FileManager
- Inherits:
-
Object
- Object
- AnagramSolver::FileManager
- Defined in:
- lib/anagram_solver/file_manager.rb
Instance Attribute Summary collapse
-
#current_file ⇒ Object
readonly
Returns the value of attribute current_file.
-
#root_path ⇒ Object
readonly
Returns the value of attribute root_path.
Instance Method Summary collapse
-
#current_file_path ⇒ Object
Returns file’s root path.
-
#file_exist?(_file_name_ = false) ⇒ Boolean
Checks file existence using current_file_path.
-
#initialize(current_file) ⇒ FileManager
constructor
current_file is a temp_file to read content from it and be destroyed later on when everything gets garbaged.
-
#new_file(new_name = false, body) ⇒ Object
Creates a new file, saves it based on root_path provided.
- #read_lines ⇒ Object
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_file ⇒ Object (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_path ⇒ Object (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_path ⇒ Object
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.
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_lines ⇒ Object
32 33 34 |
# File 'lib/anagram_solver/file_manager.rb', line 32 def read_lines open_safely! { |f| f.rewind; f.read } end |