class Eco::API::Common::Session::FileManager

Attributes

dir[R]
dir_path[R]
timestamp_pattern[RW]

Public Class Methods

new(init = {}, enviro: nil) click to toggle source
# 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.timestamp_pattern || DEFAULT_TIMESTAMP_PATTERN
  self.dir_path = init.working_directory || Dir.pwd
end

Public Instance Methods

append(content, filename, mode: :string) click to toggle source

if the file does not exist, it creates it

# File lib/eco/api/common/session/file_manager.rb, line 83
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
end
dir_path=(value) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 19
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
end
file(filename, should_exist: false) click to toggle source

FILE #####

# File lib/eco/api/common/session/file_manager.rb, line 33
def file(filename, should_exist: false)
  dir.file(filename, should_exist: should_exist)
end
file_content(filename) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 41
def file_content(filename)
  file = dir.file(filename, should_exist: true)
  if !file
    logger.error("Can't read from file '#{filename}' because it does not exist.")
    return nil
  end
  logger.debug("Reading from file '#{file}'")
  File.read(file)
end
load_json(filename) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 51
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
  end
  return parsed
end
logger() click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 28
def logger
  @enviro&.logger || ::Logger.new(IO::NULL)
end
newest(filename) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 37
def newest(filename)
  dir.newest_file(file: filename)
end
save(content, filename, modifier = :no_stamp, mode: :string) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 66
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'

  FileManager.create_directory(FileManager.file_fullpath(file))

  logger.debug("Writting to file '#{file}'")
  File.open(file, mode) { |fd| fd << content }
  return file
end
save_json(data, filename, modifier = :no_stamp) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 78
def save_json(data, filename, modifier = :no_stamp)
  return save(data.to_json, filename, modifier)
end
touch(filename, modifier = :no_stamp, mode: :string) click to toggle source
# File lib/eco/api/common/session/file_manager.rb, line 62
def touch(filename, modifier = :no_stamp, mode: :string)
  save("", filename, modifier, mode: mode)
end