Class: RegoValidate::Profiler::ByteFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/cli.rb

Overview

Formats byte sizes for profiler output.

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ ByteFormatter

Returns a new instance of ByteFormatter.



538
539
540
541
# File 'lib/ruby/rego/cli.rb', line 538

def initialize(bytes)
  @sign = bytes.negative? ? "-" : "+"
  @size = bytes.abs
end

Instance Method Details

#renderObject



543
544
545
546
547
548
549
550
551
552
# File 'lib/ruby/rego/cli.rb', line 543

def render
  unit, value = if size < 1024
                  ["B", size.to_s]
                elsif size < 1024 * 1024
                  ["KB", Kernel.format("%.2f", size / 1024.0)]
                else
                  ["MB", Kernel.format("%.2f", size / (1024.0 * 1024.0))]
                end
  "#{sign}#{value} #{unit}"
end