Class: Eco::API::Common::Session::FileManager
- Includes:
- Data::Files
- 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 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 Language::AuxiliarLogger
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.
11 12 13 14 15 16 17 |
# File 'lib/eco/api/common/session/file_manager.rb', line 11 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.
8 9 10 |
# File 'lib/eco/api/common/session/file_manager.rb', line 8 def dir @dir end |
#dir_path ⇒ Object
Returns the value of attribute dir_path.
8 9 10 |
# File 'lib/eco/api/common/session/file_manager.rb', line 8 def dir_path @dir_path end |
#timestamp_pattern ⇒ Object
Returns the value of attribute timestamp_pattern.
9 10 11 |
# File 'lib/eco/api/common/session/file_manager.rb', line 9 def @timestamp_pattern end |
Instance Method Details
#append(content, filename, mode: :string) ⇒ Object
if the file does not exist, it creates it
114 115 116 117 118 119 120 121 122 |
# File 'lib/eco/api/common/session/file_manager.rb', line 114 def append(content, filename, mode: :string) file = dir.file(filename) logger.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 #####
31 32 33 |
# File 'lib/eco/api/common/session/file_manager.rb', line 31 def file(filename, should_exist: false) dir.file(filename, should_exist: should_exist) end |
#file_content(filename, mode: nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/eco/api/common/session/file_manager.rb', line 39 def file_content(filename, mode: nil) file = dir.file(filename, should_exist: true) unless file logger.error("Can't read from file '#{filename}' because it does not exist.") return nil end logger.debug("Reading from file '#{file}'") mode ? File.read(file, mode: mode) : File.read(file) end |
#filename_for(filename, modifier = :no_stamp) ⇒ Object
124 125 126 127 128 |
# File 'lib/eco/api/common/session/file_manager.rb', line 124 def filename_for(filename, modifier = :no_stamp) file = dir.file(filename) file = FileManager.(file) if modifier == :timestamp file end |
#load_json(filename) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/eco/api/common/session/file_manager.rb', line 51 def load_json(filename) file = dir.file(filename, should_exist: true) unless file logger.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
26 27 28 |
# File 'lib/eco/api/common/session/file_manager.rb', line 26 def logger @enviro&.logger || ::Logger.new(IO::NULL) end |
#newest(filename) ⇒ Object
35 36 37 |
# File 'lib/eco/api/common/session/file_manager.rb', line 35 def newest(filename) dir.newest_file(file: filename) end |
#save(content, filename, modifier = :no_stamp, mode: :string) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/eco/api/common/session/file_manager.rb', line 100 def save(content, filename, modifier = :no_stamp, mode: :string) file = filename_for(filename, modifier) FileManager.create_directory( FileManager.file_fullpath(file) ) logger.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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/eco/api/common/session/file_manager.rb', line 72 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) ) logger.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
68 69 70 |
# File 'lib/eco/api/common/session/file_manager.rb', line 68 def touch(filename, modifier = :no_stamp, mode: :string) save("", filename, modifier, mode: mode) end |