class Bauk::Gen::Contents::ErbContent

Constants

SECTION_END_REGEX
SECTION_START_REGEX

Public Class Methods

new(opts) click to toggle source
Calls superclass method Bauk::Gen::Contents::BaseContent::new
# File lib/bauk/gen/contents/erb_content.rb, line 15
def initialize(opts)
  super(opts)
  @file = opts[:file]
end

Public Instance Methods

content() click to toggle source
# File lib/bauk/gen/contents/erb_content.rb, line 20
def content
  renderer = ERB.new(File.read(@file))
  erb_binding = OpenStruct.new(@config)
  (((@config[:contents] ||= {})[:erb] ||= {})[:mixins] ||= []).each do |mixin|
    erb_binding.extend(mixin)
  end
  begin
    renderer.result(erb_binding.instance_eval { binding })
  rescue => e
    log.error("ERROR IN FILE: #{@file}")
    throw e
  end
end
merge(current_content) click to toggle source
# File lib/bauk/gen/contents/erb_content.rb, line 34
def merge(current_content)
  if @attributes[:merge] == true
    merge_sections current_content, content
  elsif @attributes[:merge] == "json"
    merge_json current_content, content
  else
    raise "Invalid merge type provided: #{@attributes[:merge]} for template: #{@name}"
  end
end
merge_json(current_content, template_content) click to toggle source
# File lib/bauk/gen/contents/erb_content.rb, line 44
def merge_json(current_content, template_content)
  current_json = JSON.parse(current_content)
  template_json = JSON.parse(template_content)
  if @attributes[:overwrite] == false
    JSON.pretty_generate(template_json.deep_merge!(current_json))
  else
    JSON.pretty_generate(current_json.deep_merge!(template_json))
  end
end
merge_sections(current_content, template_content) click to toggle source
# File lib/bauk/gen/contents/erb_content.rb, line 54
def merge_sections(current_content, template_content)
  sections = {}
  section = nil
  section_no = nil
  current_content.split("\n").each do |line|
    if match = line.match(SECTION_START_REGEX)
      section_no = match.captures[0]
      raise "Section #{section_no} started inside previous section for file: #{@file}" if section
      raise "Section #{section_no} has been defined more than once: #{@file}" if sections[section_no]
      section = []
    elsif match = line.match(SECTION_END_REGEX)
      raise "Section #{match.captures[0]} ended before section started for file: #{@file}" unless section
      raise "Secionn #{match.captures[0]} end block found inside section #{section_no} for file: #{@file}" unless section_no == match.captures[0]
      sections[section_no] = section.join("\n")
      section = nil
    else
      if section
        section << line
      end
    end
  end

  new_content = []
  section_no = nil
  template_content.split("\n").each do |line|
    if match = line.match(SECTION_START_REGEX)
      raise "Section #{match.captures[0]} started inside previous section for template: #{@file}" if section_no
      section_no = match.captures[0]
      new_content << line
    elsif match = line.match(SECTION_END_REGEX)
      raise "Section #{match.captures[0]} ended before section started for template: #{@file}" unless section_no
      raise "Secionn #{match.captures[0]} end block found inside section #{section_no} for template: #{@file}" unless section_no == match.captures[0]
      if sections[section_no]
        new_content.push(sections[section_no])
      else
        log.error "Section #{section_no} not found so replacing with template contents: #{@file}"
      end
      new_content << line
      section_no = nil
    else
      unless section_no and sections[section_no]
        new_content << line
      end
    end
  end
  new_content.join("\n")
end