class Twig::CommitTime

Stores a branch's last commit time and its relative time representation.

Public Class Methods

new(time) click to toggle source
# File lib/twig/commit_time.rb, line 9
def initialize(time)
  @time = time
  suffix = 'ago'

  # Cache calculations against current time
  years_ago   = count_years_ago
  months_ago  = count_months_ago
  weeks_ago   = count_weeks_ago
  days_ago    = count_days_ago
  hours_ago   = count_hours_ago
  minutes_ago = count_minutes_ago
  seconds_ago = count_seconds_ago

  @time_ago =
    if years_ago > 0
      "#{years_ago}y"
    elsif months_ago > 0 and weeks_ago > 4
      "#{months_ago}mo"
    elsif weeks_ago > 0
      "#{weeks_ago}w"
    elsif days_ago > 0
      "#{days_ago}d"
    elsif hours_ago > 0
      "#{hours_ago}h"
    elsif minutes_ago > 0
      "#{minutes_ago}m"
    else
      "#{seconds_ago}s"
    end
  @time_ago << ' ' << suffix
end
now() click to toggle source
# File lib/twig/commit_time.rb, line 5
def self.now
  Time.now
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/twig/commit_time.rb, line 93
def <=>(other)
  to_i <=> other.to_i
end
count_days_ago() click to toggle source
# File lib/twig/commit_time.rb, line 58
def count_days_ago
  seconds_in_a_day = 60 * 60 * 24
  seconds = CommitTime.now - @time
  seconds < seconds_in_a_day ? 0 : (seconds / seconds_in_a_day).round
end
count_hours_ago() click to toggle source
# File lib/twig/commit_time.rb, line 64
def count_hours_ago
  seconds_in_an_hour = 60 * 60
  seconds = CommitTime.now - @time
  seconds < seconds_in_an_hour ? 0 : (seconds / seconds_in_an_hour).round
end
count_minutes_ago() click to toggle source
# File lib/twig/commit_time.rb, line 70
def count_minutes_ago
  seconds_in_a_minute = 60
  seconds = CommitTime.now - @time
  seconds < seconds_in_a_minute ? 0 : (seconds / seconds_in_a_minute).round
end
count_months_ago() click to toggle source
# File lib/twig/commit_time.rb, line 47
def count_months_ago
  now = CommitTime.now
  (now.year * 12 + now.month) - (@time.year * 12 + @time.month)
end
count_seconds_ago() click to toggle source
# File lib/twig/commit_time.rb, line 76
def count_seconds_ago
  (CommitTime.now - @time).to_i
end
count_weeks_ago() click to toggle source
# File lib/twig/commit_time.rb, line 52
def count_weeks_ago
  seconds_in_a_week = 60 * 60 * 24 * 7
  seconds = CommitTime.now - @time
  seconds < seconds_in_a_week ? 0 : (seconds / seconds_in_a_week).round
end
count_years_ago() click to toggle source
# File lib/twig/commit_time.rb, line 41
def count_years_ago
  seconds_in_a_year = 60 * 60 * 24 * 365
  seconds = CommitTime.now - @time
  seconds < seconds_in_a_year ? 0 : (seconds / seconds_in_a_year).round
end
iso8601() click to toggle source
# File lib/twig/commit_time.rb, line 89
def iso8601
  @time.iso8601
end
to_i() click to toggle source
# File lib/twig/commit_time.rb, line 80
def to_i
  @time.to_i
end
to_s() click to toggle source
# File lib/twig/commit_time.rb, line 84
def to_s
  time_string = @time.strftime('%F %R %z')
  "#{time_string} (#{@time_ago})"
end