Class: FrankensteinSinatra::TemplateGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/frankenstein-sinatra/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ TemplateGenerator

Returns a new instance of TemplateGenerator.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/frankenstein-sinatra/template.rb', line 4

def initialize project
  @project = project
  Dir.mkdir dir rescue nil
  Dir.mkdir dir('views') rescue nil
  Dir.mkdir dir('views/partials') rescue nil
  Dir.mkdir dir('views/javascripts') rescue nil
  Dir.mkdir dir('views/stylesheets') rescue nil
  Dir.mkdir dir('public') rescue nil
  Dir.mkdir dir('public/js') rescue nil
  Dir.mkdir dir('public/stylesheets') rescue nil
  Dir.mkdir dir('public/images') rescue nil
  Dir.mkdir dir('models') rescue nil
  Dir.mkdir dir('controllers') rescue nil
  Dir.mkdir dir('locales') rescue nil
  Dir.mkdir dir('configs') rescue nil
  app_rb
  config_ru
  gemfile
  layout
  index
  app_sass
  app_coffee
  partial_head
  helpers_rb
  jquery
  gitignore
end

Instance Method Details

#app_coffeeObject



107
108
109
110
# File 'lib/frankenstein-sinatra/template.rb', line 107

def app_coffee
  code = "$ ->"
  write dir('views/javascripts/app.coffee'), code
end

#app_rbObject



32
33
34
35
36
37
38
39
40
# File 'lib/frankenstein-sinatra/template.rb', line 32

def app_rb
  code  = "#coding: utf-8 \n"
  code += "require 'sinatra'\n"
  code += "require 'frankenstein-sinatra'\n\n"
  code += "get '/' do\n"
  code += "  haml :index\n"
  code += "end"
  write dir('app.rb'), code
end

#app_sassObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/frankenstein-sinatra/template.rb', line 94

def app_sass
  code  = "@import compass/reset\n"
  code += "@import compass/css3\n\n"
  code += "$wrapper: 940px\n\n"
  code += ".wrapper\n"
  code += "  :width $wrapper\n"
  code += "  :margin 0 auto\n"
  code += "#top, #middle, #bottom\n"
  code += "  :float left\n"
  code += "  :width 100%"
  write dir('views/stylesheets/app.sass'), code
end

#config_ruObject



42
43
44
45
46
# File 'lib/frankenstein-sinatra/template.rb', line 42

def config_ru
  code  = "require './app'\n"
  code +=  "run Sinatra::Application"
  write dir('config.ru'), code
end

#dir(file = '') ⇒ Object



139
140
141
# File 'lib/frankenstein-sinatra/template.rb', line 139

def dir file=''
  "#{@project}/#{file}"
end

#gemfileObject



54
55
56
57
58
59
60
61
# File 'lib/frankenstein-sinatra/template.rb', line 54

def gemfile
  code  = "source :rubygems \n"
  code += "gem 'frankenstein-sinatra'\n"
  code += "group :production do\n"
  code += "  gem 'therubyracer'\n"
  code += "end"
  write dir('Gemfile'), code
end

#gitignoreObject



127
128
129
130
131
132
133
# File 'lib/frankenstein-sinatra/template.rb', line 127

def gitignore
  code  = "*.swp\n"
  code += "*.swo\n"
  code += ".bundle/\n"
  code += ".sass-cache/"
  write dir('.gitignore'), code
end

#helpers_rbObject



48
49
50
51
52
# File 'lib/frankenstein-sinatra/template.rb', line 48

def helpers_rb
  code  = "helpers do\n"
  code += "end"
  write dir('helpers.rb'), code
end

#indexObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/frankenstein-sinatra/template.rb', line 79

def index
  songs = [
    "Saturday Night",
    "I've Got a Crush on You",
    "Oh, What it Seemed to Be",
    "Nancy (With the Laughing Face)",
    "The Coffee Song",
    "The House I Live in",
    "Someone to Watch Over Me",
    "Five Minutes More"
  ]
  code = "%h1 Frankstein sinatra is singing \"#{songs.sample}\""
  write dir('views/index.haml'), code
end

#jqueryObject



122
123
124
125
# File 'lib/frankenstein-sinatra/template.rb', line 122

def jquery
  Net::HTTP.start("code.jquery.com"){|http| @code = http.get('/jquery.min.js').body} rescue nil
  write dir('public/js/jquery.js'), @code
end

#layoutObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/frankenstein-sinatra/template.rb', line 63

def layout
  code  = "!!!\n"
  code += "%html\n"
  code += "  %head\n"
  code += "    = partial :head\n"
  code += "  %body\n"
  code += "    #top\n"
  code += "      .wrapper\n"
  code += "    #middle\n"
  code += "      .wrapper\n"
  code += "        = yield\n"
  code += "    #bottom\n"
  code += "      .wrapper\n"
  write dir('views/layout.haml'), code
end

#partial_headObject



112
113
114
115
116
117
118
119
120
# File 'lib/frankenstein-sinatra/template.rb', line 112

def partial_head
  code  = "%title #{@project}\n"
  code += "%meta{:'http-equiv' => 'Content-Type', :content => 'text/html', :charset => 'utf-8'}\n"
  code += "%link{rel: 'stylesheet', type: 'text/css', href: '/stylesheets/app.css'}\n"
  code += "%link{rel: 'shortcut icon', type: 'image/x-icon', href: '/images/favicon.png'}\n"
  code += "%script{src: '/js/jquery.js'}\n"
  code += "%script{src: '/js/app.js'}"
  write dir('views/partials/_head.haml'), code
end

#write(file, content) ⇒ Object



135
136
137
# File 'lib/frankenstein-sinatra/template.rb', line 135

def write file, content
  File.open("#{file}", 'w') {|f| f.write(content) }
end