Class: Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script_name) ⇒ Generator

Returns a new instance of Generator.



9
10
11
12
# File 'lib/generator.rb', line 9

def initialize(script_name)
  @script_name = script_name.downcase
  @dummy_name = 'my_script'
end

Instance Attribute Details

#dummy_nameObject (readonly)

Returns the value of attribute dummy_name.



7
8
9
# File 'lib/generator.rb', line 7

def dummy_name
  @dummy_name
end

#script_nameObject (readonly)

Returns the value of attribute script_name.



7
8
9
# File 'lib/generator.rb', line 7

def script_name
  @script_name
end

Instance Method Details

#after_generateObject



56
57
58
59
60
61
62
63
64
# File 'lib/generator.rb', line 56

def after_generate
  puts
  puts "#{script_name.green} has been installed in #{(File.join Dir.pwd, script_name).green}"
  puts "To start testing, run following commands"
  puts 
  puts "    cd #{script_name}".blue
  puts "    guard".blue
  puts
end

#extract_data(type, regex) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/generator.rb', line 38

def extract_data(type, regex)
  (__send__ "template_#{type}").map do |t|
    rootpath = File.join(Dir.pwd, script_name) + '/'
    file = t.gsub(regex){ |m| rootpath }.gsub(dummy_name, script_name)
    [] << file << t
  end
end

#generate_filebody(filename) ⇒ Object



26
27
28
# File 'lib/generator.rb', line 26

def generate_filebody(filename)
  ERB.new(IO.read(filename)).result(binding)
end

#result_directoriesObject



30
31
32
# File 'lib/generator.rb', line 30

def result_directories
  extract_data :directories, /^(.+)\/lib\/\.\.\/(.+)\//
end

#result_filesObject



34
35
36
# File 'lib/generator.rb', line 34

def result_files
  extract_data :files, /^(.+)\/lib\/\.\.\/template\//
end

#run_generator!Object



46
47
48
49
50
# File 'lib/generator.rb', line 46

def run_generator!
  write(result_directories) { |f| FileUtils.mkdir_p f.first }
  write(result_files) { |f| IO.write(f.first, generate_filebody(f.last)) }
  after_generate
end

#template_directoriesObject



14
15
16
# File 'lib/generator.rb', line 14

def template_directories
  template_paths.select { |f| File.directory? f }
end

#template_filesObject



18
19
20
# File 'lib/generator.rb', line 18

def template_files
  template_paths.reject { |f| File.directory? f }
end

#template_pathsObject



22
23
24
# File 'lib/generator.rb', line 22

def template_paths
  Dir.glob File.dirname(__FILE__) + "/../template/**/*"
end

#write(files) ⇒ Object



52
53
54
# File 'lib/generator.rb', line 52

def write(files)
  files.each { |f| puts f.first.green; yield f }
end