Class: Inversion::Template::AttrTag

Inherits:
CodeTag show all
Defined in:
lib/inversion/template/attrtag.rb

Overview

Inversion attribute tag.

Attribute tags add an accessor to a template like ‘attr_accessor’ does for Ruby classes.

Syntax

<?attr foo ?>
<?attr "%0.2f" % foo ?>

Constant Summary

Constants inherited from Tag

Tag::TAG_PLUGIN_PATTERN

Instance Attribute Summary collapse

Attributes inherited from CodeTag

#body, #identifiers

Attributes inherited from Tag

#body

Attributes inherited from Node

#colnum, #linenum

Instance Method Summary collapse

Methods inherited from CodeTag

inherit_tag_patterns, tag_pattern, tag_patterns

Methods included from AbstractClass

included

Methods included from AbstractClass::ClassMethods

#inherited, #pure_virtual

Methods inherited from Tag

create, #derivatives, inherited, 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, #before_appending, #before_rendering, #is_container?, #location

Constructor Details

#initialize(body, linenum = nil, colnum = nil) ⇒ AttrTag

Create a new AttrTag with the given name, which should be a valid Ruby identifier. The linenum and colnum should be the line and column of the tag in the template source, if available.



55
56
57
58
59
60
61
62
63
64
# File 'lib/inversion/template/attrtag.rb', line 55

def initialize( body, linenum=nil, colnum=nil )
  @name        = nil
  @format      = nil
  @methodchain = nil

  super

  # Add an identifier for the tag name
  self.identifiers << self.name.to_sym
end

Instance Attribute Details

#formatObject

the format string used to format the attribute in the template (if one was declared)



76
77
78
# File 'lib/inversion/template/attrtag.rb', line 76

def format
  @format
end

#methodchainObject

the chain of methods that should be called (if any).



79
80
81
# File 'lib/inversion/template/attrtag.rb', line 79

def methodchain
  @methodchain
end

#nameObject

the name of the attribute



72
73
74
# File 'lib/inversion/template/attrtag.rb', line 72

def name
  @name
end

Instance Method Details

#as_comment_bodyObject

Render the tag as the body of a comment, suitable for template debugging.



103
104
105
106
107
108
109
110
# File 'lib/inversion/template/attrtag.rb', line 103

def as_comment_body
  comment = "%s: { template.%s" % [ self.tagname, self.name ]
  comment << self.methodchain if self.methodchain
  comment << " }"
  comment << " with format: %p" % [ self.format ] if self.format

  return comment
end

#evaluate(renderstate) ⇒ Object

Evaluate the body of the tag in the context of renderstate and return the results.



96
97
98
99
# File 'lib/inversion/template/attrtag.rb', line 96

def evaluate( renderstate )
  code = [ self.name.to_s, self.methodchain ].join( '' )
  return renderstate.eval( code )
end

#render(renderstate) ⇒ Object

Render the tag attributes of the specified renderstate and return them.



83
84
85
86
87
88
89
90
91
92
# File 'lib/inversion/template/attrtag.rb', line 83

def render( renderstate )
  value = self.evaluate( renderstate ) # :FIXME: or return value # nil or false?

  # Apply the format if there is one
  if self.format && value
    return self.format % value
  else
    return value
  end
end