Class: Generamba::TemplateCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/generamba/template/creator/template_creator.rb

Overview

Responsible for generating new .rambaspec files

Constant Summary collapse

NEW_TEMPLATE_FOLDER =
'new_template'
RAMBASPEC_TEMPLATE_NAME =
'template.rambaspec.liquid'
CODE_FOLDER =
'Code'
TESTS_FOLDER =
'Tests'

Instance Method Summary collapse

Instance Method Details

#create_template(properties) ⇒ Void

Generates and saves to filesystem a new template with a .rambaspec file and sample code and tests files

Parameters:

  • properties (Hash)

    User-inputted template properties

Returns:

  • (Void)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/generamba/template/creator/template_creator.rb', line 15

def create_template(properties)
  template_dir_path = Pathname.new(File.dirname(__FILE__)).join(NEW_TEMPLATE_FOLDER)
  rambaspec_template_file_path = template_dir_path.join(RAMBASPEC_TEMPLATE_NAME)
  code_file_path = template_dir_path.join(CODE_FOLDER)
  tests_file_path = template_dir_path.join(TESTS_FOLDER)

  file_source = IO.read(rambaspec_template_file_path)

  template = Liquid::Template.parse(file_source)
  output = template.render(properties)

  result_name = properties[TEMPLATE_NAME_KEY] + RAMBASPEC_EXTENSION
  result_dir_path = Pathname.new(properties[TEMPLATE_NAME_KEY])

  FileUtils.mkdir_p result_dir_path
  FileUtils.cp_r(code_file_path, result_dir_path)
  FileUtils.cp_r(tests_file_path, result_dir_path)

  File.open(result_dir_path.join(result_name), 'w+') {|f|
    f.write(output)
  }
end