class Sinator::Generator
Attributes
app_class_name[RW]
app_name[RW]
destination[RW]
Public Class Methods
new(app_name, options={})
click to toggle source
# File lib/sinator/generator.rb, line 11 def initialize(app_name, options={}) @app_name = app_name @app_class_name = app_name.split("_").map{|s| s.capitalize }.join("") destination = options[:destination] ? "#{options[:destination]}/#{@app_name}" : @app_name @with_database = options[:with_database] unless File.directory?(destination) FileUtils.mkdir_p(destination) end @destination = File.expand_path(destination) end
Public Instance Methods
generate_app()
click to toggle source
github.com/sinatra/sinatra-book/blob/master/book/Organizing_your_application.markdown
# File lib/sinator/generator.rb, line 68 def generate_app copy_templates app = File.read File.expand_path("../templates/app.erb", __FILE__) erb = ERB.new app, 0, '-' File.open "#{@destination}/#{@app_name}.rb", "w" do |f| f.write erb.result(binding) end route = File.read File.expand_path("../templates/app/routes/home.erb", __FILE__) erb = ERB.new route, 0, '-' File.open "#{@destination}/app/routes/home.rb", "w" do |f| f.write erb.result(binding) end FileUtils.rm "#{@destination}/app/routes/home.erb" end
generate_bundle_config()
click to toggle source
# File lib/sinator/generator.rb, line 43 def generate_bundle_config config_ru = File.read File.expand_path("../templates/config.ru.erb", __FILE__) erb = ERB.new config_ru File.open "#{@destination}/config.ru", "w" do |f| f.write erb.result(binding) end end
generate_gemfile()
click to toggle source
# File lib/sinator/generator.rb, line 34 def generate_gemfile gemfile = File.read File.expand_path("../templates/Gemfile.erb", __FILE__) erb = ERB.new gemfile, 0, '-' File.open "#{@destination}/Gemfile", "w" do |f| f.write erb.result(binding) end end
generate_puma_config()
click to toggle source
# File lib/sinator/generator.rb, line 52 def generate_puma_config puma_development = File.read File.expand_path("../templates/config/puma/development.erb", __FILE__) puma_production = File.read File.expand_path("../templates/config/puma/production.erb", __FILE__) erb = ERB.new puma_development, 0, '-' File.open "#{@destination}/config/puma/development.rb", "w" do |f| f.write erb.result(binding) end erb = ERB.new puma_production, 0, '-' File.open "#{@destination}/config/puma/production.rb", "w" do |f| f.write erb.result(binding) end end
generate_rakefile()
click to toggle source
# File lib/sinator/generator.rb, line 25 def generate_rakefile gemfile = File.read File.expand_path("../templates/Rakefile.erb", __FILE__) erb = ERB.new gemfile, 0, '-' File.open "#{@destination}/Rakefile", "w" do |f| f.write erb.result(binding) end end
Private Instance Methods
copy_templates()
click to toggle source
# File lib/sinator/generator.rb, line 89 def copy_templates FileUtils.cp_r File.expand_path("../templates/assets", __FILE__), @destination FileUtils.cp_r File.expand_path("../templates/config", __FILE__), @destination FileUtils.cp_r File.expand_path("../templates/public", __FILE__), @destination FileUtils.cp_r File.expand_path("../templates/app", __FILE__), @destination FileUtils.rm "#{@destination}/config/puma/development.erb" FileUtils.rm "#{@destination}/config/puma/production.erb" if @with_database FileUtils.mkdir "#{@destination}/app/models" FileUtils.cp_r File.expand_path("../templates/db", __FILE__), @destination else FileUtils.rm "#{@destination}/config/database.yml" end end