Class: FileSystem
- Inherits:
-
Object
- Object
- FileSystem
- Defined in:
- lib/jirametrics/file_system.rb
Instance Attribute Summary collapse
-
#logfile ⇒ Object
Returns the value of attribute logfile.
-
#logfile_name ⇒ Object
Returns the value of attribute logfile_name.
Instance Method Summary collapse
-
#compress(node) ⇒ Object
In some Jira instances, a sizeable portion of the JSON is made up of empty fields.
- #deprecated(message:, date:, depth: 2) ⇒ Object
- #dir_exist?(path) ⇒ Boolean
- #error(message, more: nil) ⇒ Object
- #file_exist?(filename) ⇒ Boolean
- #foreach(root, &block) ⇒ Object
-
#initialize ⇒ FileSystem
constructor
A new instance of FileSystem.
-
#load(filename, supress_deprecation: false) ⇒ Object
Effectively the same as File.read except it forces the encoding to UTF-8.
- #load_json(filename, fail_on_error: true) ⇒ Object
- #log(message, more: nil, also_write_to_stderr: false) ⇒ Object
- #mkdir(path) ⇒ Object
- #save_file(content:, filename:) ⇒ Object
- #save_json(json:, filename:) ⇒ Object
- #unlink(filename) ⇒ Object
- #utime(file:, time:) ⇒ Object
- #warning(message, more: nil) ⇒ Object
Constructor Details
#initialize ⇒ FileSystem
Returns a new instance of FileSystem.
8 9 10 11 12 13 |
# File 'lib/jirametrics/file_system.rb', line 8 def initialize # In almost all cases, this will be immediately replaced in the Exporter # but if we fail before we get that far, this will at least let a useful # error show up on the console. @logfile = $stdout end |
Instance Attribute Details
#logfile ⇒ Object
Returns the value of attribute logfile.
6 7 8 |
# File 'lib/jirametrics/file_system.rb', line 6 def logfile @logfile end |
#logfile_name ⇒ Object
Returns the value of attribute logfile_name.
6 7 8 |
# File 'lib/jirametrics/file_system.rb', line 6 def logfile_name @logfile_name end |
Instance Method Details
#compress(node) ⇒ Object
In some Jira instances, a sizeable portion of the JSON is made up of empty fields. I’ve seen cases where this simple compression will drop the filesize by half.
69 70 71 72 73 74 75 76 77 |
# File 'lib/jirametrics/file_system.rb', line 69 def compress node if node.is_a? Hash node.reject! { |_key, value| value.nil? || (value.is_a?(Array) && value.empty?) } node.each_value { |value| compress value } elsif node.is_a? Array node.each { |a| compress a } end node end |
#deprecated(message:, date:, depth: 2) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/jirametrics/file_system.rb', line 95 def deprecated message:, date:, depth: 2 text = +'' text << "Deprecated(#{date}): " text << caller(1..depth).each do |line| text << "\n-> Called from #{line}" end log text, also_write_to_stderr: true end |
#dir_exist?(path) ⇒ Boolean
87 88 89 |
# File 'lib/jirametrics/file_system.rb', line 87 def dir_exist? path File.exist?(path) && File.directory?(path) end |
#error(message, more: nil) ⇒ Object
53 54 55 |
# File 'lib/jirametrics/file_system.rb', line 53 def error , more: nil log "Error: #{}", more: more, also_write_to_stderr: true end |
#file_exist?(filename) ⇒ Boolean
83 84 85 |
# File 'lib/jirametrics/file_system.rb', line 83 def file_exist? filename File.exist?(filename) && File.file?(filename) end |
#foreach(root, &block) ⇒ Object
79 80 81 |
# File 'lib/jirametrics/file_system.rb', line 79 def foreach root, &block Dir.foreach root, &block end |
#load(filename, supress_deprecation: false) ⇒ Object
Effectively the same as File.read except it forces the encoding to UTF-8
16 17 18 19 20 21 22 |
# File 'lib/jirametrics/file_system.rb', line 16 def load filename, supress_deprecation: false if filename.end_with?('.json') && !supress_deprecation deprecated(message: 'call load_json instead', date: '2024-11-13') end File.read filename, encoding: 'UTF-8' end |
#load_json(filename, fail_on_error: true) ⇒ Object
24 25 26 27 28 |
# File 'lib/jirametrics/file_system.rb', line 24 def load_json filename, fail_on_error: true return nil if fail_on_error == false && File.exist?(filename) == false JSON.parse load(filename, supress_deprecation: true) end |
#log(message, more: nil, also_write_to_stderr: false) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/jirametrics/file_system.rb', line 57 def log , more: nil, also_write_to_stderr: false += " See #{logfile_name} for more details about this message." if more logfile.puts logfile.puts more if more return unless also_write_to_stderr $stderr.puts # rubocop:disable Style/StderrPuts end |
#mkdir(path) ⇒ Object
41 42 43 |
# File 'lib/jirametrics/file_system.rb', line 41 def mkdir path FileUtils.mkdir_p path end |
#save_file(content:, filename:) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/jirametrics/file_system.rb', line 34 def save_file content:, filename: file_path = File.dirname(filename) FileUtils.mkdir_p file_path unless File.exist?(file_path) File.write(filename, content) end |
#save_json(json:, filename:) ⇒ Object
30 31 32 |
# File 'lib/jirametrics/file_system.rb', line 30 def save_json json:, filename: save_file content: JSON.pretty_generate(compress json), filename: filename end |
#unlink(filename) ⇒ Object
91 92 93 |
# File 'lib/jirametrics/file_system.rb', line 91 def unlink filename File.unlink filename end |
#utime(file:, time:) ⇒ Object
45 46 47 |
# File 'lib/jirametrics/file_system.rb', line 45 def utime file:, time: File.utime time, time, file end |
#warning(message, more: nil) ⇒ Object
49 50 51 |
# File 'lib/jirametrics/file_system.rb', line 49 def warning , more: nil log "Warning: #{}", more: more, also_write_to_stderr: true end |