Class: AwsMust::Template

Inherits:
Mustache
  • Object
show all
Includes:
Utils::MyLogger
Defined in:
lib/aws-must/template.rb

Constant Summary collapse

PROGNAME =

progname for logger

"template"
@@template_path =

static

nil
@@template_extension =

directory where templates stored

"mustache"

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MyLogger

#getLogger

Constructor Details

#initialize(options = {}) ⇒ Template


Constructor



26
27
28
29
30
31
32
# File 'lib/aws-must/template.rb', line 26

def initialize( options={} )
  @logger = getLogger( PROGNAME, options )
  @logger.info( "#{__FILE__}.#{__method__} created" )
  @logger.debug( "#{__FILE__}.#{__method__}, options='#{options}" )
  # for mustache templates
  @@template_path = options[:template_path] if options[:template_path]
end

Instance Attribute Details

#partials=(value) ⇒ Object (writeonly)

instance



20
21
22
# File 'lib/aws-must/template.rb', line 20

def partials=(value)
  @partials = value
end

#templates=(value) ⇒ Object (writeonly)

f: template-name –> template string



21
22
23
# File 'lib/aws-must/template.rb', line 21

def templates=(value)
  @templates = value
end

Instance Method Details

#get_partial(name) ⇒ Object

cache @partials - for easier extension



58
59
60
61
62
63
64
65
# File 'lib/aws-must/template.rb', line 58

def get_partial( name ) 
  @logger.debug( "#{__FILE__}.#{__method__} name=#{name}" )
  return @partials[name] if @partials && @partials[name]

  partial_file = "#{@@template_path}/#{name}.#{template_extension}"
  @logger.info( "#{__FILE__}.#{__method__} read partial_file=#{partial_file}" )
  File.read( partial_file )
end

#get_template(name) ⇒ Object

hide @templates - for easier extension



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
94
95
96
97
98
99
100
# File 'lib/aws-must/template.rb', line 68

def get_template( name ) 

  @logger.debug( "#{__FILE__}.#{__method__} name=#{name}" )
  return @templates[name] if @templates && @templates[name]

  if !  File.exists?( @@template_path ) then
    raise <<-eos


      No such directory '#{@@template_path}'.
 
      Use opition -t to point to an existing directory.
    
    eos
  end

  template_path = "#{@@template_path}/#{name}.#{@@template_extension}"
  @logger.info( "#{__FILE__}.#{__method__} read template_path=#{template_path}" )

  if !  File.exists?( template_path ) then
    raise <<-eos

      No such file  '#{template_path}'.
 
      Use opition -t to point to a directory, which contains file '#{name}.#{@@template_extension}'

    
    eos
  end


  File.read( template_path )
end

#partial(name) ⇒ Object

method used by mustache framework - delegate to ‘get_partial’



51
52
53
54
55
# File 'lib/aws-must/template.rb', line 51

def partial(name)
  @logger.debug( "#{__FILE__}.#{__method__} name=#{name}" )
  # File.read("#{template_path}/#{name}.#{template_extension}")
  get_partial( name )
end

#to_str(template_name, data) ⇒ Object


Services



37
38
39
40
41
42
43
44
45
# File 'lib/aws-must/template.rb', line 37

def to_str( template_name, data )
  @logger.debug( "#{__method__}: template_name=#{template_name}, data=#{data}" )
  if template_name 
    template = get_template( template_name )
    render( template, data )
  else
    render( data )
  end
end