Inversion::Template::

TimeDeltaTag class

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 )

Render the tag.

# 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