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.



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

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)



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

def format
  @format
end

#literalObject

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



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

def literal
  @literal
end

#methodchainObject

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



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

def methodchain
  @methodchain
end

#nameObject

the name of the attribute



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

def name
  @name
end

Instance Method Details

#as_comment_bodyObject

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



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

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.



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

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.



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

def render( renderstate )
  return ''
end