Class: Generator
- Inherits:
-
Object
- Object
- Generator
- Defined in:
- lib/generator.rb
Instance Attribute Summary collapse
-
#dummy_name ⇒ Object
readonly
Returns the value of attribute dummy_name.
-
#script_name ⇒ Object
readonly
Returns the value of attribute script_name.
Instance Method Summary collapse
- #after_generate ⇒ Object
- #extract_data(type, regex) ⇒ Object
- #generate_filebody(filename) ⇒ Object
-
#initialize(script_name) ⇒ Generator
constructor
A new instance of Generator.
- #result_directories ⇒ Object
- #result_files ⇒ Object
- #run_generator! ⇒ Object
- #template_directories ⇒ Object
- #template_files ⇒ Object
- #template_paths ⇒ Object
- #write(files) ⇒ Object
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_name ⇒ Object (readonly)
Returns the value of attribute dummy_name.
7 8 9 |
# File 'lib/generator.rb', line 7 def dummy_name @dummy_name end |
#script_name ⇒ Object (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_generate ⇒ Object
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_directories ⇒ Object
30 31 32 |
# File 'lib/generator.rb', line 30 def result_directories extract_data :directories, /^(.+)\/lib\/\.\.\/(.+)\// end |
#result_files ⇒ Object
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_directories ⇒ Object
14 15 16 |
# File 'lib/generator.rb', line 14 def template_directories template_paths.select { |f| File.directory? f } end |
#template_files ⇒ Object
18 19 20 |
# File 'lib/generator.rb', line 18 def template_files template_paths.reject { |f| File.directory? f } end |
#template_paths ⇒ Object
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 |