Sha256: 04019cfbc210b66cfecacdce5597daa9fd70488fadc7844d9b9ead8bf5ca83b4

Contents?: true

Size: 847 Bytes

Versions: 1

Compression:

Stored size: 847 Bytes

Contents

module TimeLord
  class Period
    attr_writer :beginning, :ending

    def initialize(beginning, ending)
      self.beginning = beginning
      self.ending = ending
    end

    def to_words
      "#{value} #{unit} #{tense}"
    end
    alias_method :in_words, :to_words

    def difference
      beginning - ending
    end
    alias_method :to_i, :difference

    def to_time
      if difference < 0 then @beginning else @ending end
    end

    def to_range
      beginning..ending
    end

    def beginning
      @beginning.to_i
    end

    def ending
      @ending.to_i
    end

    private

    def value
      Scale.new(absolute).to_value
    end

    def unit
      Scale.new(absolute).to_unit
    end

    def absolute
      difference.abs
    end

    def tense
      if difference < 0 then "ago" else "from now" end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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