Module: StructuredDataToSql::Format

Defined in:
lib/structured_data_to_sql/format.rb

Class Method Summary collapse

Class Method Details

.format_count(count) ⇒ Object



18
19
20
# File 'lib/structured_data_to_sql/format.rb', line 18

def format_count(count)
  count.to_i.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
end

.format_duration(seconds) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/structured_data_to_sql/format.rb', line 22

def format_duration(seconds)
  seconds = seconds.to_f
  return format("%.1fs", seconds) if seconds < 60

  total = seconds.round
  hours, remainder = total.divmod(3600)
  minutes, secs = remainder.divmod(60)
  return format("%dh%02dm", hours, minutes) if hours.positive?

  format("%dm%02ds", minutes, secs)
end

.format_rate(bytes, seconds) ⇒ Object



34
35
36
37
38
39
# File 'lib/structured_data_to_sql/format.rb', line 34

def format_rate(bytes, seconds)
  seconds = seconds.to_f
  return nil if seconds <= 0

  "#{format_size(bytes.to_f / seconds)}/s"
end

.format_size(size) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/structured_data_to_sql/format.rb', line 7

def format_size(size)
  size = size.to_i
  if size >= 1024 * 1024 * 1024
    return format("%.1f GB", size / 1024.0 / 1024.0 / 1024.0)
  end
  return format("%.1f MB", size / 1024.0 / 1024.0) if size >= 1024 * 1024
  return format("%.1f KB", size / 1024.0) if size >= 1024

  "#{size} B"
end