class PrettySeconds::Converter

Constants

HOUR_SCALE
KEEP_ZERO_CONFIGS
MINUTE_SCALE
TRUNCATE_CONFIGS

Attributes

keep_zero_config[R]
nil_is_zero_config[R]
padding_config[R]
truncate_config[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/pretty_seconds/converter.rb, line 3
def initialize(opts = {})
  @truncate_config    = (opts.fetch(:truncate) { :round } || :disabled).to_sym
  @padding_config     = opts.fetch(:padding) { true } # WIP
  @keep_zero_config   = (opts.fetch(:keep_zero) { :minute } || :disabled).to_sym
  @nil_is_zero_config = true # WIP

  validate_config!
end

Public Instance Methods

convert(number) click to toggle source
# File lib/pretty_seconds/converter.rb, line 12
def convert(number)
  targets = []

  return if number.nil? && !nil_is_zero_config

  if number
    hour = (number / HOUR_SCALE).round
    rest = (number % HOUR_SCALE)
    min = (rest / MINUTE_SCALE).round
    second = rest % MINUTE_SCALE
  else
    hour = 0
    min = 0
    second = 0
  end

  if hour != 0 || (hour == 0 && keep_zero?(:hour))
    targets << hour
  end

  if min != 0 || (min == 0 && second == 0 && keep_zero?(:hour, :minute))
    targets << min
  elsif min == 0 && second != 0 && keep_zero?(:hour, :minute)
    targets << min
  else
    targets << 0 if keep_zero?(:hour, :minute)
  end

  if truncate_config == :round
    second = second.round
  elsif truncate_config == :floor
    second = second.floor
  else
    second = second.round(2) # disable
  end

  targets << pad(second)

  targets.join(':')
end

Private Instance Methods

keep_zero?(*styles) click to toggle source
# File lib/pretty_seconds/converter.rb, line 63
def keep_zero?(*styles)
  styles.any? { |style| style == keep_zero_config }
end
pad(num) click to toggle source
# File lib/pretty_seconds/converter.rb, line 67
def pad(num)
  return num if num >= 10

  "0#{num}"
end
validate_config!() click to toggle source
# File lib/pretty_seconds/converter.rb, line 76
def validate_config!
  unless KEEP_ZERO_CONFIGS.include?(keep_zero_config)
    raise ArgumentError, "Keep zero config invalid: available configs are #{KEEP_ZERO_CONFIGS}"
  end

  unless TRUNCATE_CONFIGS.include?(truncate_config)
    raise ArgumentError, "Truncate config invalid: available configs are #{TRUNCATE_CONFIGS}"
  end
end