Sha256: 96844eb8c4cd9994d9a2cbc7f38a8a619f43f67180add7ad9166caf9640a6087

Contents?: true

Size: 1022 Bytes

Versions: 6

Compression:

Stored size: 1022 Bytes

Contents

require 'erb'

module Fulmar
  module Domain
    module Service
      # Renders templates of config files
      class TemplateRenderingService
        def initialize(config)
          @config = config
        end

        def render
          return unless @config[:templates]
          @config[:templates].each do |template_file|
            template = template_path(template_file)

            renderer = ERB.new(File.read(template))
            result_path = File.dirname(template) + '/' + File.basename(template, '.erb')
            File.open(result_path, 'w') { |config_file| config_file.write(renderer.result(binding)) }
          end
        end

        def template_path(template_file)
          template = "#{@config[:local_path]}/#{template_file}"
          fail "Template filenames must end in .erb - '#{template}' does not" unless template[-4, 4] == '.erb'
          fail "Cannot render missing config file '#{template}'" unless File.exist? template
          template
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
fulmar-2.1.2 lib/fulmar/domain/service/template_rendering_service.rb
fulmar-2.1.1 lib/fulmar/domain/service/template_rendering_service.rb
fulmar-2.1.0 lib/fulmar/domain/service/template_rendering_service.rb
fulmar-2.0.2 lib/fulmar/domain/service/template_rendering_service.rb
fulmar-2.0.1 lib/fulmar/domain/service/template_rendering_service.rb
fulmar-2.0.0 lib/fulmar/domain/service/template_rendering_service.rb