class Inversion::Template::TimeDeltaTag
Inversion
time delta tag.
This tag is a derivative of the ‘attr’ tag that transforms the results of its method call to a Time object (if it isn’t already), and then generates an English description of the different between it and the current time.
Syntax¶ ↑
Updated <?timedelta entry.update_date ?>.
Constants
- DAYS
- HOURS
- MINUTES
- MONTHS
- WEEKS
- YEARS
Public Instance Methods
render( renderstate )
click to toggle source
Render the tag.
Calls superclass method
Inversion::Template::AttrTag#render
# File lib/inversion/template/timedeltatag.rb, line 39 def render( renderstate ) val = super( renderstate ) time = nil omit_decorator = false if val.respond_to?( :key ) val, omit_decorator = val.values_at( :time, :omit_decorator ) end if val.respond_to?( :to_time ) time = val.to_time elsif val.is_a?( Numeric ) time = Time.at( val ) else time = Time.parse( val.to_s ) end now = Time.now if now > time seconds = now - time period = timeperiod( seconds ) period += ' ago' unless omit_decorator return period else seconds = time - now period = timeperiod( seconds ) period += ' from now' unless omit_decorator return period end end
Private Instance Methods
timeperiod( seconds )
click to toggle source
Return a string describing ‘seconds` as an approximate interval of time.
# File lib/inversion/template/timedeltatag.rb, line 76 def timeperiod( seconds ) return case when seconds < MINUTES - 5 'less than a minute' when seconds < 50 * MINUTES if seconds <= 89 "a minute" else "%d minutes" % [ (seconds.to_f / MINUTES).ceil ] end when seconds < 90 * MINUTES 'about an hour' when seconds < 18 * HOURS "%d hours" % [ (seconds.to_f / HOURS).ceil ] when seconds < 30 * HOURS 'about a day' when seconds < WEEKS "%d days" % [ (seconds.to_f / DAYS).ceil ] when seconds < 2 * WEEKS 'about a week' when seconds < 3 * MONTHS "%d weeks" % [ (seconds.to_f / WEEKS).ceil ] when seconds < 18 * MONTHS "%d months" % [ (seconds.to_f / MONTHS).ceil ] else "%d years" % [ (seconds.to_f / YEARS).ceil ] end end