Sha256: 514761821f97dbc040716ad8937861ed1a64515de383975f74dc17c4f697b682

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

require 'fileutils'
require 'erb'

module PicoApi
  module Generators
    class FileCreator
      def self.build(project_name)
        project_name_data_mapper = ProjectNameDataMapper.new(project_name)

        new(project_name_data_mapper)
      end

      def initialize(project_name_data_mapper)
        @project_name_data_mapper = project_name_data_mapper
      end

      def create(template_file_path, destination_path)
        File.open("#{snakecased_name}#{destination_path}", 'w') do |file|
          file.write(template(template_file_path))
        end
      end

      private

      attr_reader :project_name_data_mapper

      def snakecased_name
        project_name_data_mapper.snakecased
      end

      def template(template_file_path)
        erb(template_file_path).result(project_name_data_mapper.get_binding)
      end

      def erb(template_file_path)
        source_file = File.read(template_full_file_path(template_file_path))
        ERB.new(source_file)
      end

      def template_full_file_path(template_relative_path)
        File.join(PicoApi.lib_path, template_relative_path)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pico_api-0.1.0 lib/pico_api/generators/file_creator.rb