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

Inherits:
Object
  • Object
show all
Includes:
Data::Files, Language::AuxiliarLogger
Defined in:
lib/eco/api/common/session/file_manager.rb

Constant Summary

Constants included from Data::Files::Timestamp

Data::Files::Timestamp::DEFAULT_TIMESTAMP

Constants included from Data::Files::Folder

Data::Files::Folder::PRESERVED_FILES

Constants included from Data::Files::Encoding

Data::Files::Encoding::BOM_BYTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Methods included from Data::Files::ClassMethods

#copy_file, #split

Methods included from Data::Files::RelativePath

#to_relative_path

Methods included from Data::Files::Timestamp

#timestamp, #timestamp_file

Methods included from Data::Files::Folder

#archive_file, #clear_folder, #csv_files, #ensure_file_path!, #ensure_folder!, #folder_files

Methods included from Data::Files::Encoding

#bom?, #encoding, #get_file_content_with_encoding, #remove_bom, #scoped_encoding

Methods included from Data::Files::Content

#get_file_content, #read_with_tolerance

Constructor Details

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

Returns a new instance of FileManager.



9
10
11
12
13
14
15
16
# File 'lib/eco/api/common/session/file_manager.rb', line 9

def initialize(init = {}, enviro: nil)
  @enviro = enviro
  init    = @enviro.config if @enviro && init.empty?

  @timestamp_pattern   = init.files.timestamp_pattern
  @timestamp_pattern ||= Eco::Data::Files::Timestamp::DEFAULT_TIMESTAMP
  self.dir_path        = init.working_directory || Dir.pwd
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



6
7
8
# File 'lib/eco/api/common/session/file_manager.rb', line 6

def dir
  @dir
end

#dir_pathObject

Returns the value of attribute dir_path.



6
7
8
# File 'lib/eco/api/common/session/file_manager.rb', line 6

def dir_path
  @dir_path
end

#timestamp_patternObject

Returns the value of attribute timestamp_pattern.



7
8
9
# File 'lib/eco/api/common/session/file_manager.rb', line 7

def timestamp_pattern
  @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) ⇒ String

Returns resolved filename path (relative or absolute).

Returns:

  • (String)

    resolved filename path (relative or absolute)



37
38
39
# File 'lib/eco/api/common/session/file_manager.rb', line 37

def file(filename, should_exist: false)
  dir.file(filename, should_exist: should_exist)
end

#file_content(filename, mode: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eco/api/common/session/file_manager.rb', line 46

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.timestamp_file(file) if modifier == :timestamp
  file
end

#load_json(filename) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/eco/api/common/session/file_manager.rb', line 60

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

#loggerObject



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

def logger
  return @enviro.logger if @enviro
  super                 if defined?(super)
end

#newest(filename) ⇒ String

Returns newest file by last modified date.

Returns:

  • (String)

    newest file by last modified date.



42
43
44
# File 'lib/eco/api/common/session/file_manager.rb', line 42

def newest(filename)
  dir.newest_file(file: filename)
end

#save(content, filename, modifier = :no_stamp, mode: :string) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/eco/api/common/session/file_manager.rb', line 109

def save(content, filename, modifier = :no_stamp, mode: :string)
  file = filename_for(filename, modifier)
  FileUtils.mkdir_p(File.dirname(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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/eco/api/common/session/file_manager.rb', line 83

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)
  FileUtils.mkdir_p(File.dirname(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



79
80
81
# File 'lib/eco/api/common/session/file_manager.rb', line 79

def touch(filename, modifier = :no_stamp, mode: :string)
  save('', filename, modifier, mode: mode)
end