Class: RubySpriter::Utils::FileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_spriter/utils/file_helper.rb

Overview

File naming and size utilities

Class Method Summary collapse

Class Method Details

.ensure_unique_output(path, overwrite: false) ⇒ String

Ensure output filename is unique based on overwrite option

Parameters:

  • path (String)

    Desired output path

  • overwrite (Boolean) (defaults to: false)

    If true, return original path; if false, make unique

Returns:

  • (String)

    Output path (unique if overwrite is false and file exists)



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

Parameters:

  • bytes (Integer)

    File size in bytes

Returns:

  • (String)

    Formatted file size



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

Parameters:

  • input_file (String)

    Original input file

  • suffix (String)

    Suffix to add to filename

Returns:

  • (String)

    Generated output filename



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

Parameters:

  • video_file (String)

    Path to video file

Returns:

  • (String)

    Generated spritesheet filename



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

Parameters:

  • path (String)

    Original file path

Returns:

  • (String)

    Unique file path (adds timestamp if original 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)
  timestamp = 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

Parameters:

  • path (String)

    File path to validate

Raises:



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

Parameters:

  • path (String)

    File path to validate

Raises:



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