Class: Assert::View::Renderer::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/assert/view/base.rb

Overview

define rendering template class to use for rendering need to overwrite the ‘_’ and ‘__’ meths to add trailing newlines b/c streaming output doesn’t add any whitespace

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Template

Returns a new instance of Template.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/assert/view/base.rb', line 18

def initialize(*args)
  # setup a node stack with the given output obj
  @io = args.pop

  # apply any given data to template scope
  data = args.last.kind_of?(::Hash) ? args.pop : {}
  if (data.keys.map(&:to_s) & self.public_methods.map(&:to_s)).size > 0
    raise ArgumentError, "data conflicts with template public methods."
  end
  metaclass = class << self; self; end
  data.each {|key, value| metaclass.class_eval { define_method(key){value} }}

  # setup a source stack with the given source
  @source = args.pop || Proc.new {}
  instance_eval(&@source)
end

Instance Method Details

#_(data = "", nl = true) ⇒ Object



35
# File 'lib/assert/view/base.rb', line 35

def _(data="", nl=true);  @io << (data.to_s + (nl ? "\n" : "")); end

#__(data = "", nl = true) ⇒ Object



36
# File 'lib/assert/view/base.rb', line 36

def __(data="", nl=true); @io << (data.to_s + (nl ? "\n" : "")); end