Class: FlameGenerateToys::Template::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/flame_generate_toys/template/generator.rb

Overview

Class for code generation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, name, namespace, context_directory) ⇒ Generator

Returns a new instance of Generator.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#camelized_nameObject (readonly)

Returns the value of attribute camelized_name.



17
18
19
# File 'lib/flame_generate_toys/template/generator.rb', line 17

def camelized_name
  @camelized_name
end

Class Method Details

.template(type) ⇒ Object



11
12
13
14
# 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

Instance Method Details

#write(**locals) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# 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