Module: Expansions::ERBTemplateFile

Extended by:
ERBTemplateFile
Included in:
ERBTemplateFile
Defined in:
lib/expansions/erb_template_file.rb

Instance Method Summary collapse

Instance Method Details

#prepare_template(template) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/expansions/erb_template_file.rb', line 5

def prepare_template(template)
  token_regex = /(@\w[\w\.\_]+\w@)/

  hits = template.scan(token_regex)
  tags = hits.map do |item|
    item[0].gsub(/@/,'').squeeze
  end
  tags = tags.map{|tag| tag.to_sym}.uniq

  tags.inject(template) do |text, tag|
    text.gsub /@#{tag.to_s}@/, "<%= #{tag.to_s} %>"
  end
end

#process(args) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/expansions/erb_template_file.rb', line 24

def process(args)
  template = prepare_template(File.read(args[:input]))
  result = process_template(template,args[:binding])

  File.open_for_write(args[:output]) do|file|
    file.write(result)
  end
end

#process_template(template, binding) ⇒ Object



19
20
21
22
# File 'lib/expansions/erb_template_file.rb', line 19

def process_template(template,binding)
  erb = ERB.new(template, 0, "%")
  erb.result(binding)
end