Sha256: 3b744d768ab6c94210a84514b5d537de43b13fea46f79bbab0dc253c4df7d7dd

Contents?: true

Size: 927 Bytes

Versions: 14

Compression:

Stored size: 927 Bytes

Contents

require 'erb'

module Buildable::FileMaker
  class << self

    # create plain_text file with content
    #
    # plain_text('test.txt') { |content| content << "hi" }
    # # => create test.txt file with "hi" inside
    def plain_text(filename, options = {})
      # check if exist and be overwrite
      content = yield('')
      File.open(filename, 'w') { |file| file.write content }
    end

    def template(name, location = './', options = {})
      destination = File.join(location, name)
      # check if exist and can overwrite

      template = load_template(name)
      raise "Template #{name} not found" unless template

      builder = ERB.new(template)
      File.open(destination, 'w') { |f| f.write builder.result }
    end

    private

    def load_template(name)
      filename = File.expand_path("../../../templates/#{name}.erb", __FILE__)
      File.read(filename) if File.exist? filename
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
buildable-2.4.1 lib/buildable/file_maker.rb
buildable-2.4.0 lib/buildable/file_maker.rb
buildable-2.3.0 lib/buildable/file_maker.rb
buildable-2.1.6 lib/buildable/file_maker.rb
buildable-2.1.5 lib/buildable/file_maker.rb
buildable-2.1.4 lib/buildable/file_maker.rb
buildable-2.1.3 lib/buildable/file_maker.rb
buildable-2.2.0 lib/buildable/file_maker.rb
buildable-2.1.2 lib/buildable/file_maker.rb
buildable-2.1.1 lib/buildable/file_maker.rb
buildable-2.1.0 lib/buildable/file_maker.rb
buildable-2.0.0 lib/buildable/file_maker.rb
buildable-1.4.2 lib/buildable/file_maker.rb
buildable-1.3.4 lib/buildable/file_maker.rb