Module: Appydave::Tools::Dam::FileHelper
- Defined in:
- lib/appydave/tools/dam/file_helper.rb
Overview
File utility methods for DAM operations Provides reusable file and directory helpers
Class Method Summary collapse
-
.calculate_directory_size(path) ⇒ Integer
Calculate total size of directory in bytes.
-
.format_age(time) ⇒ String
Format time as relative age (e.g., “3d”, “2w”, “1mo”).
-
.format_size(bytes) ⇒ String
Format bytes into human-readable size.
Class Method Details
.calculate_directory_size(path) ⇒ Integer
Calculate total size of directory in bytes
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/appydave/tools/dam/file_helper.rb', line 16 def calculate_directory_size(path) return 0 unless Dir.exist?(path) total = 0 Find.find(path) do |file_path| total += File.size(file_path) if File.file?(file_path) rescue StandardError # Skip files we can't read end total end |
.format_age(time) ⇒ String
Format time as relative age (e.g., “3d”, “2w”, “1mo”)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/appydave/tools/dam/file_helper.rb', line 44 def format_age(time) return 'N/A' if time.nil? seconds = Time.now - time return 'just now' if seconds < 60 minutes = seconds / 60 return "#{minutes.round}m" if minutes < 60 hours = minutes / 60 return "#{hours.round}h" if hours < 24 days = hours / 24 return "#{days.round}d" if days < 7 weeks = days / 7 return "#{weeks.round}w" if weeks < 4 months = days / 30 return "#{months.round}mo" if months < 12 years = days / 365 "#{years.round}y" end |
.format_size(bytes) ⇒ String
Format bytes into human-readable size
31 32 33 34 35 36 37 38 39 |
# File 'lib/appydave/tools/dam/file_helper.rb', line 31 def format_size(bytes) return '0 B' if bytes.zero? units = %w[B KB MB GB TB] exp = (Math.log(bytes) / Math.log(1024)).to_i exp = [exp, units.length - 1].min format('%<size>.1f %<unit>s', size: bytes.to_f / (1024**exp), unit: units[exp]) end |