Class: Twig::CommitTime

Inherits:
Object
  • Object
show all
Defined in:
lib/twig/commit_time.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time) ⇒ CommitTime

Returns a new instance of CommitTime.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Class Method Details

.nowObject



5
6
7
# File 'lib/twig/commit_time.rb', line 5

def self.now
  Time.now
end

Instance Method Details

#<=>(other) ⇒ Object



93
94
95
# File 'lib/twig/commit_time.rb', line 93

def <=>(other)
  to_i <=> other.to_i
end

#count_days_agoObject



58
59
60
61
62
# 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_agoObject



64
65
66
67
68
# 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_agoObject



70
71
72
73
74
# 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_agoObject



47
48
49
50
# 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_agoObject



76
77
78
# File 'lib/twig/commit_time.rb', line 76

def count_seconds_ago
  (CommitTime.now - @time).to_i
end

#count_weeks_agoObject



52
53
54
55
56
# 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_agoObject



41
42
43
44
45
# 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

#iso8601Object



89
90
91
# File 'lib/twig/commit_time.rb', line 89

def iso8601
  @time.iso8601
end

#to_iObject



80
81
82
# File 'lib/twig/commit_time.rb', line 80

def to_i
  @time.to_i
end

#to_sObject



84
85
86
87
# File 'lib/twig/commit_time.rb', line 84

def to_s
  time_string = @time.strftime('%F %R %z')
  "#{time_string} (#{@time_ago})"
end