class Toycol::TemplateGenerator

Public Class Methods

generate!(type:, name:) click to toggle source
# File lib/toycol/template_generator.rb, line 8
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
new(name, type) click to toggle source
# File lib/toycol/template_generator.rb, line 26
def initialize(name, type)
  @name = name
  @type = type
end

Private Class Methods

valid?(type) click to toggle source
# File lib/toycol/template_generator.rb, line 21
def valid?(type)
  %w[all app protocol].include? type
end

Public Instance Methods

generate!() click to toggle source
# File lib/toycol/template_generator.rb, line 31
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 Instance Methods

filename() click to toggle source
# File lib/toycol/template_generator.rb, line 40
def filename
  @filename ||= case @type
                when "protocol" then "Protocolfile#{@name ? ".#{@name}" : nil}"
                when "app"      then "config#{@name ? "_#{@name}" : nil}.ru"
                end
end
template_text() click to toggle source
# File lib/toycol/template_generator.rb, line 55
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
template_text_for_new() click to toggle source
# File lib/toycol/template_generator.rb, line 47
def template_text_for_new
  if @name
    template_text.sub(":PROTOCOL_NAME", ":#{@name}")
  else
    template_text.sub("\(:PROTOCOL_NAME\)", "")
  end
end