Sha256: 4bcd3569f47967192357f3b397e6e849a1f9c9c7d234485d50edda7502d0785f
Contents?: true
Size: 1.9 KB
Versions: 1
Compression:
Stored size: 1.9 KB
Contents
# frozen_string_literal: false require_relative '../helper/json_converter' class MemorySize < JSONConverter attr_accessor :kilobytes ZERO_SIZE = 'zero kb'.freeze UNIT = { bytes: 'B', kilobytes: 'KB', megabytes: 'MB', gigabytes: 'GB' }.freeze def bytes @kilobytes * 1024 end def megabytes @kilobytes / 1024 end def gigabytes @kilobytes / 1024 / 1024 end def initialize(text) super() value = parse_from(text) @kilobytes = value || 0 end private def parse_from(text) text_to_memory_unit = { 'b' => :bytes, 'byte' => :bytes, 'bytes' => :bytes, 'kb' => :kilobytes, 'kilobyte' => :kilobytes, 'kilobytes' => :kilobytes, 'mb' => :megabytes, 'megabyte' => :megabytes, 'megabytes' => :megabytes, 'gb' => :gigabytes, 'gigabyte' => :gigabytes, 'gigabytes' => :gigabytes } unit = text_to_memory_unit[parse_units(text)] size = parse_size(text) return nil unless size unit ||= :megabytes case unit when :bytes kilobytes_from_bytes(size) when :kilobytes size when :megabytes kilobytes_from_megabytes(size) when :gigabytes kilobytes_from_gigabytes(size) end end def parse_units(text) return 'kb' if text.downcase == ZERO_SIZE result = '' text.each_char do |char| result << char if char.match?(/[[:alpha:]]/) && char != '.' && char != ',' end result.downcase end def parse_size(text) return 0.to_f if text.downcase == ZERO_SIZE result = '' text.each_char do |char| result << char if char.match?(/[[:digit:]]/) || char == '.' || char == ',' end result.to_f end def kilobytes_from_bytes(bytes) bytes / 1024 end def kilobytes_from_megabytes(megabytes) megabytes * 1024 end def kilobytes_from_gigabytes(gigabytes) gigabytes * 1024 * 1024 end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
danger-app_size_report-0.0.3 | lib/converter/helper/memory_size.rb |