Class: Eco::API::Common::Session::FileManager
- Includes:
- Data::Files, Language::AuxiliarLogger
- Defined in:
- lib/eco/api/common/session/file_manager.rb
Constant Summary
Constants included from Data::Files
Data::Files::DEFAULT_TIMESTAMP_PATTERN
Constants included from Data::Files::Encoding
Data::Files::Encoding::BOM_BYTES
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#dir_path ⇒ Object
Returns the value of attribute dir_path.
-
#timestamp_pattern ⇒ Object
Returns the value of attribute timestamp_pattern.
Instance Method Summary collapse
-
#append(content, filename, mode: :string) ⇒ Object
if the file does not exist, it creates it.
-
#file(filename, should_exist: false) ⇒ Object
FILE #####.
- #file_content(filename, mode: nil) ⇒ Object
- #filename_for(filename, modifier = :no_stamp) ⇒ Object
-
#initialize(init = {}, enviro: nil) ⇒ FileManager
constructor
A new instance of FileManager.
- #load_json(filename) ⇒ Object
- #logger ⇒ Object
- #newest(filename) ⇒ Object
- #save(content, filename, modifier = :no_stamp, mode: :string) ⇒ Object
- #save_json(data, filename, modifier = :no_stamp) ⇒ Object
- #touch(filename, modifier = :no_stamp, mode: :string) ⇒ Object
Methods included from Language::AuxiliarLogger
Methods included from Data::Files::ClassMethods
#copy_file, #create_directory, #csv_files, #dir_exists?, #file_basename, #file_empty?, #file_exists?, #file_fullpath, #file_name, #folder_files, #script_subfolder, #split, #timestamp, #timestamp_file
Methods included from Data::Files::Encoding
#encoding, #file_empty?, #file_exists?, #get_file_content_with_encoding, #has_bom?, #remove_bom, #scoped_encoding
Methods included from Data::Files::InstanceMethods
#get_file_content, #read_with_tolerance
Constructor Details
#initialize(init = {}, enviro: nil) ⇒ FileManager
Returns a new instance of FileManager.
12 13 14 15 16 17 18 |
# File 'lib/eco/api/common/session/file_manager.rb', line 12 def initialize(init = {}, enviro: nil) @enviro = enviro init = @enviro.config if @enviro && init.empty? @timestamp_pattern = init.files. || DEFAULT_TIMESTAMP_PATTERN self.dir_path = init.working_directory || Dir.pwd end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
9 10 11 |
# File 'lib/eco/api/common/session/file_manager.rb', line 9 def dir @dir end |
#dir_path ⇒ Object
Returns the value of attribute dir_path.
9 10 11 |
# File 'lib/eco/api/common/session/file_manager.rb', line 9 def dir_path @dir_path end |
#timestamp_pattern ⇒ Object
Returns the value of attribute timestamp_pattern.
10 11 12 |
# File 'lib/eco/api/common/session/file_manager.rb', line 10 def @timestamp_pattern end |
Instance Method Details
#append(content, filename, mode: :string) ⇒ Object
if the file does not exist, it creates it
121 122 123 124 125 126 127 128 129 |
# File 'lib/eco/api/common/session/file_manager.rb', line 121 def append(content, filename, mode: :string) file = dir.file(filename) log(:debug) { "Appending to file '#{file}'" } mode = mode == :binary ? 'ab' : 'a' File.open(file, mode) { |fd| fd << "#{content}\n" } file end |
#file(filename, should_exist: false) ⇒ Object
FILE #####
34 35 36 |
# File 'lib/eco/api/common/session/file_manager.rb', line 34 def file(filename, should_exist: false) dir.file(filename, should_exist: should_exist) end |
#file_content(filename, mode: nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/eco/api/common/session/file_manager.rb', line 42 def file_content(filename, mode: nil) file = dir.file(filename, should_exist: true) unless file log(:error) { "Can't read from file '#{filename}' because it does not exist." } return nil end log(:debug) { "Reading from file '#{file}'" } mode ? File.read(file, mode: mode) : File.read(file) end |
#filename_for(filename, modifier = :no_stamp) ⇒ Object
131 132 133 134 135 |
# File 'lib/eco/api/common/session/file_manager.rb', line 131 def filename_for(filename, modifier = :no_stamp) file = dir.file(filename) file = FileManager.(file) if modifier == :timestamp file end |
#load_json(filename) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/eco/api/common/session/file_manager.rb', line 56 def load_json(filename) file = dir.file(filename, should_exist: true) unless file log(:error) { "Can't read from file '#{filename}' because it does not exist." } return nil end fd = File.open(file) JSON.load fd # rubocop:disable Security/JSONLoad rescue JSON::ParserError => e pp "Parsing error on file #{file}" raise e ensure fd&.close end |
#logger ⇒ Object
29 30 31 |
# File 'lib/eco/api/common/session/file_manager.rb', line 29 def logger @enviro&.logger || super end |
#newest(filename) ⇒ Object
38 39 40 |
# File 'lib/eco/api/common/session/file_manager.rb', line 38 def newest(filename) dir.newest_file(file: filename) end |
#save(content, filename, modifier = :no_stamp, mode: :string) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/eco/api/common/session/file_manager.rb', line 107 def save(content, filename, modifier = :no_stamp, mode: :string) file = filename_for(filename, modifier) FileManager.create_directory( FileManager.file_fullpath(file) ) log(:debug) { "Writting to file '#{file}'" } mode = mode == :binary ? 'wb' : 'w' File.open(file, mode) { |fd| fd << content } file end |
#save_json(data, filename, modifier = :no_stamp) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/eco/api/common/session/file_manager.rb', line 79 def save_json(data, filename, modifier = :no_stamp) return save(data.to_json, filename, modifier) unless data.is_a?(Array) file = filename_for(filename, modifier) FileManager.create_directory( FileManager.file_fullpath(file) ) log(:debug) { "Writting to file '#{file}'" } mode = mode == :binary ? 'wb' : 'w' File.open(file, mode) do |fd| first = true fd << '[' data.each do |elem| fd << "," unless first first = false fd << elem.to_json end fd << ']' end file end |
#touch(filename, modifier = :no_stamp, mode: :string) ⇒ Object
75 76 77 |
# File 'lib/eco/api/common/session/file_manager.rb', line 75 def touch(filename, modifier = :no_stamp, mode: :string) save("", filename, modifier, mode: mode) end |