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.



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

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)



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

def format
  @format
end

#methodchainObject

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



81
82
83
# File 'lib/inversion/template/attrtag.rb', line 81

def methodchain
  @methodchain
end

#nameObject

the name of the attribute



74
75
76
# File 'lib/inversion/template/attrtag.rb', line 74

def name
  @name
end

Instance Method Details

#as_comment_bodyObject

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



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

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.



98
99
100
101
# File 'lib/inversion/template/attrtag.rb', line 98

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.



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

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