Class: RubySpriter::Utils::FileHelper
- Inherits:
-
Object
- Object
- RubySpriter::Utils::FileHelper
- Defined in:
- lib/ruby_spriter/utils/file_helper.rb
Overview
File naming and size utilities
Class Method Summary collapse
-
.ensure_unique_output(path, overwrite: false) ⇒ String
Ensure output filename is unique based on overwrite option.
-
.format_size(bytes) ⇒ String
Format file size in human-readable format.
-
.output_filename(input_file, suffix) ⇒ String
Generate output filename with suffix.
-
.spritesheet_filename(video_file) ⇒ String
Generate spritesheet filename from video file.
-
.unique_filename(path) ⇒ String
Generate unique filename by adding timestamp if file exists.
-
.validate_exists!(path) ⇒ Object
Validate file exists.
-
.validate_readable!(path) ⇒ Object
Validate file is readable.
Class Method Details
.ensure_unique_output(path, overwrite: false) ⇒ String
Ensure output filename is unique based on overwrite option
73 74 75 76 77 78 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 73 def ensure_unique_output(path, overwrite: false) return path if overwrite return path unless File.exist?(path) unique_filename(path) end |
.format_size(bytes) ⇒ String
Format file size in human-readable format
30 31 32 33 34 35 36 37 38 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 30 def format_size(bytes) if bytes >= 1024 * 1024 "#{(bytes / (1024.0 * 1024.0)).round(2)} MB" elsif bytes >= 1024 "#{(bytes / 1024.0).round(2)} KB" else "#{bytes} bytes" end end |
.output_filename(input_file, suffix) ⇒ String
Generate output filename with suffix
21 22 23 24 25 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 21 def output_filename(input_file, suffix) dir = File.dirname(input_file) basename = File.basename(input_file, '.*') File.join(dir, "#{basename}-#{suffix}.png") end |
.spritesheet_filename(video_file) ⇒ String
Generate spritesheet filename from video file
11 12 13 14 15 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 11 def spritesheet_filename(video_file) dir = File.dirname(video_file) basename = File.basename(video_file, '.*') File.join(dir, "#{basename}_spritesheet.png") end |
.unique_filename(path) ⇒ String
Generate unique filename by adding timestamp if file exists
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 58 def unique_filename(path) return path unless File.exist?(path) dir = File.dirname(path) ext = File.extname(path) basename = File.basename(path, ext) = Time.now.strftime('%Y%m%d_%H%M%S_%3N') # Include milliseconds File.join(dir, "#{basename}_#{timestamp}#{ext}") end |
.validate_exists!(path) ⇒ Object
Validate file exists
43 44 45 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 43 def validate_exists!(path) raise ValidationError, "File not found: #{path}" unless File.exist?(path) end |
.validate_readable!(path) ⇒ Object
Validate file is readable
50 51 52 53 |
# File 'lib/ruby_spriter/utils/file_helper.rb', line 50 def validate_readable!(path) validate_exists!(path) raise ValidationError, "File not readable: #{path}" unless File.readable?(path) end |