Class: Inversion::Template::TimeDeltaTag

Inherits:
AttrTag show all
Defined in:
lib/inversion/template/timedeltatag.rb

Overview

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 ?>.

Constant Summary collapse

MINUTES =
60
HOURS =
60  * MINUTES
DAYS =
24  * HOURS
WEEKS =
7   * DAYS
MONTHS =
30  * DAYS
YEARS =
365.25 * DAYS

Constants inherited from Tag

Inversion::Template::Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary

Attributes inherited from AttrTag

#format, #methodchain, #name

Attributes inherited from CodeTag

#body, #identifiers

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

Methods inherited from AttrTag

#as_comment_body, #evaluate, #initialize

Methods inherited from CodeTag

inherit_tag_patterns, #initialize, tag_pattern, tag_patterns

Methods included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

Methods inherited from Tag

#as_comment_body, create, #derivatives, inherited, #initialize, load, load_all, #tagname, types, #types

Methods included from MethodUtilities

#singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer

Methods inherited from Node

#after_appending, #after_rendering, #as_comment_body, #before_appending, #before_rendering, #initialize, #is_container?, #location

Constructor Details

This class inherits a constructor from Inversion::Template::AttrTag

Instance Method Details

#render(renderstate) ⇒ Object

Render the tag.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/inversion/template/timedeltatag.rb', line 41

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