class FlameGenerateToys::Template::Generator

Class for code generation

Attributes

camelized_name[R]

Public Class Methods

new(type, name, namespace, context_directory) click to toggle source
# File lib/flame_generate_toys/template/generator.rb, line 21
def initialize(type, name, namespace, context_directory)
        @template = self.class.template(type)

        @camelized_name = name.camelize

        @namespace = namespace

        @relative_file_path = "#{type.to_s.pluralize}/#{name}.rb"

        @file_path = "#{context_directory}/#{@relative_file_path}"
        return unless File.exist?(@file_path)

        raise ArgumentError, "File #{name} already exists"
end
template(type) click to toggle source
# File lib/flame_generate_toys/template/generator.rb, line 11
def template(type)
        (@templates ||= {})[type] ||=
                ERB.new File.read "#{__dir__}/#{type}/template.rb.erb"
end

Public Instance Methods

write(**locals) click to toggle source
# File lib/flame_generate_toys/template/generator.rb, line 36
def write(**locals)
        FileUtils.mkdir_p File.dirname @file_path
        File.write @file_path, @template.result_with_hash(
                camelized_name: camelized_name,
                namespace: @namespace,
                **locals
        )

        puts "#{@relative_file_path} created"
end