raamen 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +354 -7
- data/bin/console +2 -5
- data/bin/raamen +104 -0
- data/lib/raamen/controller_base.rb +3 -2
- data/lib/raamen/file_contents/app_content.rb +27 -0
- data/lib/raamen/file_contents/controller_content.rb +7 -0
- data/lib/raamen/file_contents/db_connection_content.rb +62 -0
- data/lib/raamen/file_contents/gem_file_content.rb +6 -0
- data/lib/raamen/file_contents/model_content.rb +8 -0
- data/lib/raamen/flash.rb +0 -2
- data/lib/raamen/session.rb +0 -2
- data/lib/raamen/show_exceptions.rb +0 -2
- data/lib/raamen/sql_object.rb +1 -1
- data/lib/raamen/static.rb +1 -2
- data/lib/raamen/version.rb +1 -1
- data/raamen.gemspec +3 -3
- metadata +15 -8
- data/lib/raamen/db/sqlite3.db +0 -0
- data/lib/raamen/db/sqlite3.sql +0 -43
- data/lib/raamen/db_connection.rb +0 -62
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52786c2ccabef8c2b68a15f3b9da71d13c2ade12
|
4
|
+
data.tar.gz: 76987cdb2b1e66b9b30a99385483401a771a5c1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3cc111150e47cbdbcfc5f5fee64cbc7ef75ad9c430c716dc9960c037828efb54958f37e9f0e5e1248fadb03bd6475c0b985dd9f295a0eb592e0a6529a31fc918
|
7
|
+
data.tar.gz: 83d634295027b157461b4488ff87ac39b0884bfb01095feb9a0badfb0cc342f80d688cd33f65f0f9e7f7536d0a85eec39024cc3b082cf5c6a973aae11e0d2646
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,14 @@
|
|
1
1
|
# Raamen
|
2
2
|
|
3
|
-
|
3
|
+
[Raamen](http://rubygems.org/gems/raamen) is a Rack and SQLite based MVC web framework. It contains modules to provide basic and necessary features of web app components as well as helpful Rack middleware, cookie manipulation, and CLI to make the development process faster and easier.
|
4
4
|
|
5
|
-
|
5
|
+
#### contents
|
6
|
+
- SQLObject
|
7
|
+
- ControllerBase
|
8
|
+
- Routing
|
9
|
+
- Cookies
|
10
|
+
- Middleware
|
11
|
+
- Command Line Interface
|
6
12
|
|
7
13
|
## Installation
|
8
14
|
|
@@ -22,13 +28,355 @@ Or install it yourself as:
|
|
22
28
|
|
23
29
|
## Usage
|
24
30
|
|
25
|
-
|
31
|
+
### SQLObject
|
26
32
|
|
27
|
-
|
33
|
+
```ruby
|
34
|
+
class Cat < Raamen::SQLObject
|
35
|
+
belongs_to :owner,
|
36
|
+
class_name: 'Human',
|
37
|
+
foreign_key: :owner_id
|
38
|
+
|
39
|
+
self.finalize!
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
### ControllerBase
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
class CatsController < Raamen::ControllerBase
|
47
|
+
def index
|
48
|
+
@cats = Cat.all
|
49
|
+
render :index
|
50
|
+
end
|
51
|
+
|
52
|
+
def new
|
53
|
+
@cat = Cat.new
|
54
|
+
render :new
|
55
|
+
end
|
56
|
+
|
57
|
+
def create
|
58
|
+
cat = Cat.new(cat_params)
|
59
|
+
cat.save
|
60
|
+
redirect_to '/cats'
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def cat_params
|
66
|
+
params['cat']
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
```
|
71
|
+
|
72
|
+
### Routing
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
router = Raamen::Router.new
|
76
|
+
router.draw do
|
77
|
+
get Regexp.new("^/cats$"), CatsController, :index
|
78
|
+
get Regexp.new("^/cats/new$"), CatsController, :new
|
79
|
+
post Regexp.new("^/cats$"), CatsController, :create
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
### Cookies
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class MyController < ControllerBase
|
87
|
+
def go
|
88
|
+
session["count"] ||= 0
|
89
|
+
session["count"] += 1
|
90
|
+
render :counting_show
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
```
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class DogsController < ControllerBase
|
98
|
+
def create
|
99
|
+
@dog = Dog.new(params["dog"])
|
100
|
+
if @dog.save
|
101
|
+
flash[:notice] = "Saved dog successfully"
|
102
|
+
redirect_to "/dogs"
|
103
|
+
else
|
104
|
+
flash.now[:errors] = @dog.errors
|
105
|
+
render :new
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
### Middleware
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
app = Rack::Builder.new do
|
115
|
+
use Raamen::ShowExceptions
|
116
|
+
use Raamen::Static
|
117
|
+
run app
|
118
|
+
end.to_app
|
119
|
+
|
120
|
+
```
|
121
|
+
|
122
|
+
### Command Line Interface
|
123
|
+
|
124
|
+
To start a new project:
|
125
|
+
|
126
|
+
```
|
127
|
+
$ raamen n/new cats
|
128
|
+
```
|
129
|
+
|
130
|
+
To generate a new component:
|
131
|
+
|
132
|
+
```
|
133
|
+
$ raamen g/generate controller CatsController
|
134
|
+
```
|
135
|
+
|
136
|
+
To start server:
|
137
|
+
|
138
|
+
```
|
139
|
+
$ raamen s/start
|
140
|
+
```
|
141
|
+
|
142
|
+
To start console:
|
143
|
+
|
144
|
+
```
|
145
|
+
$ raamen c/console
|
146
|
+
```
|
147
|
+
|
148
|
+
## Implementation
|
149
|
+
|
150
|
+
#### SQLObject modules:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
module Associatable
|
154
|
+
def belongs_to(name, options = {})
|
155
|
+
self.assoc_options[name] = BelongsToOptions.new(name, options)
|
156
|
+
|
157
|
+
define_method(name) do
|
158
|
+
options = self.class.assoc_options[name]
|
159
|
+
key_val = self.send(options.foreign_key)
|
160
|
+
options.model_class.where(options.primary_key => key_val).first
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def has_many(name, options = {})
|
165
|
+
self.assoc_options[name] = HasManyOptions.new(name, self.name, options)
|
166
|
+
|
167
|
+
define_method(name) do
|
168
|
+
options = self.class.assoc_options[name]
|
169
|
+
key_val = self.send(options.primary_key)
|
170
|
+
options.model_class.where(options.foreign_key => key_val)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
module Searchable
|
178
|
+
def where(params)
|
179
|
+
where_line = params.keys.map do |col|
|
180
|
+
"#{col}= ?"
|
181
|
+
end.join(" AND ")
|
28
182
|
|
29
|
-
|
183
|
+
parse_all(DBConnection.execute(<<-SQL, *params.values))
|
184
|
+
SELECT
|
185
|
+
*
|
186
|
+
FROM
|
187
|
+
#{self.table_name}
|
188
|
+
where
|
189
|
+
#{where_line}
|
190
|
+
SQL
|
191
|
+
end
|
192
|
+
end
|
193
|
+
```
|
194
|
+
|
195
|
+
#### Template rendering:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
def render_content(content, content_type)
|
199
|
+
raise "double render" if already_built_response?
|
200
|
+
self.res["Content-Type"] = content_type
|
201
|
+
self.res.write(content)
|
202
|
+
self.session.store_session(res)
|
203
|
+
self.flash.store_flash(res)
|
204
|
+
self.already_built_response = true
|
205
|
+
end
|
206
|
+
|
207
|
+
def render(template_name)
|
208
|
+
template_path = File.join(
|
209
|
+
Dir.pwd,
|
210
|
+
"app",
|
211
|
+
"views",
|
212
|
+
"#{self.class.name.underscore}",
|
213
|
+
"#{template_name}.html.erb"
|
214
|
+
)
|
215
|
+
template_content = File.read(template_path)
|
216
|
+
render_content(ERB.new(template_content).result(binding), "text/html")
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
220
|
+
#### Redirecting:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
def redirect_to(url)
|
224
|
+
raise "double render" if already_built_response?
|
225
|
+
self.res["location"] = url
|
226
|
+
self.res.status = 302
|
227
|
+
self.session.store_session(res)
|
228
|
+
self.flash.store_flash(res)
|
229
|
+
self.already_built_response = true
|
230
|
+
end
|
231
|
+
```
|
232
|
+
|
233
|
+
#### Cross-Site Request Forgery protection:
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
def form_authenticity_token
|
237
|
+
self.res.set_cookie(
|
238
|
+
"authenticity_token",
|
239
|
+
{path: "/", value: self.authenticity_token}
|
240
|
+
)
|
241
|
+
self.authenticity_token
|
242
|
+
end
|
243
|
+
|
244
|
+
def self.protect_from_forgery
|
245
|
+
@@protect_from_forgery = true
|
246
|
+
end
|
247
|
+
|
248
|
+
private
|
249
|
+
|
250
|
+
def generate_authenticity_token
|
251
|
+
SecureRandom.urlsafe_base64(16)
|
252
|
+
end
|
253
|
+
|
254
|
+
def check_authenticity_token
|
255
|
+
cookie = self.req.cookies["authenticity_token"]
|
256
|
+
unless cookie && cookie == params["authenticity_token"]
|
257
|
+
raise "Invalid authenticity token"
|
258
|
+
end
|
259
|
+
end
|
260
|
+
```
|
261
|
+
|
262
|
+
#### Adding new routes:
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
def add_route(pattern, method, controller_class, action_name)
|
266
|
+
self.routes.push(Route.new(pattern, method, controller_class, action_name))
|
267
|
+
end
|
268
|
+
|
269
|
+
def draw(&proc)
|
270
|
+
self.instance_eval(&proc)
|
271
|
+
end
|
272
|
+
|
273
|
+
[:get, :post, :put, :delete].each do |http_method|
|
274
|
+
define_method(http_method) do |pattern, controller_class, action_name|
|
275
|
+
add_route(pattern, http_method, controller_class, action_name)
|
276
|
+
end
|
277
|
+
end
|
278
|
+
```
|
279
|
+
|
280
|
+
#### Flash cookies:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
class Flash
|
284
|
+
attr_reader :flash, :now
|
285
|
+
|
286
|
+
def initialize(req)
|
287
|
+
flash = req.cookies["_rails_lite_app_flash"]
|
288
|
+
@now = flash ? Now.new(JSON.parse(flash)) : Now.new({})
|
289
|
+
@flash = {}
|
290
|
+
end
|
291
|
+
|
292
|
+
def [](key)
|
293
|
+
self.now[key.to_sym] || self.flash[key.to_sym]
|
294
|
+
end
|
295
|
+
|
296
|
+
def []=(key, val)
|
297
|
+
self.flash[key.to_sym] = val
|
298
|
+
end
|
299
|
+
|
300
|
+
def store_flash(res)
|
301
|
+
res.set_cookie("_rails_lite_app_flash", {path: "/", value: self.flash.to_json})
|
302
|
+
end
|
303
|
+
end
|
304
|
+
```
|
305
|
+
|
306
|
+
#### Sesssion Cookies:
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
class Session
|
310
|
+
attr_reader :cookies
|
311
|
+
|
312
|
+
def initialize(req)
|
313
|
+
cookies = req.cookies["_rails_lite_app"]
|
314
|
+
cookies = Hash[JSON.parse(cookies).map{ |k,v| [k.to_sym, v] }] if cookies
|
315
|
+
@cookies = cookies || {}
|
316
|
+
end
|
317
|
+
|
318
|
+
def [](key)
|
319
|
+
self.cookies[key.to_sym]
|
320
|
+
end
|
321
|
+
|
322
|
+
def []=(key, val)
|
323
|
+
self.cookies[key.to_sym] = val
|
324
|
+
end
|
325
|
+
|
326
|
+
def store_session(res)
|
327
|
+
res.set_cookie("_rails_lite_app", {path: "/", value: self.cookies.to_json})
|
328
|
+
end
|
329
|
+
end
|
330
|
+
```
|
331
|
+
|
332
|
+
#### Show exceptions middleware:
|
333
|
+
|
334
|
+
```ruby
|
335
|
+
class ShowExceptions
|
336
|
+
attr_reader :app
|
337
|
+
|
338
|
+
def initialize(app)
|
339
|
+
@app = app
|
340
|
+
end
|
341
|
+
|
342
|
+
def call(env)
|
343
|
+
begin
|
344
|
+
self.app.call(env)
|
345
|
+
rescue Exception => e
|
346
|
+
render_exception(e)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
350
|
+
```
|
351
|
+
|
352
|
+
#### Static assets middleware:
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
class Static
|
356
|
+
attr_reader :app, :root, :file_server
|
357
|
+
|
358
|
+
def initialize(app)
|
359
|
+
@app = app
|
360
|
+
@root = :public
|
361
|
+
@file_server = FileServer.new(self.root)
|
362
|
+
end
|
363
|
+
|
364
|
+
def call(env)
|
365
|
+
req = Rack::Request.new(env)
|
366
|
+
path = req.path
|
367
|
+
|
368
|
+
if path.include?("/#{self.root}")
|
369
|
+
res = self.file_server.call(env)
|
370
|
+
else
|
371
|
+
res = self.app.call(env)
|
372
|
+
end
|
373
|
+
|
374
|
+
res
|
375
|
+
end
|
376
|
+
end
|
377
|
+
|
378
|
+
```
|
30
379
|
|
31
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
380
|
|
33
381
|
## Contributing
|
34
382
|
|
@@ -38,4 +386,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
38
386
|
## License
|
39
387
|
|
40
388
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
-
|
data/bin/console
CHANGED
@@ -7,8 +7,5 @@ require "raamen"
|
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
9
|
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start(__FILE__)
|
10
|
+
require "pry"
|
11
|
+
Pry.start
|
data/bin/raamen
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'thor'
|
3
|
+
require 'pry'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/core_ext'
|
6
|
+
require_relative '../lib/raamen/file_contents/db_connection_content'
|
7
|
+
require_relative '../lib/raamen/file_contents/app_content'
|
8
|
+
require_relative '../lib/raamen/file_contents/gem_file_content'
|
9
|
+
require_relative '../lib/raamen/file_contents/controller_content'
|
10
|
+
require_relative '../lib/raamen/file_contents/model_content'
|
11
|
+
|
12
|
+
class RaamenCLI < Thor
|
13
|
+
|
14
|
+
desc "new", "creates new project directory"
|
15
|
+
def new(app_name)
|
16
|
+
system "mkdir", "#{app_name}"
|
17
|
+
system "touch", "#{app_name}/Gemfile"
|
18
|
+
system "touch", "#{app_name}/.gitignore"
|
19
|
+
system "mkdir", "#{app_name}/public"
|
20
|
+
system "mkdir", "#{app_name}/app"
|
21
|
+
system "touch", "#{app_name}/app/#{app_name}.rb"
|
22
|
+
system "touch", "#{app_name}/app/db_connection.rb"
|
23
|
+
system "mkdir", "#{app_name}/db"
|
24
|
+
system "touch", "#{app_name}/db/#{app_name}.sql"
|
25
|
+
system "mkdir", "#{app_name}/app/models"
|
26
|
+
system "mkdir", "#{app_name}/app/controllers"
|
27
|
+
system "mkdir", "#{app_name}/app/views"
|
28
|
+
system "mkdir", "#{app_name}/app/assets"
|
29
|
+
system "mkdir", "#{app_name}/app/assets/images"
|
30
|
+
system "mkdir", "#{app_name}/app/assets/javascripts"
|
31
|
+
system "mkdir", "#{app_name}/app/assets/stylesheets"
|
32
|
+
File.open("#{app_name}/app/db_connection.rb", "w") do |file|
|
33
|
+
file_content = FileContent::db_connection_content(app_name)
|
34
|
+
file.write(file_content)
|
35
|
+
end
|
36
|
+
File.open("#{app_name}/app/#{app_name}.rb", "w") do |file|
|
37
|
+
file_content = FileContent::app_content
|
38
|
+
file.write(file_content)
|
39
|
+
end
|
40
|
+
File.open("#{app_name}/Gemfile", "w") do |file|
|
41
|
+
file_content = FileContent::gem_file_content
|
42
|
+
file.write(file_content)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
desc "generate", "generates app components"
|
47
|
+
def generate(component, name)
|
48
|
+
file_name = name.underscore
|
49
|
+
case component
|
50
|
+
when "controller"
|
51
|
+
system "touch", "#{Dir.pwd}/app/controllers/#{file_name}.rb"
|
52
|
+
system "mkdir", "#{Dir.pwd}/app/views/#{file_name}"
|
53
|
+
File.open("#{Dir.pwd}/app/controllers/#{file_name}.rb", "w") do |file|
|
54
|
+
file_content = FileContent::controller_content(name)
|
55
|
+
file.write(file_content)
|
56
|
+
end
|
57
|
+
when "model"
|
58
|
+
system "touch", "#{Dir.pwd}/app/models/#{file_name}.rb"
|
59
|
+
File.open("#{Dir.pwd}/app/models/#{file_name}.rb", "w") do |file|
|
60
|
+
file_content = FileContent::model_content(name)
|
61
|
+
file.write(file_content)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
puts "Use 'raamen generate component name' to generate app components."
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
desc "console", "opens app console"
|
69
|
+
def console
|
70
|
+
require_relative "#{Dir.pwd}/app/#{Dir.pwd.split('/').last}.rb"
|
71
|
+
Pry.start app, prompt: [proc{ ">> " }]
|
72
|
+
end
|
73
|
+
|
74
|
+
desc "start", "starts app server"
|
75
|
+
def start
|
76
|
+
require_relative "#{Dir.pwd}/app/#{Dir.pwd.split('/').last}.rb"
|
77
|
+
Rack::Server.start(
|
78
|
+
app: app,
|
79
|
+
Port: 3000
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
desc "n", "short for 'new'"
|
84
|
+
def n(app_name)
|
85
|
+
new(app_name)
|
86
|
+
end
|
87
|
+
|
88
|
+
desc "g", "short for 'generate'"
|
89
|
+
def g(component, name)
|
90
|
+
generate(component, name)
|
91
|
+
end
|
92
|
+
|
93
|
+
desc "c", "short for 'console'"
|
94
|
+
def c
|
95
|
+
console
|
96
|
+
end
|
97
|
+
|
98
|
+
desc "s", "short for 'start'"
|
99
|
+
def s
|
100
|
+
start
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
RaamenCLI.start
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'active_support/core_ext'
|
3
3
|
require 'erb'
|
4
|
+
require 'json'
|
4
5
|
require_relative 'session'
|
5
6
|
require_relative 'flash'
|
6
7
|
|
@@ -44,8 +45,8 @@ module Raamen
|
|
44
45
|
|
45
46
|
def render(template_name)
|
46
47
|
template_path = File.join(
|
47
|
-
|
48
|
-
"
|
48
|
+
Dir.pwd,
|
49
|
+
"app",
|
49
50
|
"views",
|
50
51
|
"#{self.class.name.underscore}",
|
51
52
|
"#{template_name}.html.erb"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class FileContent
|
2
|
+
def self.app_content
|
3
|
+
"require 'raamen'
|
4
|
+
require 'rack'
|
5
|
+
|
6
|
+
def app()
|
7
|
+
router = Raamen::Router.new
|
8
|
+
router.draw do
|
9
|
+
# Add routes here, example:
|
10
|
+
# get Regexp.new(\"^/cats$\"), CatsController, :index
|
11
|
+
end
|
12
|
+
|
13
|
+
app = Proc.new do |env|
|
14
|
+
req = Rack::Request.new(env)
|
15
|
+
res = Rack::Response.new
|
16
|
+
router.run(req, res)
|
17
|
+
res.finish
|
18
|
+
end
|
19
|
+
|
20
|
+
app = Rack::Builder.new do
|
21
|
+
use Raamen::ShowExceptions
|
22
|
+
use Raamen::Static
|
23
|
+
run app
|
24
|
+
end.to_app
|
25
|
+
end"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class FileContent
|
2
|
+
def self.db_connection_content(app_name)
|
3
|
+
"require 'sqlite3'
|
4
|
+
|
5
|
+
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
6
|
+
# https://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path
|
7
|
+
ROOT_FOLDER = Dir.pwd
|
8
|
+
SQL_FILE = File.join(ROOT_FOLDER, 'db', '#{app_name}.sql')
|
9
|
+
DB_FILE = File.join(ROOT_FOLDER, 'db', '#{app_name}.db')
|
10
|
+
|
11
|
+
class DBConnection
|
12
|
+
def self.open(db_file_name)
|
13
|
+
@db = SQLite3::Database.new(db_file_name)
|
14
|
+
@db.results_as_hash = true
|
15
|
+
@db.type_translation = true
|
16
|
+
|
17
|
+
@db
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.reset
|
21
|
+
return DBConnection.open(DB_FILE) if File.exist?(DB_FILE)
|
22
|
+
|
23
|
+
commands = [\"cat '\#{SQL_FILE}' | sqlite3 '\#{DB_FILE}'\"]
|
24
|
+
commands.each { |command| `\#{command}` }
|
25
|
+
DBConnection.open(DB_FILE)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.instance
|
29
|
+
reset if @db.nil?
|
30
|
+
|
31
|
+
@db
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.execute(*args)
|
35
|
+
print_query(*args)
|
36
|
+
instance.execute(*args)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.execute2(*args)
|
40
|
+
print_query(*args)
|
41
|
+
instance.execute2(*args)
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.last_insert_row_id
|
45
|
+
instance.last_insert_row_id
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def self.print_query(query, *interpolation_args)
|
51
|
+
return unless PRINT_QUERIES
|
52
|
+
|
53
|
+
puts '--------------------'
|
54
|
+
puts query
|
55
|
+
unless interpolation_args.empty?
|
56
|
+
puts \"interpolate: \#{interpolation_args.inspect}\"
|
57
|
+
end
|
58
|
+
puts '--------------------'
|
59
|
+
end
|
60
|
+
end"
|
61
|
+
end
|
62
|
+
end
|
data/lib/raamen/flash.rb
CHANGED
data/lib/raamen/session.rb
CHANGED
data/lib/raamen/sql_object.rb
CHANGED
data/lib/raamen/static.rb
CHANGED
data/lib/raamen/version.rb
CHANGED
data/raamen.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["yuangaonyc@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = "A rack based web framework"
|
13
|
-
spec.description = ""
|
13
|
+
spec.description = "Raamen is a Rack and SQLite based MVC web framework. It contains modules to provide basic and necessary features of web app components as well as helpful Rack middleware, cookie manipulation, and CLI to make the development process faster and easier."
|
14
14
|
spec.homepage = "https://github.com/yuangaonyc/raamen"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
27
|
f.match(%r{^(test|spec|features)/})
|
28
28
|
end
|
29
|
-
|
30
|
-
spec.executables =
|
29
|
+
|
30
|
+
spec.executables = ["raamen"]
|
31
31
|
spec.require_paths = ["lib"]
|
32
32
|
|
33
33
|
spec.add_development_dependency "bundler", "~> 1.14"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raamen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuan Gao
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,10 +52,14 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
description:
|
55
|
+
description: Raamen is a Rack and SQLite based MVC web framework. It contains modules
|
56
|
+
to provide basic and necessary features of web app components as well as helpful
|
57
|
+
Rack middleware, cookie manipulation, and CLI to make the development process faster
|
58
|
+
and easier.
|
56
59
|
email:
|
57
60
|
- yuangaonyc@gmail.com
|
58
|
-
executables:
|
61
|
+
executables:
|
62
|
+
- raamen
|
59
63
|
extensions: []
|
60
64
|
extra_rdoc_files: []
|
61
65
|
files:
|
@@ -68,12 +72,15 @@ files:
|
|
68
72
|
- README.md
|
69
73
|
- Rakefile
|
70
74
|
- bin/console
|
75
|
+
- bin/raamen
|
71
76
|
- bin/setup
|
72
77
|
- lib/raamen.rb
|
73
78
|
- lib/raamen/controller_base.rb
|
74
|
-
- lib/raamen/
|
75
|
-
- lib/raamen/
|
76
|
-
- lib/raamen/
|
79
|
+
- lib/raamen/file_contents/app_content.rb
|
80
|
+
- lib/raamen/file_contents/controller_content.rb
|
81
|
+
- lib/raamen/file_contents/db_connection_content.rb
|
82
|
+
- lib/raamen/file_contents/gem_file_content.rb
|
83
|
+
- lib/raamen/file_contents/model_content.rb
|
77
84
|
- lib/raamen/flash.rb
|
78
85
|
- lib/raamen/router.rb
|
79
86
|
- lib/raamen/session.rb
|
data/lib/raamen/db/sqlite3.db
DELETED
Binary file
|
data/lib/raamen/db/sqlite3.sql
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
CREATE TABLE cats (
|
2
|
-
id INTEGER PRIMARY KEY,
|
3
|
-
name VARCHAR(255) NOT NULL,
|
4
|
-
owner_id INTEGER,
|
5
|
-
|
6
|
-
FOREIGN KEY(owner_id) REFERENCES human(id)
|
7
|
-
);
|
8
|
-
|
9
|
-
CREATE TABLE humans (
|
10
|
-
id INTEGER PRIMARY KEY,
|
11
|
-
fname VARCHAR(255) NOT NULL,
|
12
|
-
lname VARCHAR(255) NOT NULL,
|
13
|
-
house_id INTEGER,
|
14
|
-
|
15
|
-
FOREIGN KEY(house_id) REFERENCES human(id)
|
16
|
-
);
|
17
|
-
|
18
|
-
CREATE TABLE houses (
|
19
|
-
id INTEGER PRIMARY KEY,
|
20
|
-
address VARCHAR(255) NOT NULL
|
21
|
-
);
|
22
|
-
|
23
|
-
INSERT INTO
|
24
|
-
houses (id, address)
|
25
|
-
VALUES
|
26
|
-
(1, "26th and Guerrero"), (2, "Dolores and Market");
|
27
|
-
|
28
|
-
INSERT INTO
|
29
|
-
humans (id, fname, lname, house_id)
|
30
|
-
VALUES
|
31
|
-
(1, "Devon", "Watts", 1),
|
32
|
-
(2, "Matt", "Rubens", 1),
|
33
|
-
(3, "Ned", "Ruggeri", 2),
|
34
|
-
(4, "Catless", "Human", NULL);
|
35
|
-
|
36
|
-
INSERT INTO
|
37
|
-
cats (id, name, owner_id)
|
38
|
-
VALUES
|
39
|
-
(1, "Breakfast", 1),
|
40
|
-
(2, "Earl", 2),
|
41
|
-
(3, "Haskell", 3),
|
42
|
-
(4, "Markov", 3),
|
43
|
-
(5, "Stray Cat", NULL);
|
data/lib/raamen/db_connection.rb
DELETED
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'sqlite3'
|
2
|
-
|
3
|
-
module Raamen
|
4
|
-
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
5
|
-
# https://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path
|
6
|
-
ROOT_FOLDER = File.join(File.dirname(__FILE__), '..')
|
7
|
-
SQL_FILE = File.join(ROOT_FOLDER, 'db', 'sqlite3.sql')
|
8
|
-
DB_FILE = File.join(ROOT_FOLDER, 'db', 'sqlite3.db')
|
9
|
-
|
10
|
-
class DBConnection
|
11
|
-
def self.open(db_file_name)
|
12
|
-
@db = SQLite3::Database.new(db_file_name)
|
13
|
-
@db.results_as_hash = true
|
14
|
-
@db.type_translation = true
|
15
|
-
|
16
|
-
@db
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.reset
|
20
|
-
commands = [
|
21
|
-
"rm '#{DB_FILE}'",
|
22
|
-
"cat '#{SQL_FILE}' | sqlite3 '#{DB_FILE}'"
|
23
|
-
]
|
24
|
-
|
25
|
-
commands.each { |command| `#{command}` }
|
26
|
-
DBConnection.open(DB_FILE)
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.instance
|
30
|
-
reset if @db.nil?
|
31
|
-
|
32
|
-
@db
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.execute(*args)
|
36
|
-
print_query(*args)
|
37
|
-
instance.execute(*args)
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.execute2(*args)
|
41
|
-
print_query(*args)
|
42
|
-
instance.execute2(*args)
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.last_insert_row_id
|
46
|
-
instance.last_insert_row_id
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def self.print_query(query, *interpolation_args)
|
52
|
-
return unless PRINT_QUERIES
|
53
|
-
|
54
|
-
puts '--------------------'
|
55
|
-
puts query
|
56
|
-
unless interpolation_args.empty?
|
57
|
-
puts "interpolate: #{interpolation_args.inspect}"
|
58
|
-
end
|
59
|
-
puts '--------------------'
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|