Class: Sinator::Generator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_name, options = {}) ⇒ Generator

Returns a new instance of Generator.



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#app_class_nameObject

Returns the value of attribute app_class_name.



9
10
11
# File 'lib/sinator/generator.rb', line 9

def app_class_name
  @app_class_name
end

#app_nameObject

Returns the value of attribute app_name.



9
10
11
# File 'lib/sinator/generator.rb', line 9

def app_name
  @app_name
end

#destinationObject

Returns the value of attribute destination.



9
10
11
# File 'lib/sinator/generator.rb', line 9

def destination
  @destination
end

Instance Method Details

#generate_appObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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_configObject



43
44
45
46
47
48
49
50
# 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_gemfileObject



34
35
36
37
38
39
40
41
# 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_configObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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_rakefileObject



25
26
27
28
29
30
31
32
# 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