makanai 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +23 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +55 -0
- data/LICENCE.txt +21 -0
- data/README.md +225 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/makanai.rb +6 -0
- data/lib/makanai/application.rb +41 -0
- data/lib/makanai/database.rb +27 -0
- data/lib/makanai/dsl.rb +18 -0
- data/lib/makanai/main.rb +11 -0
- data/lib/makanai/migration.rb +18 -0
- data/lib/makanai/model.rb +124 -0
- data/lib/makanai/rake_tasks.rb +24 -0
- data/lib/makanai/request.rb +57 -0
- data/lib/makanai/response.rb +18 -0
- data/lib/makanai/router.rb +48 -0
- data/lib/makanai/settings.rb +10 -0
- data/lib/makanai/template.rb +18 -0
- data/lib/makanai/version.rb +3 -0
- data/makanai.gemspec +34 -0
- data/sample/Rakefile +3 -0
- data/sample/app.rb +7 -0
- data/sample/db/.keep +0 -0
- data/sample/migration/.keep +0 -0
- data/sample/models/.keep +0 -0
- data/sample/views/.keep +0 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 045d01dcb6633aa95bb5e62f1714b477e80d9df4b29bb225f70ee22ce0b62677
|
4
|
+
data.tar.gz: 66dd196776a842b881097bf686e658054e0a32377dc1fea5564f540275030763
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7fbceba802f6ae1b6edd0477afa1e13dc0ca73f182527f616a013e23d32b72577eba0e42e65798b0ba3c9452c75638b47da153cd6f40aa42ff6644ca1803413d
|
7
|
+
data.tar.gz: 86fd67e171f26715f21e6725e18c5d564267387334e519d6ea2f7bffa7c002ba786300b79edf61b0f96f38f6745a81748909fe5aedc13b086bf3cd6b13ae9a12
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6.3
|
3
|
+
Exclude:
|
4
|
+
- './makanai.gemspec'
|
5
|
+
|
6
|
+
Style/Documentation:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 90
|
11
|
+
|
12
|
+
Metrics/ClassLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Exclude:
|
17
|
+
- 'spec/**/*'
|
18
|
+
|
19
|
+
Style/ParallelAssignment:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Layout/EmptyLineAfterGuardClause:
|
23
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at 17684091+Madogiwa0124@users.noreply.github.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
makanai (0.1.0)
|
5
|
+
rack (~> 2.0.7)
|
6
|
+
rake (~> 10.0)
|
7
|
+
sqlite3 (~> 1.4.1)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
ast (2.4.0)
|
13
|
+
diff-lcs (1.3)
|
14
|
+
jaro_winkler (1.5.3)
|
15
|
+
parallel (1.17.0)
|
16
|
+
parser (2.6.3.0)
|
17
|
+
ast (~> 2.4.0)
|
18
|
+
rack (2.0.7)
|
19
|
+
rainbow (3.0.0)
|
20
|
+
rake (10.5.0)
|
21
|
+
rspec (3.8.0)
|
22
|
+
rspec-core (~> 3.8.0)
|
23
|
+
rspec-expectations (~> 3.8.0)
|
24
|
+
rspec-mocks (~> 3.8.0)
|
25
|
+
rspec-core (3.8.2)
|
26
|
+
rspec-support (~> 3.8.0)
|
27
|
+
rspec-expectations (3.8.4)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-mocks (3.8.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-support (3.8.2)
|
34
|
+
rubocop (0.74.0)
|
35
|
+
jaro_winkler (~> 1.5.1)
|
36
|
+
parallel (~> 1.10)
|
37
|
+
parser (>= 2.6)
|
38
|
+
rainbow (>= 2.2.2, < 4.0)
|
39
|
+
ruby-progressbar (~> 1.7)
|
40
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
41
|
+
ruby-progressbar (1.10.1)
|
42
|
+
sqlite3 (1.4.1)
|
43
|
+
unicode-display_width (1.6.0)
|
44
|
+
|
45
|
+
PLATFORMS
|
46
|
+
ruby
|
47
|
+
|
48
|
+
DEPENDENCIES
|
49
|
+
bundler (~> 2.0)
|
50
|
+
makanai!
|
51
|
+
rspec (~> 3.0)
|
52
|
+
rubocop (~> 0.74)
|
53
|
+
|
54
|
+
BUNDLED WITH
|
55
|
+
2.0.2
|
data/LICENCE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 madogiwa
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
# Makanai
|
2
|
+
simple web application framework for learning.
|
3
|
+
|
4
|
+
# Getting Start
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'makanai'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle install
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install makanai
|
21
|
+
|
22
|
+
## Dependencies
|
23
|
+
|
24
|
+
Makanai depends on the following, so please install it in advance.
|
25
|
+
|
26
|
+
* [SQLite3](https://www.sqlite.org/index.html)
|
27
|
+
|
28
|
+
## Start sample project
|
29
|
+
Getting started with Makanai is easy.
|
30
|
+
|
31
|
+
```
|
32
|
+
$ git clone https://github.com/Madogiwa0124/makanai.git
|
33
|
+
$ bundle install
|
34
|
+
$ cd makanai/sample
|
35
|
+
$ be ruby app.rb
|
36
|
+
[2019-09-09 23:20:05] INFO WEBrick 1.4.2
|
37
|
+
[2019-09-09 23:20:05] INFO ruby 2.6.3 (2019-04-16) [x86_64-darwin18]
|
38
|
+
[2019-09-09 23:20:05] INFO WEBrick::HTTPServer#start: pid=30871 port=8080
|
39
|
+
```
|
40
|
+
|
41
|
+
## How to customize
|
42
|
+
You can customize the Makanai project by changing the sample directory. (You can also change the directory name from the sample.)
|
43
|
+
|
44
|
+
To see what features are available, see Usage.
|
45
|
+
|
46
|
+
# Usage
|
47
|
+
|
48
|
+
create a ruby file(ex. app.rb).
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
require_relative '../lib/main.rb'
|
52
|
+
|
53
|
+
router.get '/' do
|
54
|
+
'Hello Makanai!'
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
start server(WEBrick) at execute `$ ruby app.rb`.
|
59
|
+
|
60
|
+
```
|
61
|
+
$ ruby app.rb
|
62
|
+
[2019-08-12 20:44:20] INFO WEBrick 1.4.2
|
63
|
+
[2019-08-12 20:44:20] INFO ruby 2.6.3 (2019-04-16) [x86_64-darwin18]
|
64
|
+
[2019-08-12 20:44:20] INFO WEBrick::HTTPServer#start: pid=26043 port=8080
|
65
|
+
```
|
66
|
+
|
67
|
+
When accessing root, `Hello Makanai!` is displayed.
|
68
|
+
|
69
|
+
## constitution
|
70
|
+
Makanai application has the following constitution.
|
71
|
+
|
72
|
+
```
|
73
|
+
root ┬ app.rb # main application file
|
74
|
+
├ views # views files(ex. index.erb)
|
75
|
+
├ models # models files(ex. post.rb)
|
76
|
+
├ migration # migration files(ex. 00_create_resources.sql)
|
77
|
+
└ db # use Sqlite3 dabase file
|
78
|
+
```
|
79
|
+
|
80
|
+
## routing
|
81
|
+
|
82
|
+
``` ruby
|
83
|
+
require_relative '../lib/main.rb'
|
84
|
+
|
85
|
+
# root path
|
86
|
+
router.get '/' do
|
87
|
+
'Hello Makanai!'
|
88
|
+
end
|
89
|
+
|
90
|
+
# enable access to /hoge
|
91
|
+
router.get '/hoge' do
|
92
|
+
'Hello Hoge!'
|
93
|
+
end
|
94
|
+
|
95
|
+
# enable access to /params with get parameter
|
96
|
+
router.get '/check' do |request|
|
97
|
+
request.params['hoge']
|
98
|
+
end
|
99
|
+
|
100
|
+
# enable access to /resources with method post and redirect other url.
|
101
|
+
router.post '/resources' do |request|
|
102
|
+
Resource.new(request.params).create
|
103
|
+
redirect_to("#{request.root_url}/resources")
|
104
|
+
end
|
105
|
+
|
106
|
+
# enable access to /resources with method put and redirect other url.
|
107
|
+
router.put '/resources' do |request|
|
108
|
+
resource = Resource.find(request.params['id'])
|
109
|
+
resource.assign_attributes(request.params).update
|
110
|
+
redirect_to("#{request.root_url}/resources")
|
111
|
+
end
|
112
|
+
|
113
|
+
# enable access to /resources with method delete and redirect other url.
|
114
|
+
router.delete '/resources' do |request|
|
115
|
+
resource = Resource.find(request.params['id'])
|
116
|
+
resource.assign_attributes(request.params).delete
|
117
|
+
redirect_to("#{request.root_url}/resources")
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
## erb render
|
122
|
+
|
123
|
+
Define instance variables used in the routing view.
|
124
|
+
|
125
|
+
``` ruby
|
126
|
+
require_relative '../lib/main.rb'
|
127
|
+
|
128
|
+
router.get '/index' do
|
129
|
+
@title = 'Makanai title'
|
130
|
+
@body = 'Makanai body'
|
131
|
+
render :index
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
Create an erb file in `src/views` with the name specified in render.
|
136
|
+
|
137
|
+
``` html
|
138
|
+
<!-- src/views/index.erb -->
|
139
|
+
<html>
|
140
|
+
<head>
|
141
|
+
<meta charset="UTF-8">
|
142
|
+
<title><%= @title %></title>
|
143
|
+
</head>
|
144
|
+
<body>
|
145
|
+
<%= @body %>
|
146
|
+
</body>
|
147
|
+
</html>
|
148
|
+
```
|
149
|
+
|
150
|
+
## migration
|
151
|
+
|
152
|
+
makanai uses `sqlite3` as db. create db and migrate schema when executed `rake makanai:db:migration`.
|
153
|
+
|
154
|
+
```
|
155
|
+
# execute all migraiton sql
|
156
|
+
$ rake makanai:db:migration target=all
|
157
|
+
|
158
|
+
# execute migraiton target sql
|
159
|
+
$ rake makanai:db:migration target=20190816_1_create_resource.sql
|
160
|
+
```
|
161
|
+
|
162
|
+
display information when excuted migration.
|
163
|
+
|
164
|
+
```
|
165
|
+
$ rake makanai:db:migration target=all
|
166
|
+
INFO: start migration all
|
167
|
+
execute: makanai/src/migration/20190816_1_create_resources.sql
|
168
|
+
create table resources (
|
169
|
+
name varchar(30),
|
170
|
+
val int
|
171
|
+
);
|
172
|
+
execute: makanai/src/migration/20190816_2_drop_resources.sql
|
173
|
+
drop table resources;
|
174
|
+
INFO: finished migration all
|
175
|
+
```
|
176
|
+
|
177
|
+
## model
|
178
|
+
`Makanai::Model` is simple ORM.
|
179
|
+
It can be used by creating a class that inherits `Makanai::Model`.
|
180
|
+
|
181
|
+
``` ruby
|
182
|
+
require_relative '../../lib/model.rb'
|
183
|
+
|
184
|
+
class Resource < Makanai::Model
|
185
|
+
# target table when executing sql.
|
186
|
+
TABLE_NAME = 'resources'
|
187
|
+
# primary key of the table.
|
188
|
+
PRYMARY_KEY = 'id'
|
189
|
+
end
|
190
|
+
|
191
|
+
# execute `select * from resources;` and get all records.
|
192
|
+
Resource.all
|
193
|
+
#=> [
|
194
|
+
# #<Resource:0x00007fc967a7c578 @origin_attributes={"id"=>1, "name"=>"one", "val"=>1}, @id=1, @name="one", @val=1>
|
195
|
+
# #<Resource:0x00007ffa55a60520 @origin_attributes={"id"=>2, "name"=>"two", "val"=>2}, @id=2, @name="two", @val=2>
|
196
|
+
# ]
|
197
|
+
|
198
|
+
# execute `select * from resources where id = 1 LIMIT 1` and get record.
|
199
|
+
Resource.find(1)
|
200
|
+
#=> #<Resource:0x00007ffa5509f400 @origin_attributes={"id"=>1, "name"=>"one", "val"=>1}, @id=1, @name="one", @val=1>
|
201
|
+
|
202
|
+
# execute `insert into resources(name, val) values ('one', 1);`
|
203
|
+
Resource.new(name: 'one', val: 1).create
|
204
|
+
#=> #<Resource:0x00007fca1c160dd0 @origin_attributes={"id"=>1, "name"=>"one", "val"=>1}, @id=1, @name="one", @val=1>
|
205
|
+
|
206
|
+
# execute `update resources set id=1, name='eins',val=1 where id = 1;`
|
207
|
+
Resource.find(1).tap { |num| num.name = 'eins' }.update
|
208
|
+
#=> #<Resource:0x00007fca1c160dd0 @origin_attributes={"id"=>2, "name"=>"zwei", "val"=>2}, @id=6, @name="zwei", @val=2>
|
209
|
+
|
210
|
+
# execute `delete from resources where id = 1;`
|
211
|
+
Resource.find(1).delete
|
212
|
+
#=> nil
|
213
|
+
```
|
214
|
+
|
215
|
+
# Contributing
|
216
|
+
|
217
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Madogiwa0124/makanai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
218
|
+
|
219
|
+
# License
|
220
|
+
|
221
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
222
|
+
|
223
|
+
# Code of Conduct
|
224
|
+
|
225
|
+
Everyone interacting in the makanai project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Madogiwa0124/makanai/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "makanai"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/makanai.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'uri'
|
5
|
+
require 'rack'
|
6
|
+
require_relative './request.rb'
|
7
|
+
require_relative './response.rb'
|
8
|
+
require_relative './router.rb'
|
9
|
+
|
10
|
+
module Makanai
|
11
|
+
class Application
|
12
|
+
def initialize(router:)
|
13
|
+
@router = router
|
14
|
+
@handler = Rack::Handler::WEBrick
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :router, :handler, :request, :response
|
18
|
+
|
19
|
+
def run!
|
20
|
+
handler.run self
|
21
|
+
end
|
22
|
+
|
23
|
+
def call(env)
|
24
|
+
@request = Makanai::Request.new(env)
|
25
|
+
@response = Makanai::Response.new
|
26
|
+
route_result = execute_route
|
27
|
+
return route_result.result if route_result.class == Response
|
28
|
+
@response.body << execute_route
|
29
|
+
@response.result
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def execute_route
|
35
|
+
router.bind!(url: request.url, method: request.method).process.call(request)
|
36
|
+
rescue Makanai::Router::NotFound
|
37
|
+
@response.status = 404
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
require_relative './settings.rb'
|
5
|
+
|
6
|
+
module Makanai
|
7
|
+
class Database
|
8
|
+
DATABASE_PATH = "#{Settings::APP_ROOT_PATH}#{Settings::DATABASE_PATH}"
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@handler = SQLite3::Database
|
12
|
+
@db = handler.new DATABASE_PATH
|
13
|
+
db.tap { |db| db.results_as_hash = true }
|
14
|
+
end
|
15
|
+
|
16
|
+
attr_reader :handler, :db
|
17
|
+
|
18
|
+
def execute_sql(sql)
|
19
|
+
puts "SQL: #{sql.gsub("\n", ' ')}"
|
20
|
+
db.execute(sql).tap { close_db }
|
21
|
+
end
|
22
|
+
|
23
|
+
def close_db
|
24
|
+
db.close
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/makanai/dsl.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './template.rb'
|
4
|
+
require_relative './response.rb'
|
5
|
+
require_relative './settings.rb'
|
6
|
+
|
7
|
+
def render(path)
|
8
|
+
template_root_path = "#{Dir.pwd}#{Makanai::Settings::TEMPLATE_ROOT_PATH}"
|
9
|
+
full_path = "#{template_root_path}#{path}.erb"
|
10
|
+
Makanai::Template.new(path: full_path).render
|
11
|
+
end
|
12
|
+
|
13
|
+
def redirect_to(url)
|
14
|
+
Makanai::Response.new.tap do |response|
|
15
|
+
response.status = 302
|
16
|
+
response.header = { 'Location' => url }
|
17
|
+
end
|
18
|
+
end
|
data/lib/makanai/main.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './settings.rb'
|
4
|
+
require_relative './database.rb'
|
5
|
+
|
6
|
+
module Makanai
|
7
|
+
module Migration
|
8
|
+
def execute_sql(sql_path:)
|
9
|
+
sql = File.read(sql_path)
|
10
|
+
puts "execute: #{sql_path}"
|
11
|
+
Makanai::Database.new.execute_sql(sql)
|
12
|
+
end
|
13
|
+
|
14
|
+
def migration_root_path
|
15
|
+
"#{Settings::APP_ROOT_PATH}#{Settings::MIGRATION_ROOT_PATH}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './database.rb'
|
4
|
+
|
5
|
+
module Makanai
|
6
|
+
class Model
|
7
|
+
class NotFound < StandardError; end
|
8
|
+
|
9
|
+
def initialize(attributes)
|
10
|
+
@origin_attributes = attributes
|
11
|
+
difine_attribute_methods
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :origin_attributes
|
15
|
+
|
16
|
+
def self.execute_sql(sql)
|
17
|
+
Makanai::Database.new.execute_sql(sql)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.all
|
21
|
+
results = execute_sql("SELECT * FROM #{self::TABLE_NAME};")
|
22
|
+
results.map { |result| new(result) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.find(key)
|
26
|
+
results = execute_sql(
|
27
|
+
<<~SQL
|
28
|
+
SELECT *
|
29
|
+
FROM #{self::TABLE_NAME}
|
30
|
+
WHERE #{self::PRYMARY_KEY} = #{buid_sql_text(key)}
|
31
|
+
LIMIT 1;
|
32
|
+
SQL
|
33
|
+
)
|
34
|
+
raise Makanai::Model::NotFound if results.empty?
|
35
|
+
new(results.pop)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.last
|
39
|
+
results = execute_sql(
|
40
|
+
<<~SQL
|
41
|
+
SELECT *
|
42
|
+
FROM #{self::TABLE_NAME}
|
43
|
+
ORDER BY #{self::PRYMARY_KEY} DESC
|
44
|
+
LIMIT 1;
|
45
|
+
SQL
|
46
|
+
)
|
47
|
+
new(results.pop)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.buid_sql_text(value)
|
51
|
+
case value
|
52
|
+
when String then "'#{value.gsub(/'/, "''")}'"
|
53
|
+
when Numeric, Integer then value
|
54
|
+
else value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def assign_attributes(attributes)
|
59
|
+
attributes.each { |key, val| send("#{key}=", val) }
|
60
|
+
self
|
61
|
+
end
|
62
|
+
|
63
|
+
def attributes
|
64
|
+
origin_attributes.map { |key, _val| [key, send(key)] }.to_h
|
65
|
+
end
|
66
|
+
|
67
|
+
def create
|
68
|
+
self.class.execute_sql(
|
69
|
+
<<~SQL
|
70
|
+
INSERT
|
71
|
+
INTO #{self.class::TABLE_NAME}(#{clumns.join(',')})
|
72
|
+
VALUES (#{insert_values.join(',')});
|
73
|
+
SQL
|
74
|
+
)
|
75
|
+
@origin_attributes = self.class.last.attributes
|
76
|
+
difine_attribute_methods
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
def update
|
81
|
+
self.class.execute_sql(
|
82
|
+
<<~SQL
|
83
|
+
UPDATE #{self.class::TABLE_NAME}
|
84
|
+
SET #{update_values.join(',')}
|
85
|
+
WHERE #{self.class::PRYMARY_KEY} = #{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))};
|
86
|
+
SQL
|
87
|
+
)
|
88
|
+
@origin_attributes = attributes
|
89
|
+
difine_attribute_methods
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
def delete
|
94
|
+
self.class.execute_sql(
|
95
|
+
<<~SQL
|
96
|
+
DELETE FROM #{self.class::TABLE_NAME}
|
97
|
+
WHERE #{self.class::PRYMARY_KEY} = #{self.class.buid_sql_text(send(self.class::PRYMARY_KEY))};
|
98
|
+
SQL
|
99
|
+
)
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def clumns
|
106
|
+
attributes.keys
|
107
|
+
end
|
108
|
+
|
109
|
+
def insert_values
|
110
|
+
attributes.values.map { |attribute| self.class.buid_sql_text(attribute).to_s }
|
111
|
+
end
|
112
|
+
|
113
|
+
def update_values
|
114
|
+
attributes.map { |key, val| "#{key}=#{self.class.buid_sql_text(val)}" }
|
115
|
+
end
|
116
|
+
|
117
|
+
def difine_attribute_methods
|
118
|
+
origin_attributes.each do |key, val|
|
119
|
+
instance_variable_set("@#{key}".to_sym, val)
|
120
|
+
self.class.class_eval { attr_accessor key.to_sym }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake'
|
4
|
+
require 'sqlite3'
|
5
|
+
require_relative './migration.rb'
|
6
|
+
|
7
|
+
namespace :makanai do
|
8
|
+
namespace :db do
|
9
|
+
desc 'execute migration'
|
10
|
+
task :migration do
|
11
|
+
include Makanai::Migration
|
12
|
+
|
13
|
+
puts "INFO: start migration #{ENV['target']}"
|
14
|
+
target = ENV['target']
|
15
|
+
if target == 'all'
|
16
|
+
sql_paths = Dir.glob("#{migration_root_path}*")
|
17
|
+
sql_paths.each { |sql_path| execute_sql(sql_path: sql_path) }
|
18
|
+
else
|
19
|
+
execute_sql(sql_path: "#{migration_root_path}#{target}")
|
20
|
+
end
|
21
|
+
puts "INFO: finished migration #{ENV['target']}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'rack'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Makanai
|
8
|
+
class Request < Rack::Request
|
9
|
+
attr_reader :env, :url, :query, :origin_body, :method, :params
|
10
|
+
|
11
|
+
def initialize(env)
|
12
|
+
super
|
13
|
+
@env = env
|
14
|
+
@url = build_url
|
15
|
+
@origin_body = body&.read
|
16
|
+
@query = parsed_url.query
|
17
|
+
@params = build_params
|
18
|
+
@method = build_method
|
19
|
+
end
|
20
|
+
|
21
|
+
def root_url
|
22
|
+
@root_url ||= "#{parsed_url.scheme}://#{parsed_url.host}:#{parsed_url.port}"
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def build_url
|
28
|
+
root = "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}"
|
29
|
+
root += ":#{env['SERVER_PORT']}#{env['PATH_INFO']}"
|
30
|
+
env['QUERY_STRING'] ? (root + "?#{env['QUERY_STRING']}") : root
|
31
|
+
end
|
32
|
+
|
33
|
+
def parsed_body
|
34
|
+
@parsed_body ||= JSON.parse(origin_body)
|
35
|
+
rescue JSON::ParserError
|
36
|
+
@parsed_body ||= origin_body.split('&').map { |param| param.split('=') }.to_h
|
37
|
+
end
|
38
|
+
|
39
|
+
def parsed_url
|
40
|
+
@parsed_url ||= URI.parse(url)
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_method
|
44
|
+
return 'GET' if env['REQUEST_METHOD'] == 'GET'
|
45
|
+
params&.delete('_method') || 'POST'
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_params
|
49
|
+
case env['REQUEST_METHOD']
|
50
|
+
when 'GET'
|
51
|
+
return Hash[URI.decode_www_form(query)] if query
|
52
|
+
when 'POST'
|
53
|
+
return parsed_body if origin_body
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rack'
|
4
|
+
|
5
|
+
module Makanai
|
6
|
+
class Response < Rack::Response
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
@header = { 'Content-Type' => 'text/html' }
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_accessor :header
|
13
|
+
|
14
|
+
def result
|
15
|
+
[status, header, body.map(&:to_s)]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
module Makanai
|
6
|
+
class Router
|
7
|
+
class NotFound < StandardError; end
|
8
|
+
|
9
|
+
attr_accessor :routes
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@routes = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(path, &block)
|
16
|
+
@routes << Route.new(path: path, process: block, method: 'GET')
|
17
|
+
end
|
18
|
+
|
19
|
+
def post(path, &block)
|
20
|
+
@routes << Route.new(path: path, process: block, method: 'POST')
|
21
|
+
end
|
22
|
+
|
23
|
+
def put(path, &block)
|
24
|
+
@routes << Route.new(path: path, process: block, method: 'PUT')
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete(path, &block)
|
28
|
+
@routes << Route.new(path: path, process: block, method: 'DELETE')
|
29
|
+
end
|
30
|
+
|
31
|
+
def bind!(url:, method:)
|
32
|
+
path = URI.parse(url).path
|
33
|
+
routes.find { |route| route.path == path && route.method == method }.tap do |route|
|
34
|
+
raise NotFound if route.nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Route
|
39
|
+
def initialize(path:, process:, method:)
|
40
|
+
@path = path
|
41
|
+
@process = process
|
42
|
+
@method = method
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_reader :path, :process, :method
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
module Makanai
|
6
|
+
class Template
|
7
|
+
def initialize(path:)
|
8
|
+
@path = path
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :path
|
12
|
+
|
13
|
+
def render
|
14
|
+
template_file = File.read(path)
|
15
|
+
ERB.new(template_file).result
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/makanai.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "makanai/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "makanai"
|
7
|
+
spec.version = Makanai::VERSION
|
8
|
+
spec.authors = ["Madogiwa"]
|
9
|
+
spec.email = ["madogiwa0124@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{simple web application framework for learning.}
|
12
|
+
spec.description = %q{simple web application framework for learning.}
|
13
|
+
spec.homepage = "https://github.com/Madogiwa0124/makanai"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/Madogiwa0124/makanai"
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_dependency "rack", "~> 2.0.7"
|
30
|
+
spec.add_dependency "sqlite3", "~> 1.4.1"
|
31
|
+
spec.add_development_dependency "rubocop", "~> 0.74"
|
32
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
34
|
+
end
|
data/sample/Rakefile
ADDED
data/sample/app.rb
ADDED
data/sample/db/.keep
ADDED
File without changes
|
File without changes
|
data/sample/models/.keep
ADDED
File without changes
|
data/sample/views/.keep
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: makanai
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Madogiwa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.0.7
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.0.7
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.4.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.4.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.74'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.74'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: simple web application framework for learning.
|
98
|
+
email:
|
99
|
+
- madogiwa0124@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".rubocop.yml"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENCE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- lib/makanai.rb
|
117
|
+
- lib/makanai/application.rb
|
118
|
+
- lib/makanai/database.rb
|
119
|
+
- lib/makanai/dsl.rb
|
120
|
+
- lib/makanai/main.rb
|
121
|
+
- lib/makanai/migration.rb
|
122
|
+
- lib/makanai/model.rb
|
123
|
+
- lib/makanai/rake_tasks.rb
|
124
|
+
- lib/makanai/request.rb
|
125
|
+
- lib/makanai/response.rb
|
126
|
+
- lib/makanai/router.rb
|
127
|
+
- lib/makanai/settings.rb
|
128
|
+
- lib/makanai/template.rb
|
129
|
+
- lib/makanai/version.rb
|
130
|
+
- makanai.gemspec
|
131
|
+
- sample/Rakefile
|
132
|
+
- sample/app.rb
|
133
|
+
- sample/db/.keep
|
134
|
+
- sample/migration/.keep
|
135
|
+
- sample/models/.keep
|
136
|
+
- sample/views/.keep
|
137
|
+
homepage: https://github.com/Madogiwa0124/makanai
|
138
|
+
licenses:
|
139
|
+
- MIT
|
140
|
+
metadata:
|
141
|
+
homepage_uri: https://github.com/Madogiwa0124/makanai
|
142
|
+
source_code_uri: https://github.com/Madogiwa0124/makanai
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubygems_version: 3.0.3
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: simple web application framework for learning.
|
162
|
+
test_files: []
|