64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/ago.rb', line 64
def ago(opts={})
focus = opts[:focus] ? opts[:focus] : 0
start_at = opts[:start_at] ? opts[:start_at] : :year
now = opts[:now] ? opts[:now] : Time.now
in_time = opts[:in_time] ? opts[:in_time] : :past
calendar = opts[:calendar] ? opts[:calendar] : :basic
in_time_error = ":in_time => value must be either :past or :future, " \
+ "depending on whether the Time object is before or after Time.now."
unless in_time == :past || in_time == :future
raise ArgumentError, in_time_error
end
Ago.calendar_check(calendar)
base_error = " => value must either be a number " +
"between 0 and 6 (inclusive),\nor one of the following " +
"symbols: " + Valids
{:focus => focus, :start_at => start_at}.each do |key, opt|
opt_error = ":" + key.to_s + base_error
if opt.class == Fixnum
raise ArgumentError, opt_error unless opt >= 0 && opt <= 6
elsif opt.class == Symbol
raise ArgumentError, opt_error unless Ago::Units[opt]
else
raise ArgumentError, opt_error
end
end
frags = []
output = ""
count = 0
now = calendar == :basic ? now.to_i : now.to_f
my_time = calendar == :basic ? self.to_i : self.to_f
if now > my_time
diff = now - my_time
tail = " ago"
elsif my_time > now
diff = my_time - now
tail = " from now"
else
diff = 0
tail = "Right now, this very moment."
end
Ago::Order.each do |u|
if calendar == :gregorian && Ago::Units[u][:gregorian]
value = Ago::Units[u][:gregorian]
else
value = Ago::Units[u][:basic]
end
count += 1
if start_at.class == Fixnum
next if count <= start_at
elsif start_at.class == Symbol
next if Ago::Order.index(u) < Ago::Order.index(start_at)
end
n = (diff/value).floor
if n > 0
plural = n > 1 ? "s" : ""
frags << "#{n} #{u.to_s + plural}"
if focus.class == Symbol
break if u == focus || u == :second
elsif focus.class == Fixnum
if focus == 0 || u == :second
break
else
focus -= 1
end
end
diff -= n * value
end
end
frags.size.times do |n|
output += frags[n]
output += ", " unless n == frags.size - 1
end
return output + "#{tail}"
end
|