lib/eco/api/common/session/file_manager.rb in eco-helpers-2.7.21 vs lib/eco/api/common/session/file_manager.rb in eco-helpers-2.7.22
- old
+ new
@@ -1,30 +1,28 @@
module Eco
module API
module Common
module Session
class FileManager
-
include Eco::Data::Files
attr_reader :dir, :dir_path
attr_accessor :timestamp_pattern
def initialize(init = {}, enviro: nil)
@enviro = enviro
- init = @enviro.config if @enviro && init.empty?
+ init = @enviro.config if @enviro && init.empty?
+
@timestamp_pattern = init.files.timestamp_pattern || DEFAULT_TIMESTAMP_PATTERN
- self.dir_path = init.working_directory || Dir.pwd
+ self.dir_path = init.working_directory || Dir.pwd
end
def dir_path=(value)
- begin
- @dir = Eco::Data::Files::Directory.new(value)
- @dir_path = @dir.create
- rescue Exception => e
- logger.error("could not create or make any sense of directory '#{value}': #{e.to_s}")
- end
+ @dir = Eco::Data::Files::Directory.new(value)
+ @dir_path = @dir.create
+ rescue StandardError => e
+ logger.error("could not create or make any sense of directory '#{value}': #{e.to_s}")
end
def logger
@enviro&.logger || ::Logger.new(IO::NULL)
end
@@ -38,56 +36,95 @@
dir.newest_file(file: filename)
end
def file_content(filename, mode: nil)
file = dir.file(filename, should_exist: true)
- if !file
+
+ 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
def load_json(filename)
- content = file_content(filename)
- begin
- parsed = content && JSON.parse(content)
- rescue JSON::ParserError => e
- pp "Parsing error on file #{filename}"
- raise e
+ 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
- return parsed
+
+ 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
def touch(filename, modifier = :no_stamp, mode: :string)
save("", filename, modifier, mode: mode)
end
- def save(content, filename, modifier = :no_stamp, mode: :string)
- file = dir.file(filename)
- file = FileManager.timestamp_file(file) if modifier == :timestamp
- mode = (mode == :binary) ? 'wb' : 'w'
+ def save_json(data, filename, modifier = :no_stamp)
+ return save(data.to_json, filename, modifier) unless data.is_a?(Array)
- FileManager.create_directory(FileManager.file_fullpath(file))
+ file = filename_for(filename, modifier)
+ FileManager.create_directory(
+ FileManager.file_fullpath(file)
+ )
logger.debug("Writting to file '#{file}'")
- File.open(file, mode) { |fd| fd << content }
- return 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
- def save_json(data, filename, modifier = :no_stamp)
- return save(data.to_json, filename, modifier)
+ 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
# if the file does not exist, it creates it
def append(content, filename, mode: :string)
file = dir.file(filename)
- mode = (mode == :binary) ? 'ab' : 'a'
logger.debug("Appending to file '#{file}'")
- File.open(file, mode) { |fd| fd << content + "\n" } # '\n' won't add line
- return file
+
+ mode = mode == :binary ? 'ab' : 'a'
+ File.open(file, mode) { |fd| fd << "#{content}\n" }
+ file
+ end
+
+ def filename_for(filename, modifier = :no_stamp)
+ file = dir.file(filename)
+ FileManager.timestamp_file(file) if modifier == :timestamp
end
end
end
end
end