Sha256: 12a2c0d8b854c6dd880ee617420f83eb848374ddab58416cb807783bf264c66f

Contents?: true

Size: 1.83 KB

Versions: 16

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Doing
  # Chronify array helpers
  class ::Array
    ##
    ## Format [d, h, m] as string
    ##
    ## @accept     [Array] Array of [days, hours, minutes]
    ##
    ## @param      format  [Symbol] The format, :dhm, :hm,
    ##                     :m, :clock, :natural
    ## @return     [String] formatted string
    ##
    def time_string(format: :dhm)
      raise InvalidArgument, 'Invalid array, must be [d,h,m]' unless count == 3

      d, h, m = self
      case format
      when :clock
        format('%<d>02d:%<h>02d:%<m>02d', d: d, h: h, m: m)
      when :dhm
        output = []
        output.push(format('%<d>dd', d: d)) if d.positive?
        output.push(format('%<h>dh', h: h)) if h.positive?
        output.push(format('%<m>dm', m: m)) if m.positive?
        output.join(' ')
      when :hm
        h += d * 24 if d.positive?
        format('%<h> 4dh %<m>02dm', h: h, m: m)
      when :m
        h += d * 24 if d.positive?
        m += h * 60 if h.positive?
        format('%<m> 4dm', m: m)
      when :natural
        human = []
        human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive?
        human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive?
        human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive?
        human.join(', ')
      when :speech
        human = []
        human.push(format('%<d>d %<s>s', d: d, s: 'day'.to_p(d))) if d.positive?
        human.push(format('%<h>d %<s>s', h: h, s: 'hour'.to_p(h))) if h.positive?
        human.push(format('%<m>d %<s>s', m: m, s: 'minute'.to_p(m))) if m.positive?
        last = human.pop
        case human.count
        when 2
          human.join(', ') + ", and #{last}"
        when 1
          "#{human[0]} and #{last}"
        when 0
          last
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
doing-2.1.37 lib/doing/chronify/array.rb
doing-2.1.36 lib/doing/chronify/array.rb
doing-2.1.35 lib/doing/chronify/array.rb
doing-2.1.34 lib/doing/chronify/array.rb
doing-2.1.33 lib/doing/chronify/array.rb
doing-2.1.32 lib/doing/chronify/array.rb
doing-2.1.31pre lib/doing/chronify/array.rb
doing-2.1.30 lib/doing/chronify/array.rb
doing-2.1.29 lib/doing/chronify/array.rb
doing-2.1.28 lib/doing/chronify/array.rb
doing-2.1.27 lib/doing/chronify/array.rb
doing-2.1.26 lib/doing/array_chronify.rb
doing-2.1.25 lib/doing/array_chronify.rb
doing-2.1.24 lib/doing/array_chronify.rb
doing-2.1.23 lib/doing/array_chronify.rb
doing-2.1.22 lib/doing/array_chronify.rb