Sha256: ca8f7bad6067df6e185b3c6d97694dc4135de3d47c1ee183ac055d78a1263a3f

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

module Toycol
  class TemplateGenerator
    include Helper

    class << self
      def generate!(type:, name:)
        raise Error, "Unknown Type: This type of template can't be generated" unless valid? type

        if type == "all"
          new(name, "protocol").generate!
          new(name, "app").generate!
        else
          new(name, type).generate!
        end
      end

      private

      def valid?(type)
        %w[all app protocol].include? type
      end
    end

    def initialize(name, type)
      @name = name
      @type = type
    end

    def generate!
      raise Error, "#{filename} already exists" unless Dir.glob(filename).empty?

      File.open(filename, "w") { |f| f.print template_text_for_new }
      logger "Generate #{filename} in #{FileUtils.pwd}"
    end

    private

    def filename
      @filename ||= case @type
                    when "protocol" then "Protocolfile#{@name ? ".#{@name}" : nil}"
                    when "app"      then "config#{@name ? "_#{@name}" : nil}.ru"
                    end
    end

    def template_text_for_new
      if @name
        template_text.sub(":PROTOCOL_NAME", ":#{@name}")
      else
        template_text.sub("\(:PROTOCOL_NAME\)", "")
      end
    end

    def template_text
      case @type
      when "protocol" then File.open("#{__dir__}/templates/protocol.txt",    "r", &:read)
      when "app"      then File.open("#{__dir__}/templates/application.txt", "r", &:read)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toycol-1.0.0 lib/toycol/template_generator.rb
toycol-0.3.1 lib/toycol/template_generator.rb