Class: GemStone::Generator

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

Instance Method Summary collapse

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(options = nil)
  cli_mode = options.nil?
  options ||= CLI.ask_options
  @options = options
  OptionsValidator.validate!(@options)
  @options[:gem_file_name] = underscore(@options[:name])
  @options[:gem_class_name] = classify(@options[:name])
  @options[: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 @options[: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 @options[:gem_path], source_path
end

#template_pathObject



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