Sha256: 855f50620fe9e5a158a7fa2d2b95e4ffe43b821ae5bc8588253d4118afc922cf

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

module TimeLord
  class Scale
    include Units

    attr_accessor :absolute

    def initialize(absolute)
      self.absolute = absolute
    end

    def to_value
      timemap.first
    end

    def to_unit
      timemap.last
    end

    private

    def timemap
      case absolute
      when SECOND...MINUTE then [absolute, pluralized_word("second", plurality?(absolute)) || "second"]
      when MINUTE...HOUR   then as(MINUTE, "minute")
      when HOUR...DAY      then as(HOUR, "hour")
      when DAY...WEEK      then as(DAY, "day")
      when WEEK...MONTH    then as(WEEK, "week")
      when MONTH...YEAR    then as(MONTH, "month")
      else                      as(YEAR, "year")
      end
    end

    def as(unit, word)
      [count(unit), pluralized_word(word, plurality?(count(unit))) || word]
    end

    def count(unit)
      absolute / unit
    end

    def pluralized_word(word, plural)
      word += "s" if plural
    end

    def plurality?(count)
      count > 1 || count.zero?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
time-lord-1.0.1 lib/time-lord/scale.rb