Class: GemStone::Generator
- Inherits:
-
Object
- Object
- GemStone::Generator
- Defined in:
- lib/gemstone.rb
Instance Method Summary collapse
-
#classify(gem_name) ⇒ Object
‘My gem’ => ‘MyGem’.
-
#initialize(options = nil) ⇒ Generator
constructor
A new instance of Generator.
- #render_file(path, result_file_path = path) ⇒ Object
- #result_path(source_path) ⇒ Object
- #template_path ⇒ Object
-
#underscore(gem_name) ⇒ Object
‘My gem’ => ‘my_gem’.
Constructor Details
#initialize(options = nil) ⇒ Generator
Returns a new instance of Generator.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/gemstone.rb', line 62 def initialize( = nil) cli_mode = .nil? ||= CLI. = OptionsValidator.validate!() [:gem_file_name] = underscore([:name]) [:gem_class_name] = classify([:name]) [:gem_path] ||= "./#{options[:gem_file_name]}" %w{ README.md Changelog.md Gemfile RakeFile LICENSE test/test_helper.rb }.each{ |path| render_file path } render_file 'sample.gemspec', "#{@options[:gem_file_name]}.gemspec" render_file 'test/sample_test.rb', "test/#{@options[:gem_file_name]}_test.rb" render_file 'lib/sample.rb', "lib/#{@options[:gem_file_name]}.rb" if [:executable] render_file('bin/sample', "bin/#{@options[:gem_file_name]}") exec_path = result_path "bin/#{@options[:gem_file_name]}" `chmod +x #{exec_path}` end puts "Your gem is ready at #{@options[:gem_path]}, start coding!" if cli_mode end |
Instance Method Details
#classify(gem_name) ⇒ Object
‘My gem’ => ‘MyGem’
88 89 90 |
# File 'lib/gemstone.rb', line 88 def classify(gem_name) gem_name.strip.split.map{ |word| word.capitalize}.join.gsub(' ', '') end |
#render_file(path, result_file_path = path) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/gemstone.rb', line 93 def render_file(path, result_file_path = path) source_path = File.join template_path, path result_file_path = result_path(result_file_path) FileUtils.mkdir_p File.dirname(result_file_path) result = ERB.new(File.read(source_path)).result(binding) File.open(result_file_path, 'w') {|f| f.write(result) } end |
#result_path(source_path) ⇒ Object
105 106 107 |
# File 'lib/gemstone.rb', line 105 def result_path(source_path) File.join [:gem_path], source_path end |
#template_path ⇒ Object
101 102 103 |
# File 'lib/gemstone.rb', line 101 def template_path File.dirname(__FILE__) + '/../template' end |
#underscore(gem_name) ⇒ Object
‘My gem’ => ‘my_gem’
83 84 85 |
# File 'lib/gemstone.rb', line 83 def underscore(gem_name) gem_name.strip.downcase.gsub(' ', '_') end |