Class: Inversion::Template::DefaultTag

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

Overview

Inversion ‘default’ tag.

The default tag sets the default value of an attribute to a constant, the value of another attribute, or the results of evaluating a methodchain on an attribute.

Syntax

<!-- Set a default width that can be overridden by the controller -->
<?default width to 120 ?>
<?default employees to [] ?>

<!-- Default an attribute to the value of a second attribute -->
<?default content to body ?>

<!-- Set the title to the employee's name if it hasn't been set explicitly -->
<?default title to "%s, %s" % [ employee.lastname, employee.firstname ] ?>

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, #is_container?, #location

Constructor Details

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

Create a new DefaultTag with the given ‘name`, which should be a valid Ruby identifier.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/inversion/template/defaulttag.rb', line 59

def initialize( body, linenum=nil, colnum=nil )
	@name        = nil
	@methodchain = nil
	@literal     = nil
	@format      = 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)



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

def format
  @format
end

#literalObject

The literal, if the tag had one (as opposed to an attribute or methodchain)



84
85
86
# File 'lib/inversion/template/defaulttag.rb', line 84

def literal
  @literal
end

#methodchainObject

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



87
88
89
# File 'lib/inversion/template/defaulttag.rb', line 87

def methodchain
  @methodchain
end

#nameObject

the name of the attribute



77
78
79
# File 'lib/inversion/template/defaulttag.rb', line 77

def name
  @name
end

Instance Method Details

#as_comment_bodyObject

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/inversion/template/defaulttag.rb', line 91

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

	return comment
end

#before_rendering(renderstate) ⇒ Object

Set the specified value (if it’s nil) before rendering.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/inversion/template/defaulttag.rb', line 106

def before_rendering( renderstate )
	existing_value = renderstate.attributes[ self.name.to_sym ]
	unless existing_value.nil?
		self.log.info "Not defaulting %s: already set to %p" %
			[ self.name, existing_value ]
		return nil
	end

	default = nil
	if chain = self.methodchain
		self.log.debug "Using methodchain %p to set default for %p" %
			[ chain, self.name ]
		default = renderstate.eval( 'self' + '.' + self.identifiers.first + chain )
	else
		self.log.debug "Using literal %p to set default for %p" %
			[ self.literal, self.name ]
		default = renderstate.eval( self.literal )
		default = self.format % default if self.format
	end

	self.log.debug "  default value: %p" % [ default ]
	renderstate.attributes[ self.name.to_sym ] = default
end

#render(renderstate) ⇒ Object

Render as the empty string.



132
133
134
# File 'lib/inversion/template/defaulttag.rb', line 132

def render( renderstate )
	return ''
end