Class: Scaffolder

Inherits:
Object
  • Object
show all
Defined in:
lib/scaffold/scaffolder.rb

Constant Summary collapse

ANY_TEMPLATE =
"any"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ Scaffolder

Returns a new instance of Scaffolder.



8
9
10
11
12
# File 'lib/scaffold/scaffolder.rb', line 8

def initialize shell
  @shell = shell
  @template_machine = TextTemplateMachineFactory.create @shell
  @filename_formatter = FilenameFormatter.new
end

Class Method Details

.current_fileObject



71
72
73
# File 'lib/scaffold/scaffolder.rb', line 71

def self.current_file
 __FILE__
end

Instance Method Details

#as_dotted_list(indentation, items) ⇒ Object



26
27
28
29
30
31
# File 'lib/scaffold/scaffolder.rb', line 26

def as_dotted_list indentation, items
  indent_string = ' '*indentation
  grouped_items = items.group_by{|item| item.split('.').first}
  group_strings = grouped_items.collect{|group| group.last.join ', '}
  "#{indent_string}* " + group_strings.join("\n#{indent_string}* ")
end

#list_templatesObject



14
15
16
17
18
# File 'lib/scaffold/scaffolder.rb', line 14

def list_templates
  templates = @shell.real_dir_entries template_path
  templates.delete ANY_TEMPLATE
  templates.join(', ')
end

#list_templates_as_dotted_list(indentation) ⇒ Object



20
21
22
23
24
# File 'lib/scaffold/scaffolder.rb', line 20

def list_templates_as_dotted_list indentation
  templates = @shell.real_dir_entries template_path
  templates.delete ANY_TEMPLATE
  as_dotted_list indentation, templates
end

#scaffold(template, kata_file) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/scaffold/scaffolder.rb', line 33

def scaffold template, kata_file
  @template_machine.placeholder_values['kata_file.ext'] = kata_file
  @template_machine.placeholder_values['kata_file'] = Filename.new(kata_file).without_extension.to_s
  template = template == '???' ? ANY_TEMPLATE : template
    dir_path = "#{template_path}/#{template}"
    to_copy = @shell.real_dir_entries dir_path
    to_copy.each do |item|
      file_path = "#{dir_path}/#{item}"
      @shell.cp_r file_path, "."
      transform_file_content item
      transform_file_name item
    end
end

#template_pathObject



62
63
64
65
66
67
68
69
# File 'lib/scaffold/scaffolder.rb', line 62

def template_path
  file_path_elements = Scaffolder::current_file.split '/' 
   while file_path_elements.last != 'lib' do
    file_path_elements.pop
   end
   file_path_elements.pop
  (file_path_elements << 'templates').join '/'
end

#transform_file_content(filename) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/scaffold/scaffolder.rb', line 47

def transform_file_content filename
  if @shell.file?(filename)
    content = @shell.read_file filename
    content = @template_machine.render content
    @shell.write_file filename, content
  end
end

#transform_file_name(filename) ⇒ Object



55
56
57
58
59
60
# File 'lib/scaffold/scaffolder.rb', line 55

def transform_file_name filename
new_filename = @template_machine.render filename
if new_filename != filename
  @shell.rename filename, new_filename
end
end