Sha256: c986aba5c45484145c12c9780b19571afaf1b29d3a2882e035a46d666599bce4

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module Hem
  module Lib
    module Seed
      class Replacer
        def replace(path, tokens, excludes = [])
          unless !tokens.instance_of?(Hash)
            raise "Invalid token list (expected Hash)"
          end

          return search_replace(path, tokens, excludes)
        end

        private

        def search_replace(path, tokens, excludes = [])
          require 'erb'

          template = Template.new(tokens)
          Hem::Helper.locate('*.erb', nil, path: path, type: 'files').each do |candidate|
            next unless FileTest.file? candidate # Skip unless file
            next unless excludes.select { |exclude| File.fnmatch?(exclude, candidate) }.empty?

            content = File.read(candidate)
            next unless content.force_encoding("UTF-8").valid_encoding? # Skip unless file can be valid UTF-8

            content = template.render(content, candidate)

            File.delete(candidate)
            File.write(candidate.sub('.erb', ''), content)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
hem-1.2.4 lib/hem/lib/seed/replacer.rb
hem-1.2.3 lib/hem/lib/seed/replacer.rb
hem-1.2.2 lib/hem/lib/seed/replacer.rb
hem-1.2.1 lib/hem/lib/seed/replacer.rb
hem-1.2.0 lib/hem/lib/seed/replacer.rb