Class: Sbuilder::TemplateReader

Inherits:
Object
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/sbuilder/mustache/template_reader.rb

Direct Known Subclasses

TemplateContextReader

Constant Summary collapse

PROGNAME =

progname for logger

"treader"

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(controller, options = {}) ⇒ TemplateReader


Constructor



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sbuilder/mustache/template_reader.rb', line 28

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

  # use default templates - prefix with user specified templates
  @template_paths = options[:default_templates]
  # for mustache templates
  if options[:templates] then
    @template_paths = options[:templates] + (options[:default_templates] || [] )
  end
  
  # # init partial cache
  # @partials = {}

end

Instance Attribute Details

#partials=(value) ⇒ Object (writeonly)

f: partial-name –> template string



22
23
24
# File 'lib/sbuilder/mustache/template_reader.rb', line 22

def partials=(value)
  @partials = value
end

#template_pathsObject (readonly)

instance



21
22
23
# File 'lib/sbuilder/mustache/template_reader.rb', line 21

def template_paths
  @template_paths
end

#templates=(value) ⇒ Object (writeonly)

f: template-name –> template string



23
24
25
# File 'lib/sbuilder/mustache/template_reader.rb', line 23

def templates=(value)
  @templates = value
end

Instance Method Details

#filtered_templateString

default template when filtered

Returns:

  • (String)

    emptpy string for filtered templtae



65
66
67
# File 'lib/sbuilder/mustache/template_reader.rb', line 65

def filtered_template
  ""
end

#get_template(name) ⇒ String

Check whether template ‘name’ should be be shown, next resolve location of a template file, and do ‘File.read’ on the file path

Returns:

  • (String)

    for template content



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sbuilder/mustache/template_reader.rb', line 85

def get_template( name )

  if !show_template( name )
    @logger.info( "#{__method__} filtered, name=#{name}" )
    return filtered_template
  end

  template_file = get_template_filepath( name )
  @logger.info( "#{__method__} read template_file=#{template_file}" )
  #File.read( template_file )
  template_read( template_file )

end

#get_template_filepath(name) ⇒ Object

return path to an existing template file name



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sbuilder/mustache/template_reader.rb', line 110

def get_template_filepath( name ) 

  @template_paths.each do |directory_or_gemname|
    
    template_path = get_template_filepath_resolve( directory_or_gemname, name ) 
    return template_path if template_path && File.exists?( template_path ) 

  end # each 


  # could not find 
  
  raise "\n      No such template '\#{name}' found in directories: \#{@template_paths.join(\", \")}\n    \n    eos\n  \nend\n"

#show_template(name) ⇒ Boolean

To override in sub-classes

Returns:

  • (Boolean)

    true - show allways



72
73
74
# File 'lib/sbuilder/mustache/template_reader.rb', line 72

def show_template( name )
  true
end

#template_read(path) ⇒ String

Returns for template content.

Parameters:

  • path (String)

    path name to template

Returns:

  • (String)

    for template content



101
102
103
# File 'lib/sbuilder/mustache/template_reader.rb', line 101

def template_read( path )
  File.read( path  )
end