Class: Eco::API::Common::Session::FileManager

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Data::Files::ClassMethods

#copy_file, #create_directory, #dir_exists?, #encoding, #file_basename, #file_empty?, #file_exists?, #file_fullpath, #file_name, #file_path, #has_bom?, #script_subfolder, #split, #timestamp, #timestamp_file

Constructor Details

#initialize(init = {}, enviro: nil) ⇒ FileManager

Returns a new instance of FileManager.



12
13
14
15
16
17
# 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

Instance Attribute Details

#dirObject (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_pathObject

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_patternObject

Returns the value of attribute timestamp_pattern.



10
11
12
# File 'lib/eco/api/common/session/file_manager.rb', line 10

def timestamp_pattern
  @timestamp_pattern
end

Instance Method Details

#append(content, filename, mode: :string) ⇒ Object

if the file does not exist, it creates it



83
84
85
86
87
88
89
90
# 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

#file(filename, should_exist: false) ⇒ Object

FILE #####



33
34
35
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# 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

#loggerObject



28
29
30
# File 'lib/eco/api/common/session/file_manager.rb', line 28

def logger
  @enviro&.logger || ::Logger.new(IO::NULL)
end

#newest(filename) ⇒ Object



37
38
39
# 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) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



78
79
80
# 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) ⇒ Object



62
63
64
# 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