class Twittbot::Generators::AppGenerator
Class to generate a new Twittbot
bot.
Public Class Methods
new(app_name, options = {})
click to toggle source
# File lib/twittbot/generators/twittbot/app/app_generator.rb, line 13 def initialize(app_name, options = {}) @options = { 'template_dir' => Twittbot::TEMPLATE_DIR }.merge!(options) @app_name = app_name @options['template_dir'] = File.expand_path @options['template_dir'] end
Public Instance Methods
create()
click to toggle source
Creates the bot.
# File lib/twittbot/generators/twittbot/app/app_generator.rb, line 22 def create path = File.expand_path "./#{@app_name}" if File.exist?(@app_name) say "#{File.directory?(@app_name) ? 'Directory' : 'File'} #{@app_name} already exists", :red exit 1 end FileUtils.mkdir_p(path) # build the template files = Dir["#{@options['template_dir']}/**/*"] files.each do |file| real_filename = file.sub(/^#{@options['template_dir']}\//, '').sub(/^_/, '.') real_path = "#{path}/#{real_filename}" say_status :create, real_filename, :green if File.directory? file FileUtils.mkdir_p real_path else erb = Erubis::Eruby.new File.read(file) File.open real_path, 'w' do |f| f.write erb.result(binding) end end end generate_config path end
Private Instance Methods
generate_config(base_path)
click to toggle source
# File lib/twittbot/generators/twittbot/app/app_generator.rb, line 50 def generate_config(base_path) say_status :create, Twittbot::CONFIG_FILE_NAME, :green File.open "#{base_path}/#{Twittbot::CONFIG_FILE_NAME}", 'w' do |f| f.write Twittbot::DEFAULT_BOT_CONFIG.to_yaml end end