Class: Grizzled::String::Template::TemplateBase

Inherits:
Object
  • Object
show all
Defined in:
lib/grizzled/string/template.rb

Overview

Base (abstract) class for a string template. Common logic is here. Subclasses implement specific methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver, options = {}) ⇒ TemplateBase

Initializer.

Parameters:

resolver

A hash-like object that can take a variable name (via the [] function) and resolve its value, returning the value (which is converted to string) or nil.

options

hash of options. See below.

Options:

:safe

true for a safe template that substitutes a blank string for a non-existent variable, instead of throwing an exception. Defaults to true.



94
95
96
97
# File 'lib/grizzled/string/template.rb', line 94

def initialize(resolver, options={})
  @resolver = resolver
  @safe = options.fetch(:safe, true)
end

Instance Attribute Details

#resolverObject (readonly)

Returns the value of attribute resolver.



78
79
80
# File 'lib/grizzled/string/template.rb', line 78

def resolver
  @resolver
end

#safeObject (readonly)

Returns the value of attribute safe.



78
79
80
# File 'lib/grizzled/string/template.rb', line 78

def safe
  @safe
end

Instance Method Details

#find_variable_ref(s) ⇒ Object

Parse the location of the first variable in the string. Subclasses should override this method.

Parameters:

s

the string

Returns a Variable object, or nil.



141
142
143
# File 'lib/grizzled/string/template.rb', line 141

def find_variable_ref(s)
  nil
end

#get_variable(name, default) ⇒ Object

Get a variable’s value, returning the empty string or throwing an exception, depending on the setting of safe.

Parameters:

name

Variable name

default

Default value, or nil



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/grizzled/string/template.rb', line 152

def get_variable(name, default)
  
  def handle_no_value(default, name)
    if not default.nil?
      default
    elsif @safe
      ""
    else
      raise VariableNotFoundException.new(name)
    end
  end

  resolver[name] || handle_no_value(default, name)
end

#handle_no_value(default, name) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/grizzled/string/template.rb', line 154

def handle_no_value(default, name)
  if not default.nil?
    default
  elsif @safe
    ""
  else
    raise VariableNotFoundException.new(name)
  end
end

#substitute(s) ⇒ Object

Replace all variable references in the given string. Variable references are recognized per the regular expression passed to the constructor. If a referenced variable is not found in the resolver, this method either:

  • throws a VariableNotFoundException (if safe is false).

  • substitutes an empty string (if safe is true)

Recursive references are supported (but beware of infinite recursion).

Parameters:

s

the string in which to replace variable references

Returns the substituted result.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/grizzled/string/template.rb', line 114

def substitute(s)

  def substitute_variable(var, s)
    end_string = var.iend == s.length ? "" : s[var.iend..-1]
    value = get_variable(var.name, var.default)
    transformed = 
      (var.istart == 0 ? "" : s[0..(var.istart-1)]) + value + end_string
    substitute(transformed)
  end

  # Locate the next variable reference.
  var = find_variable_ref(s)
  if var.nil?
    s
  else
    substitute_variable(var, s)
  end
end

#substitute_variable(var, s) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/grizzled/string/template.rb', line 116

def substitute_variable(var, s)
  end_string = var.iend == s.length ? "" : s[var.iend..-1]
  value = get_variable(var.name, var.default)
  transformed = 
    (var.istart == 0 ? "" : s[0..(var.istart-1)]) + value + end_string
  substitute(transformed)
end