Class: Bauk::Gen::Contents::ErbContent
- Inherits:
-
BaseContent
- Object
- BaseContent
- Bauk::Gen::Contents::ErbContent
- Defined in:
- lib/bauk/gen/contents/erb_content.rb
Constant Summary collapse
- SECTION_START_REGEX =
/BAUK-GEN CUSTOM SECTION ([0-9A-Z_]+) START/
- SECTION_END_REGEX =
/BAUK-GEN CUSTOM SECTION ([0-9A-Z_]+) END/
Instance Method Summary collapse
- #content ⇒ Object
-
#initialize(opts) ⇒ ErbContent
constructor
A new instance of ErbContent.
- #merge(current_content) ⇒ Object
- #merge_json(current_content, template_content) ⇒ Object
- #merge_sections(current_content, template_content) ⇒ Object
Constructor Details
#initialize(opts) ⇒ ErbContent
Returns a new instance of ErbContent.
15 16 17 18 |
# File 'lib/bauk/gen/contents/erb_content.rb', line 15 def initialize(opts) super(opts) @file = opts[:file] end |
Instance Method Details
#content ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# 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) ⇒ Object
54 55 56 57 58 59 60 61 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 94 95 96 97 98 99 100 |
# 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 |