Class: Sbuilder::Template

Inherits:
TemplateRoot show all
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/mustache/template.rb

Constant Summary collapse

PROGNAME =
nil

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Attributes inherited from TemplateRoot

#reader

Called from 'partial' -method collapse

private collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Methods inherited from TemplateRoot

#is_extension_point, #partial

Constructor Details

#initialize(reader, options = {}) ⇒ Template


Constructor



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sbuilder/mustache/template.rb', line 18

def initialize( reader, options={} )
  super( reader, options )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__method__} created" )
  @logger.debug( "#{__method__}, options='#{options}" )

  # init partial cache
  @partials = {}

  # inidirection data init to empty hash
  setData( {} )
  

end

Instance Attribute Details

#partials=(value) ⇒ Object (writeonly)

instance



14
15
16
# File 'lib/sbuilder/mustache/template.rb', line 14

def partials=(value)
  @partials = value
end

Instance Method Details

#dataObject

using lambda function



111
112
113
# File 'lib/sbuilder/mustache/template.rb', line 111

def data 
  return @data
end

#get_partial(name) ⇒ Object

Intercept chain to read template file, and cache @partials.

Delegates actual read operation to ‘get_template’ in parent class reader.

Parameters:

  • name (String)

    name of partial template to include



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sbuilder/mustache/template.rb', line 127

def get_partial( name ) 
  @logger.debug( "#{__method__} name=#{name}" )
  # return indirection for 'name[2..-1]) ' if name[0..1]  == '!!'
  name = resolve_partial_name(name)      
  return @partials[name] if @partials[name]
  @logger.info( "#{__method__} read partial_file=#{name}" )
  
  @partials[name] = get_template( name )
  return @partials[name]

end

#render_str(template, data) ⇒ Object

Parameters:

  • template (String)

    mustache template used to render

  • data (Hash)

    data to render



98
99
100
# File 'lib/sbuilder/mustache/template.rb', line 98

def render_str( template, data )
  render( template, data )
end

#resolve_partial_name(name) ⇒ String

Return name of partial, which normally is simply ‘name’ parameer, but which can be found form ‘@data’ using indirect address following starting exclamation mark.

minus prefixed exclamation

Returns:

  • (String)

    ‘name’ or value from ‘@data’ pointed by name



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

def resolve_partial_name( name )
  return name unless name[0..0]  == '!'
  # remove !, split, and use injected traves to access data
  ret = @data.traverse( *(name[1..-1].split( "." )) )
  if ret.nil?
    raise "              Could not resolve \#{name} in template data\n\n              \#{@data.to_yaml}\n\n    EOS\n  end\n  @logger.info( \"\#{__method__} name=\#{name} --> \#{ret}\" )\n  ret\nend\n" 

#setData(data = {}) ⇒ Object

Parameters:

  • data (Hash) (defaults to: {})

    data being rendered



105
106
107
# File 'lib/sbuilder/mustache/template.rb', line 105

def setData( data={} )
  @data = data
end

#to_str(template_name, data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sbuilder/mustache/template.rb', line 62

def to_str( template_name, data )
  @logger.info( "#{__method__}: template_name=#{template_name}" )
  @logger.debug( "#{__method__}: template_name=#{template_name}" ) if @logger.debug?
  # @logger.debug( "#{__method__}: nodes=#{data.nodes}" )
  
  # parent class keeps tract of data, and get_template may need it
  # for indirection
  setData( data )

  if is_extension_point( template_name ) then
    #
    # Use a dynamically created string to output extension points
    #
    template = extension_template( template_name )
    @logger.debug "#{__method__} template: is extension  #{template_name} -> #{template}"  if @logger.debug?
    
  else

    # reader in parent class returns template to resolve as a
    # string.
    #
    # Normally reads template from file to which 'template_name'
    # resolves.  It may be also an empty string (if reader has
    # decided that 'template_name' should not be shown.
    template = get_template( template_name )
    @logger.debug "#{__method__} template: is not extension  #{template_name} -> #{template}" if @logger.debug?
    # render( template, @data )
  end
  
  # render( template, @data )
  render_str( template, data )
end