Class Tap::Templater
In: lib/tap/templater.rb
Parent: OpenStruct

Templater is a convenience class for creating ERB templates. As a subclass of OpenStruct, attributes can be assigned/unassigned directly. When the template is built, all the method of Templater (and hence all the assigned attributes) are available.

  t = Templater.new( "key: <%= value %>")
  t.value = "default"
  t.build                 # => "key: default"

  t.value = "another"
  t.build                 # => "key: another"

Templater includes the Templater::Utils utility methods.

ERB Redirection

Templater hooks into the ERB templating mechanism by providing itself as the ERB output target (_erbout). ERB concatenates each line of an ERB template to _erbout, as can be seen here:

  e = ERB.new("<%= 1 + 2 %>")
  e.src                   # => "_erbout = ''; _erbout.concat(( 1 + 2 ).to_s); _erbout"

By setting itself as _erbout, instances of Templater can redirect output to a temporary target and perform string transformations. For example, redirection allows indentation of nested content:

  template = %Q{
  # Un-nested content
  <% redirect do |target| %>
  # Nested content
  <% module_nest("Nesting::Module") { target } %>
  <% end %>
  }

  t = Templater.new(template)
  t.build
  # => %Q{
  # # Un-nested content
  # module Nesting
  #   module Module
  #     # Nested content
  #
  #   end
  # end}

Methods

_erbout   _erbout=   build   build   build_file   concat   force_encoding   new   redirect  

Included Modules

Utils

Classes and Modules

Module Tap::Templater::Utils

Public Class methods

Builds the erb template with the specified attributes.

[Source]

     # File lib/tap/templater.rb, line 130
130:       def build(template,  attributes={}, filename=nil)
131:         new(template).build(attributes, filename)
132:       end

Builds the erb template file with the specified attributes.

[Source]

     # File lib/tap/templater.rb, line 135
135:       def build_file(path, attributes={})
136:         self.build(File.read(path), attributes, path)
137:       end

Initialized a new Templater. An ERB or String may be provided as the template. If a String is provided, it will be used to initialize an ERB with a trim_mode of "<>".

[Source]

     # File lib/tap/templater.rb, line 145
145:     def initialize(template, attributes={})
146:       @template = case template
147:       when ERB
148:         # matching w/wo the coding effectively checks @src
149:         # across ruby versions (encoding appears in 1.9)
150:         if template.instance_variable_get(:@src) !~ /^(#coding:US-ASCII\n)?_erbout =/
151:           raise ArgumentError, "Templater does not work with ERB templates where eoutvar != '_erbout'"
152:         end
153:         template
154:       when String then ERB.new(template, nil, "<>")
155:       else raise ArgumentError, "cannot convert #{template.class} into an ERB template"
156:       end
157:       
158:       src = @template.instance_variable_get(:@src)
159:       @template.instance_variable_set(:@src, "self." + src) 
160: 
161:       super(attributes)
162:     end

Public Instance methods

Returns self (not the underlying erbout storage that actually receives the output lines). In the ERB context, this method directs erb outputs to Templater#concat and into the redirect mechanism.

[Source]

     # File lib/tap/templater.rb, line 167
167:     def _erbout
168:       self
169:     end

Sets the underlying erbout storage to input.

[Source]

     # File lib/tap/templater.rb, line 172
172:     def _erbout=(input)
173:       @_erbout = input
174:     end

Build the template, setting the attributes and filename if specified. All methods of self will be accessible in the template.

[Source]

     # File lib/tap/templater.rb, line 204
204:     def build(attrs=nil, filename=nil)
205:       attrs.each_pair do |key, value|
206:         send("#{key}=", value)
207:       end if attrs
208:       
209:       @template.filename = filename
210:       @template.result(binding)
211:       @_erbout
212:     end

Concatenates the specified input to the underlying erbout storage.

[Source]

     # File lib/tap/templater.rb, line 198
198:     def concat(input)
199:       @_erbout << input
200:     end

[Source]

     # File lib/tap/templater.rb, line 179
179:       def force_encoding(encoding)
180:         @_erbout.force_encoding(encoding)
181:         @_erbout
182:       end

Redirects output of erb to the redirected_erbout string for the duration of the block. When redirect completes, the redirected_erbout is concatenated to the main erbout storage.

[Source]

     # File lib/tap/templater.rb, line 189
189:     def redirect # :yields: redirected_erbout
190:       current = @_erbout
191:       @_erbout = ""
192:       result = yield(@_erbout)
193:       @_erbout = current
194:       concat(result)
195:     end

[Validate]