makanai 0.1.1 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e90461a7d5e372f93f13f282e5f20b0410db84438b4eb9cbb587f0e739bd0c19
4
- data.tar.gz: a3dbff0cd0661110d7503889d768e6e277d8b3bf1241fcb13c62589ca8133d9d
3
+ metadata.gz: 8c7b32701bd75c6ea7cae7f893310a50e5e70a9fea7c14e9a99ba9559b2e7a39
4
+ data.tar.gz: 8890f0956507a1d0bfa92197718f40b4ed7da313737fe4501032565154f68e08
5
5
  SHA512:
6
- metadata.gz: 83066b43bf8207fdc6cc99763cd546504a324420e679c09c5aad6d98f65b7fb308cfd034af1b3e8508735de9693fb93919f55424b3ef04b53e45d5ae7ec8655f
7
- data.tar.gz: 047dcbdf9ed4afe1c4991b0355ba58f469d63211f840c7f6f28c24f47be07b6a20f595e6eb19636e018152f636f44acffd9ede2cf7993e4a143b0663d7a8a9ec
6
+ metadata.gz: 268af34e32c0cfd48753227a04bf604f6b1fe3fae0a2ef4bb709965c8d6ac73ec3b78a6c6813a2576c33e69e9c00506d60ef1edecd8fd4046bdb013f96081574
7
+ data.tar.gz: d112687c6207e860f5c0513f2530c26c0d388472d3058aae069dc98771c7e637636ef61dfb2300727fa7952c8d03d018d3649b7d086a3b395d98a4c33bb2e8eb
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
 
12
12
  # rspec failure tracking
13
13
  .rspec_status
14
+ /**/.DS_Store
@@ -1,13 +1,9 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.7.0
3
+ NewCops: enable
3
4
  Exclude:
4
5
  - './makanai.gemspec'
5
-
6
- Style/Documentation:
7
- Enabled: false
8
-
9
- Metrics/LineLength:
10
- Max: 90
6
+ - 'vendor/**/*'
11
7
 
12
8
  Metrics/ClassLength:
13
9
  Max: 120
@@ -17,8 +13,23 @@ Metrics/BlockLength:
17
13
  - 'spec/**/*'
18
14
  - 'lib/makanai/rake_tasks.rb'
19
15
 
16
+ Style/Documentation:
17
+ Enabled: false
18
+
20
19
  Style/ParallelAssignment:
21
20
  Enabled: false
22
21
 
22
+ Style/HashEachMethods:
23
+ Enabled: true
24
+
25
+ Style/HashTransformKeys:
26
+ Enabled: true
27
+
28
+ Style/HashTransformValues:
29
+ Enabled: true
30
+
31
+ Layout/LineLength:
32
+ Max: 90
33
+
23
34
  Layout/EmptyLineAfterGuardClause:
24
35
  Enabled: false
@@ -0,0 +1 @@
1
+ 2.7.1
@@ -1,7 +1,24 @@
1
1
  ---
2
- sudo: false
3
2
  language: ruby
4
3
  cache: bundler
4
+ services:
5
+ - postgresql
6
+ - mysql
5
7
  rvm:
6
- - 2.7.0
7
- before_install: gem install bundler -v 2.0.2
8
+ - 2.6.6
9
+ - 2.7.1
10
+ - ruby-head
11
+ os:
12
+ - linux
13
+ before_install:
14
+ - gem install bundler -v 2.1.4
15
+ - mysql -e 'CREATE DATABASE IF NOT EXISTS makanai;'
16
+ before_script:
17
+ - psql -c "create user makanai WITH PASSWORD 'password';" -U postgres
18
+ - psql -c 'create database makanai;' -U postgres
19
+ script:
20
+ - bundle exec rubocop --parallel
21
+ - bundle exec rspec
22
+ jobs:
23
+ allow_failures:
24
+ - rvm: ruby-head
@@ -13,3 +13,36 @@
13
13
  * :sparkles: add `Makanai::Model#first`.
14
14
  execute `SELECT * FROM table_name ORDER BY prymary_key asc limit 1` and return record object.
15
15
  https://github.com/Madogiwa0124/makanai/commit/8949796bf7428f5e2d854041b35397ef4d442ee6
16
+
17
+ ## :gift: 2020/02/22 `v0.1.2` released.
18
+ * :sparkles: use config object by Makanai::Settings for replaceable app config.
19
+ Changed the setting so that it is retained by the Class variable, and made it configurable.
20
+ The big advantage is that you can use any web server(ex. puma) except WEBrick.
21
+ https://github.com/Madogiwa0124/makanai/pull/18/commits/e6ea10e596c685ef77c042fa1e8f0294c85166f2
22
+
23
+ ## :gift: 2020/03/04 `v0.1.3` released.
24
+ * :sparkles: Multiple DBMS available.
25
+ you can use dbms(MySQL :dolphin:, PostgreSQL :elephant:) other then SQLite.
26
+ https://github.com/Madogiwa0124/makanai/pull/19
27
+
28
+ ## :gift: 2020/04/08 `v0.1.4` released.
29
+ * :sparkles: support lastest ruby versions(2.6.6, 2.7.1)
30
+ * :sparkles: support for dynamic URL routing.
31
+
32
+ ``` ruby
33
+ router.get '/hoge/:id' do
34
+ request.params['id']
35
+ end
36
+ ```
37
+
38
+ https://github.com/Madogiwa0124/makanai/commit/e256368fd0a13f1d0d2d6c6af17caef7760e64f0
39
+
40
+ ## :gift: 2020/06/16 `v0.1.5` released.
41
+ * :package: dependency update.
42
+
43
+ ## :gift: 2020/08/07 `v0.1.6` released.
44
+ * :sparkles: enabled to switch template engine Haml and ERB.
45
+ https://github.com/Madogiwa0124/makanai/commit/b9f977fe9e974aa1abbf2764a2fdad989e2767ec
46
+
47
+ * :zap: remove sqlite3 from runtime_dependency.
48
+ https://github.com/Madogiwa0124/makanai/commit/d63c3b12cf6fc412a8d88806dca9710e210e5fe0
data/Gemfile CHANGED
@@ -5,3 +5,5 @@ source 'https://rubygems.org'
5
5
  git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  gemspec
8
+
9
+ gem 'codecov', require: false
@@ -1,55 +1,82 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- makanai (0.1.1)
5
- rack (~> 2.0.7)
4
+ makanai (0.1.6)
5
+ rack (>= 2.0.7, < 2.3.0)
6
6
  rake (>= 10, < 14)
7
- sqlite3 (~> 1.4.1)
8
7
 
9
8
  GEM
10
9
  remote: https://rubygems.org/
11
10
  specs:
12
- ast (2.4.0)
11
+ ast (2.4.1)
12
+ codecov (0.2.3)
13
+ colorize
14
+ json
15
+ simplecov
16
+ colorize (0.8.1)
13
17
  diff-lcs (1.3)
14
- jaro_winkler (1.5.4)
15
- parallel (1.19.1)
16
- parser (2.7.0.0)
17
- ast (~> 2.4.0)
18
- rack (2.0.8)
18
+ docile (1.3.2)
19
+ haml (5.1.2)
20
+ temple (>= 0.8.0)
21
+ tilt
22
+ json (2.3.1)
23
+ mysql2 (0.5.3)
24
+ parallel (1.19.2)
25
+ parser (2.7.1.4)
26
+ ast (~> 2.4.1)
27
+ pg (1.2.3)
28
+ rack (2.2.3)
19
29
  rainbow (3.0.0)
20
30
  rake (13.0.1)
31
+ regexp_parser (1.7.1)
32
+ rexml (3.2.4)
21
33
  rspec (3.9.0)
22
34
  rspec-core (~> 3.9.0)
23
35
  rspec-expectations (~> 3.9.0)
24
36
  rspec-mocks (~> 3.9.0)
25
- rspec-core (3.9.0)
26
- rspec-support (~> 3.9.0)
27
- rspec-expectations (3.9.0)
37
+ rspec-core (3.9.1)
38
+ rspec-support (~> 3.9.1)
39
+ rspec-expectations (3.9.1)
28
40
  diff-lcs (>= 1.2.0, < 2.0)
29
41
  rspec-support (~> 3.9.0)
30
- rspec-mocks (3.9.0)
42
+ rspec-mocks (3.9.1)
31
43
  diff-lcs (>= 1.2.0, < 2.0)
32
44
  rspec-support (~> 3.9.0)
33
- rspec-support (3.9.0)
34
- rubocop (0.78.0)
35
- jaro_winkler (~> 1.5.1)
45
+ rspec-support (3.9.2)
46
+ rubocop (0.89.0)
36
47
  parallel (~> 1.10)
37
- parser (>= 2.6)
48
+ parser (>= 2.7.1.1)
38
49
  rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.7)
51
+ rexml
52
+ rubocop-ast (>= 0.1.0, < 1.0)
39
53
  ruby-progressbar (~> 1.7)
40
- unicode-display_width (>= 1.4.0, < 1.7)
54
+ unicode-display_width (>= 1.4.0, < 2.0)
55
+ rubocop-ast (0.3.0)
56
+ parser (>= 2.7.1.4)
41
57
  ruby-progressbar (1.10.1)
58
+ simplecov (0.18.5)
59
+ docile (~> 1.1)
60
+ simplecov-html (~> 0.11)
61
+ simplecov-html (0.12.2)
42
62
  sqlite3 (1.4.2)
43
- unicode-display_width (1.6.0)
63
+ temple (0.8.2)
64
+ tilt (2.0.10)
65
+ unicode-display_width (1.7.0)
44
66
 
45
67
  PLATFORMS
46
68
  ruby
47
69
 
48
70
  DEPENDENCIES
49
71
  bundler (~> 2.0)
72
+ codecov
73
+ haml (~> 5.0)
50
74
  makanai!
75
+ mysql2 (~> 0.5)
76
+ pg (~> 1.2)
51
77
  rspec (~> 3.0)
52
78
  rubocop (~> 0.74)
79
+ sqlite3 (~> 1.4.1)
53
80
 
54
81
  BUNDLED WITH
55
- 2.1.2
82
+ 2.1.4
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/makanai.svg)](https://badge.fury.io/rb/makanai)
4
4
  [![Build Status](https://travis-ci.com/Madogiwa0124/makanai.svg?branch=master)](https://travis-ci.com/Madogiwa0124/makanai)
5
+ [![codecov](https://codecov.io/gh/Madogiwa0124/makanai/branch/master/graph/badge.svg)](https://codecov.io/gh/Madogiwa0124/makanai)
5
6
 
6
7
  simple web application framework for learning.
7
8
 
@@ -23,12 +24,6 @@ Or install it yourself as:
23
24
 
24
25
  $ gem install makanai
25
26
 
26
- ## Dependencies
27
-
28
- Makanai depends on the following, so please install it in advance.
29
-
30
- * [SQLite3](https://www.sqlite.org/index.html)
31
-
32
27
  ## Start sample project
33
28
  Getting started with Makanai is easy.
34
29
 
@@ -47,7 +42,7 @@ $ ruby app.rb
47
42
  create a ruby ​​file(ex. app.rb).
48
43
 
49
44
  ``` ruby
50
- require_relative '../lib/main.rb'
45
+ require 'makanai/main'
51
46
 
52
47
  router.get '/' do
53
48
  'Hello Makanai!'
@@ -76,10 +71,57 @@ root ┬ app.rb # main application file
76
71
  └ db # use Sqlite3 dabase file
77
72
  ```
78
73
 
74
+ ## override application config
75
+
76
+ You can overwrite the settings (`Makanai :: Settings`) with the created ruby ​​file(ex. app.rb).
77
+
78
+ ex) When overriding rack app settings.
79
+
80
+ ``` ruby
81
+ Makanai::Settings.rack_app_config = { handler: :webrick, host: '0.0.0.0', port: '8081' }
82
+ ```
83
+
84
+ ### use other application server
85
+
86
+ Add rack web server(ex. puma) gem in your Gemfile.
87
+
88
+ ``` ruby
89
+ gem 'puma'
90
+ ```
91
+
92
+ Overwrite the `handler` in `rack_app_config` with the created ruby ​​file(ex. app.rb).
93
+
94
+ ``` ruby
95
+ Makanai::Settings.rack_app_config = { handler: :puma, host: '0.0.0.0', port: '8080' }
96
+ ```
97
+
98
+ ### use dbms
99
+
100
+ Makanai use `sqlite` by default, and make `db/makanai.db`.
101
+
102
+ If you use another dbms, install dbms(PostgreSQL or MySQL). And add dbms gem (`pg` or `mysql2`) in your Gemfile.
103
+
104
+ ``` ruby
105
+ gem 'pg'
106
+ ```
107
+
108
+ Overwrite the `databse_client` and `databse_config` with the created ruby ​​file(ex. app.rb) and Rakefile.
109
+
110
+ ``` ruby
111
+ Makanai::Settings.databse_client = :postgres
112
+ Makanai::Settings.databse_config = {
113
+ host: 'localhost',
114
+ password:'password',
115
+ dbname: 'makanai',
116
+ port: 5432
117
+ }
118
+ ```
119
+
79
120
  ## routing
121
+ Routing is searched in the order defined and call block args.
80
122
 
81
123
  ``` ruby
82
- require_relative '../lib/main.rb'
124
+ require 'makanai/main'
83
125
 
84
126
  # root path
85
127
  router.get '/' do
@@ -91,11 +133,17 @@ router.get '/hoge' do
91
133
  'Hello Hoge!'
92
134
  end
93
135
 
94
- # enable access to /params with get parameter
95
- router.get '/check' do |request|
136
+ # enable access to /hoge with get parameter(?hoge=fuga)
137
+ router.get '/hoge' do |request|
96
138
  request.params['hoge']
97
139
  end
98
140
 
141
+ # enable access to /hoge/:id with dinamic url args({ 'id' => '1'})
142
+ # NOTE: dinamics prefix is `:`.
143
+ router.get '/hoge/:id' do
144
+ request.params['id']
145
+ end
146
+
99
147
  # enable access to /resources with method post and redirect other url.
100
148
  router.post '/resources' do |request|
101
149
  Resource.new(request.params).create
@@ -117,22 +165,33 @@ router.delete '/resources' do |request|
117
165
  end
118
166
  ```
119
167
 
120
- ## erb render
168
+ ## rendering
121
169
 
122
- Define instance variables used in the routing view.
170
+ Define instance variables used in the routing view. And specify template engine(`ERB` or `Haml`).
123
171
 
124
172
  ``` ruby
125
- require_relative '../lib/main.rb'
173
+ require 'makanai/main'
174
+
175
+ # setting template engine(default: :erb).
176
+ Makanai::Settings.template_engine = :haml
177
+
178
+ router.get '/index' do
179
+ @title = 'Makanai title'
180
+ @body = 'Makanai body'
181
+ render :index # render default template engine.
182
+ end
126
183
 
127
184
  router.get '/index' do
128
185
  @title = 'Makanai title'
129
186
  @body = 'Makanai body'
130
- render :index
187
+ render :index, :haml # render specified template engine.
131
188
  end
132
189
  ```
133
190
 
134
191
  Create an erb file in `src/views` with the name specified in render.
135
192
 
193
+ erb
194
+
136
195
  ``` html
137
196
  <!-- src/views/index.erb -->
138
197
  <html>
@@ -146,9 +205,22 @@ Create an erb file in `src/views` with the name specified in render.
146
205
  </html>
147
206
  ```
148
207
 
208
+ haml
209
+
210
+ ``` haml
211
+ !!!
212
+ %html
213
+ %head
214
+ %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
215
+ %meta{:charset => "UTF-8"}/
216
+ %title= @title
217
+ %body
218
+ = @body
219
+ ```
220
+
149
221
  ## migration
150
222
 
151
- makanai uses `sqlite3` as db. create db and migrate schema when executed `rake makanai:db:migration`.
223
+ migrate schema when executed `rake makanai:db:migration`.
152
224
 
153
225
  ```
154
226
  # execute all migraiton sql
@@ -178,7 +250,7 @@ INFO: finished migration all
178
250
  It can be used by creating a class that inherits `Makanai::Model`.
179
251
 
180
252
  ``` ruby
181
- require_relative '../../lib/model.rb'
253
+ require 'makanai/model'
182
254
 
183
255
  class Resource < Makanai::Model
184
256
  # target table when executing sql.
@@ -215,6 +287,22 @@ Resource.find(1).delete
215
287
 
216
288
  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.
217
289
 
290
+ ## setup develop enviroment
291
+
292
+ Use docker-compose to build `mysql` and` postgres` containers for development.
293
+
294
+ ``` sh
295
+ $ docker-compose up -d
296
+ Creating makanai_mysql_1 ... done
297
+ Creating makanai_postgres_1 ... done
298
+
299
+ $ docker-compose ps
300
+ Name Command State Ports
301
+ ----------------------------------------------------------------------------------------------
302
+ makanai_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
303
+ makanai_postgres_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
304
+ ```
305
+
218
306
  # License
219
307
 
220
308
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "makanai"
4
+ require 'bundler/setup'
5
+ require 'makanai'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "makanai"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -0,0 +1,14 @@
1
+ mysql:
2
+ image: mysql
3
+ ports:
4
+ - 3306:3306
5
+ environment:
6
+ - MYSQL_DATABASE=makanai
7
+ - MYSQL_ALLOW_EMPTY_PASSWORD=yes
8
+ postgres:
9
+ image: postgres
10
+ ports:
11
+ - 5432:5432
12
+ environment:
13
+ - POSTGRES_USER=makanai
14
+ - POSTGRES_PASSWORD=password
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'makanai'
4
5
  require 'makanai/rake_tasks'
5
6
 
6
- if ARGV[0] == 'init'
7
- Rake::Task['makanai:initialize:app'].execute
8
- end
7
+ Rake::Task['makanai:initialize:app'].execute if ARGV[0] == 'init'
@@ -1,4 +1,6 @@
1
- require "makanai/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'makanai/version'
2
4
 
3
5
  module Makanai
4
6
  def self.root
@@ -3,21 +3,27 @@
3
3
  require 'json'
4
4
  require 'uri'
5
5
  require 'rack'
6
- require_relative './request.rb'
7
- require_relative './response.rb'
8
- require_relative './router.rb'
6
+ require_relative './settings'
7
+ require_relative './request'
8
+ require_relative './response'
9
+ require_relative './router'
9
10
 
10
11
  module Makanai
11
12
  class Application
12
13
  def initialize(router:)
13
14
  @router = router
14
- @handler = Rack::Handler::WEBrick
15
+ @config = Settings
15
16
  end
16
17
 
17
- attr_reader :router, :handler, :request, :response
18
+ attr_reader :router, :config, :request, :response
18
19
 
19
20
  def run!
20
- handler.run self
21
+ app_config = config.rack_app_config
22
+ handler = Rack::Handler.get(app_config[:handler])
23
+ handler.run(self, { Host: app_config[:host], Port: app_config[:port] })
24
+ rescue Interrupt
25
+ handler.shutdown
26
+ puts '==== Goodbye! :) ===='
21
27
  end
22
28
 
23
29
  def call(env)
@@ -32,7 +38,10 @@ module Makanai
32
38
  private
33
39
 
34
40
  def execute_route
35
- router.bind!(url: request.url, method: request.method).process.call(request)
41
+ route = router.bind!(url: request.url, method: request.method)
42
+ # NOTE: merge dynamic url params (ex. /resources/1 -> { 'id' => '1' })
43
+ @request.params.merge!(route.url_args)
44
+ route.process.call(request)
36
45
  rescue Makanai::Router::NotFound
37
46
  @response.status = 404
38
47
  nil
@@ -1,29 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sqlite3'
4
- require_relative './settings.rb'
3
+ require_relative './settings'
5
4
 
6
5
  module Makanai
7
6
  class Database
8
- DATABASE_PATH = "#{Settings::APP_ROOT_PATH}#{Settings::DATABASE_PATH}"
7
+ class UnsupportedException < StandardError; end
9
8
 
10
- def initialize(path: DATABASE_PATH)
11
- @handler = SQLite3::Database
12
- @db = handler.new path
13
- db.tap { |db| db.results_as_hash = true }
9
+ def initialize(client: Settings.databse_client, config: Settings.databse_config)
10
+ client_class = client_class(client)
11
+ @client = client_class.new(config)
14
12
  end
15
13
 
16
- attr_reader :handler, :db
14
+ attr_reader :client
17
15
 
18
16
  def execute_sql(sql)
19
17
  puts "SQL: #{sql.gsub("\n", ' ')}"
20
- db.execute(sql).tap { close_db }
18
+ client.execute_sql(sql)
21
19
  end
22
20
 
23
21
  private
24
22
 
25
- def close_db
26
- db.close
23
+ def client_class(client)
24
+ require_relative File.join('dbms', client.to_s)
25
+ Object.const_get("Makanai::Dbms::#{client.capitalize}")
26
+ rescue LoadError
27
+ raise UnsupportedException
27
28
  end
28
29
  end
29
30
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makanai
4
+ module Dbms
5
+ class Base
6
+ def db
7
+ raise NotImplementedError
8
+ end
9
+
10
+ def execute_sql
11
+ raise NotImplementedError
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mysql2'
4
+ require_relative './base'
5
+ module Makanai
6
+ module Dbms
7
+ class Mysql < Base
8
+ def initialize(config)
9
+ super()
10
+ @db = Mysql2::Client.new(config || default_config)
11
+ end
12
+
13
+ attr_reader :db
14
+
15
+ def execute_sql(sql)
16
+ db.query(sql).to_a.tap { close_db }
17
+ end
18
+
19
+ private
20
+
21
+ def default_config
22
+ {
23
+ host: '0.0.0.0',
24
+ username: 'root',
25
+ password: 'password',
26
+ database: 'makanai',
27
+ port: 3306
28
+ }
29
+ end
30
+
31
+ def close_db
32
+ db.close
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require_relative './base'
5
+ module Makanai
6
+ module Dbms
7
+ class Postgres < Base
8
+ def initialize(config)
9
+ super()
10
+ @db = PG.connect(config || default_config)
11
+ db.type_map_for_results = PG::BasicTypeMapForResults.new(db)
12
+ end
13
+
14
+ attr_reader :db
15
+
16
+ def execute_sql(sql)
17
+ db.exec(sql).each.to_a.tap { close_db }
18
+ end
19
+
20
+ private
21
+
22
+ def default_config
23
+ {
24
+ host: 'localhost',
25
+ password: nil,
26
+ dbname: 'makanai',
27
+ port: 5432
28
+ }
29
+ end
30
+
31
+ def close_db
32
+ db.finish
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sqlite3'
4
+ require_relative './base'
5
+ module Makanai
6
+ module Dbms
7
+ class Sqlite < Base
8
+ def initialize(config)
9
+ super()
10
+ @db = SQLite3::Database.new(config[:path])
11
+ db.tap { |db| db.results_as_hash = true }
12
+ end
13
+
14
+ attr_reader :db
15
+
16
+ def execute_sql(sql)
17
+ db.execute(sql).tap { close_db }
18
+ end
19
+
20
+ private
21
+
22
+ def close_db
23
+ db.close
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './template.rb'
4
- require_relative './response.rb'
5
- require_relative './settings.rb'
3
+ require_relative './template'
4
+ require_relative './response'
5
+ require_relative './settings'
6
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
7
+ def render(path, engine = Makanai::Settings.template_engine)
8
+ template_root_path = Makanai::Settings.template_full_path
9
+ full_path = File.join(template_root_path, path.to_s)
10
+ # NOTE: Get all instance variables in main by Hash
11
+ locals = instance_variables.map { |name| [name, instance_variable_get(name)] }.to_h
12
+ Makanai::Template.new(path: full_path, engine: engine, locals: locals).render
11
13
  end
12
14
 
13
15
  def redirect_to(url)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'erb'
4
4
  require 'yaml'
5
- require_relative '../makanai.rb'
5
+ require_relative '../makanai'
6
6
 
7
7
  module Makanai
8
8
  class Generator
@@ -10,6 +10,7 @@ module Makanai
10
10
  DIRECTORY_NAMES = YAML.load_file("#{ROOT_PATH}/application/directories.yaml")
11
11
  APP_TEMPLATE = File.read("#{ROOT_PATH}/application/templates/app.erb")
12
12
  RAKEFILE_TEMPLATE = File.read("#{ROOT_PATH}/application/templates/rakefile.erb")
13
+ GEMFILE_TEMPLATE = File.read("#{ROOT_PATH}/application/templates/gemfile.erb")
13
14
 
14
15
  def initialize(path = Dir.pwd)
15
16
  @path = path
@@ -39,6 +40,12 @@ module Makanai
39
40
  end
40
41
  end
41
42
 
43
+ def create_gemfile(template = GEMFILE_TEMPLATE)
44
+ File.join(path, 'Gemfile').tap do |file_path|
45
+ create_file(file_path, ERB.new(template).result)
46
+ end
47
+ end
48
+
42
49
  private
43
50
 
44
51
  def create_file(file_path, content)
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ gem 'makanai'
4
+ gem 'sqlite3'
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './application.rb'
4
- require_relative './router.rb'
5
- require_relative './dsl.rb'
3
+ require_relative './application'
4
+ require_relative './router'
5
+ require_relative './dsl'
6
6
 
7
7
  def router
8
8
  @router ||= Makanai::Router.new
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './settings.rb'
4
- require_relative './database.rb'
3
+ require_relative './settings'
4
+ require_relative './database'
5
5
 
6
6
  module Makanai
7
7
  module Migration
@@ -12,7 +12,7 @@ module Makanai
12
12
  end
13
13
 
14
14
  def migration_root_path
15
- "#{Settings::APP_ROOT_PATH}#{Settings::MIGRATION_ROOT_PATH}"
15
+ Settings.migration_full_path
16
16
  end
17
17
  end
18
18
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './database.rb'
3
+ require_relative './database'
4
4
 
5
5
  module Makanai
6
6
  class Model
@@ -1,9 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rake'
4
- require 'sqlite3'
5
- require_relative './migration.rb'
6
- require_relative './generator.rb'
4
+ require_relative './migration'
5
+ require_relative './generator'
7
6
 
8
7
  namespace :makanai do
9
8
  namespace :initialize do
@@ -14,6 +13,7 @@ namespace :makanai do
14
13
  puts generator.create_app_directories
15
14
  puts generator.create_app_rb
16
15
  puts generator.create_rakefile
16
+ puts generator.create_gemfile
17
17
  puts 'INFO: finished ganerate app'
18
18
  end
19
19
  end
@@ -25,11 +25,12 @@ namespace :makanai do
25
25
 
26
26
  puts "INFO: start migration #{ENV['target']}"
27
27
  target = ENV['target']
28
+ pp migration_root_path
28
29
  if target == 'all'
29
30
  sql_paths = Dir.glob("#{migration_root_path}*")
30
31
  sql_paths.each { |sql_path| execute_sql(sql_path: sql_path) }
31
32
  else
32
- execute_sql(sql_path: "#{migration_root_path}#{target}")
33
+ execute_sql(sql_path: File.join(migration_root_path, target))
33
34
  end
34
35
  puts "INFO: finished migration #{ENV['target']}"
35
36
  end
@@ -6,7 +6,8 @@ require 'uri'
6
6
 
7
7
  module Makanai
8
8
  class Request < Rack::Request
9
- attr_reader :env, :url, :query, :origin_body, :method, :params
9
+ attr_reader :env, :url, :query, :origin_body, :method
10
+ attr_accessor :params
10
11
 
11
12
  def initialize(env)
12
13
  super
@@ -30,19 +30,50 @@ module Makanai
30
30
 
31
31
  def bind!(url:, method:)
32
32
  path = URI.parse(url).path
33
- routes.find { |route| route.path == path && route.method == method }.tap do |route|
33
+ routes.find { |route| route.match?(path: path, method: method) }.tap do |route|
34
34
  raise NotFound if route.nil?
35
35
  end
36
36
  end
37
37
 
38
38
  class Route
39
+ DYNAMIC_PREFIX = ':' # Prefix for dynamic path
40
+
39
41
  def initialize(path:, process:, method:)
40
42
  @path = path
41
43
  @process = process
42
44
  @method = method
45
+ @url_args = {}
46
+ end
47
+
48
+ attr_reader :path, :process, :method, :url_args
49
+
50
+ def match?(path:, method:)
51
+ build_url_args(path: path)
52
+ path_match?(path) && self.method == method
53
+ end
54
+
55
+ private
56
+
57
+ def dynamic_indx
58
+ @dynamic_indx ||= path.split('/').map.with_index do |val, i|
59
+ i if val.include?(DYNAMIC_PREFIX)
60
+ end.compact
43
61
  end
44
62
 
45
- attr_reader :path, :process, :method
63
+ def path_match?(path)
64
+ request_path, route_path = [path, self.path].map(&->(array) { array.split('/') })
65
+ request_path.each.with_index do |val, i|
66
+ route_path[i] = val if dynamic_indx.include?(i)
67
+ end
68
+ route_path == request_path
69
+ end
70
+
71
+ def build_url_args(path:)
72
+ request_path, route_path = [path, self.path].map(&->(array) { array.split('/') })
73
+ dynamic_indx.each do |i|
74
+ @url_args.merge!({ route_path[i].gsub(DYNAMIC_PREFIX, '') => request_path[i] })
75
+ end
76
+ end
46
77
  end
47
78
  end
48
79
  end
@@ -1,10 +1,67 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rack'
4
+
3
5
  module Makanai
4
6
  class Settings
5
- APP_ROOT_PATH = Dir.pwd
6
- DATABASE_PATH = '/db/makanai.db'
7
- TEMPLATE_ROOT_PATH = '/views/'
8
- MIGRATION_ROOT_PATH = '/migration/'
7
+ DEFAULT_APP_ROOT_PATH = Dir.pwd
8
+ DEFAULT_DATABASE_PATH = '/db/makanai.db'
9
+ DEFAULT_TEMPLATE_ROOT_PATH = '/views/'
10
+ DEFAULT_MIGRATION_ROOT_PATH = '/migration/'
11
+ DEFAULT_RACK_APP_CONFIG = { handler: :webrick, host: '0.0.0.0', port: '8080' }.freeze
12
+ DEFAULT_TEMPLATE_ENGINE = :erb
13
+
14
+ @app_root_path = DEFAULT_APP_ROOT_PATH
15
+ @database_path = DEFAULT_DATABASE_PATH
16
+ @template_root_path = DEFAULT_TEMPLATE_ROOT_PATH
17
+ @template_engine = DEFAULT_TEMPLATE_ENGINE
18
+ @migration_root_path = DEFAULT_MIGRATION_ROOT_PATH
19
+ @rack_app_config = DEFAULT_RACK_APP_CONFIG
20
+
21
+ # NOTE: Use Sqlite3
22
+ @databse_client = :sqlite
23
+ @databse_config = { path: File.join(@app_root_path, @database_path) }
24
+
25
+ # NOTE: Use Postgresql
26
+ # @databse_client = :postgres
27
+ # @databse_config = {
28
+ # host: 'localhost',
29
+ # password: 'password',
30
+ # dbname: 'makanai',
31
+ # port: 5432
32
+ # }.freeze
33
+
34
+ # NOTE: Use MySQL
35
+ # @databse_client = :mysql
36
+ # @databse_config = {
37
+ # host: 'localhost',
38
+ # username: 'root',
39
+ # password: 'password',
40
+ # database: 'makanai',
41
+ # port: 3306
42
+ # }.freeze
43
+
44
+ class << self
45
+ attr_accessor :app_root_path,
46
+ :database_path,
47
+ :template_root_path,
48
+ :template_engine,
49
+ :migration_root_path,
50
+ :rack_app_config,
51
+ :databse_client,
52
+ :databse_config
53
+
54
+ def database_full_path
55
+ File.join(app_root_path, database_path)
56
+ end
57
+
58
+ def template_full_path
59
+ File.join(app_root_path, template_root_path)
60
+ end
61
+
62
+ def migration_full_path
63
+ File.join(app_root_path, migration_root_path)
64
+ end
65
+ end
9
66
  end
10
67
  end
@@ -1,18 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'erb'
4
-
5
3
  module Makanai
6
4
  class Template
7
- def initialize(path:)
5
+ class UnsupportedException < StandardError; end
6
+
7
+ def initialize(path:, engine: :erb, locals: {})
8
8
  @path = path
9
+ @engine = engine
10
+ @locals = locals
9
11
  end
10
12
 
11
- attr_reader :path
13
+ attr_reader :path, :engine, :locals
12
14
 
13
15
  def render
14
- template_file = File.read(path)
15
- ERB.new(template_file).result
16
+ template_file = File.read("#{path}.#{engine}")
17
+ engine_class.new(text: template_file, locals: locals).result
18
+ end
19
+
20
+ private
21
+
22
+ def engine_class
23
+ require_relative File.join('template_engine', engine.to_s)
24
+ Object.const_get("Makanai::TemplateEngine::#{engine.capitalize}")
25
+ rescue LoadError
26
+ raise UnsupportedException
16
27
  end
17
28
  end
18
29
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Makanai
4
+ module TemplateEngine
5
+ class Base
6
+ def result
7
+ raise NotImplementedError
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'erb'
4
+ require 'ostruct'
5
+ require_relative './base'
6
+
7
+ module Makanai
8
+ module TemplateEngine
9
+ class Erb < Base
10
+ def initialize(text:, locals: {})
11
+ super()
12
+ @text = text
13
+ @locals = locals
14
+ end
15
+
16
+ attr_reader :text, :locals
17
+
18
+ def result
19
+ # NOTE: ERB is need to pass the binding.
20
+ # So, pass the binding of the object that defined the instance variable.
21
+ # ref: https://docs.ruby-lang.org/en/2.7.0/ERB.html#method-i-result
22
+ ERB.new(text).result(Locals.new(locals).self_binding)
23
+ end
24
+ class Locals
25
+ def initialize(locals)
26
+ locals.each { |key, val| instance_variable_set(key, val) }
27
+ end
28
+
29
+ def self_binding
30
+ binding
31
+ end
32
+ end
33
+
34
+ private_constant :Locals
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'haml'
4
+ require_relative './base'
5
+
6
+ module Makanai
7
+ module TemplateEngine
8
+ class Haml < Base
9
+ def initialize(text:, locals: {})
10
+ super()
11
+ @text = text
12
+ @locals = locals
13
+ end
14
+
15
+ attr_reader :text, :locals
16
+
17
+ def result
18
+ # ref: http://haml.info/docs/yardoc/Haml/Engine.html#render-instance_method
19
+ ::Haml::Engine.new(text).render(Object.new, locals)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Makanai
2
- VERSION = "0.1.1"
4
+ VERSION = '0.1.6'
3
5
  end
@@ -26,9 +26,12 @@ Gem::Specification.new do |spec|
26
26
  spec.require_paths = ["lib"]
27
27
 
28
28
  spec.add_dependency "rake", ">= 10", "< 14"
29
- spec.add_dependency "rack", "~> 2.0.7"
30
- spec.add_dependency "sqlite3", "~> 1.4.1"
29
+ spec.add_dependency "rack", ">= 2.0.7", "< 2.3.0"
30
+ spec.add_development_dependency "sqlite3", "~> 1.4.1"
31
31
  spec.add_development_dependency "rubocop", "~> 0.74"
32
32
  spec.add_development_dependency "bundler", "~> 2.0"
33
33
  spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "pg", "~> 1.2"
35
+ spec.add_development_dependency "mysql2", "~> 0.5"
36
+ spec.add_development_dependency "haml", "~> 5.0"
34
37
  end
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'makanai/rake_tasks.rb'
3
+ require 'makanai/rake_tasks'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: makanai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Madogiwa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-28 00:00:00.000000000 Z
11
+ date: 2020-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -34,16 +34,22 @@ dependencies:
34
34
  name: rack
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - "~>"
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: 2.0.7
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: 2.3.0
40
43
  type: :runtime
41
44
  prerelease: false
42
45
  version_requirements: !ruby/object:Gem::Requirement
43
46
  requirements:
44
- - - "~>"
47
+ - - ">="
45
48
  - !ruby/object:Gem::Version
46
49
  version: 2.0.7
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
47
53
  - !ruby/object:Gem::Dependency
48
54
  name: sqlite3
49
55
  requirement: !ruby/object:Gem::Requirement
@@ -51,7 +57,7 @@ dependencies:
51
57
  - - "~>"
52
58
  - !ruby/object:Gem::Version
53
59
  version: 1.4.1
54
- type: :runtime
60
+ type: :development
55
61
  prerelease: false
56
62
  version_requirements: !ruby/object:Gem::Requirement
57
63
  requirements:
@@ -100,6 +106,48 @@ dependencies:
100
106
  - - "~>"
101
107
  - !ruby/object:Gem::Version
102
108
  version: '3.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: pg
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '1.2'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '1.2'
123
+ - !ruby/object:Gem::Dependency
124
+ name: mysql2
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.5'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '0.5'
137
+ - !ruby/object:Gem::Dependency
138
+ name: haml
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '5.0'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '5.0'
103
151
  description: simple web application framework for learning.
104
152
  email:
105
153
  - madogiwa0124@gmail.com
@@ -111,6 +159,7 @@ files:
111
159
  - ".gitignore"
112
160
  - ".rspec"
113
161
  - ".rubocop.yml"
162
+ - ".ruby-version"
114
163
  - ".travis.yml"
115
164
  - CHANGELOG.md
116
165
  - CODE_OF_CONDUCT.md
@@ -121,14 +170,20 @@ files:
121
170
  - Rakefile
122
171
  - bin/console
123
172
  - bin/setup
173
+ - docker-compose.yml
124
174
  - exe/makanai
125
175
  - lib/makanai.rb
126
176
  - lib/makanai/application.rb
127
177
  - lib/makanai/database.rb
178
+ - lib/makanai/dbms/base.rb
179
+ - lib/makanai/dbms/mysql.rb
180
+ - lib/makanai/dbms/postgres.rb
181
+ - lib/makanai/dbms/sqlite.rb
128
182
  - lib/makanai/dsl.rb
129
183
  - lib/makanai/generator.rb
130
184
  - lib/makanai/generator/application/directories.yaml
131
185
  - lib/makanai/generator/application/templates/app.erb
186
+ - lib/makanai/generator/application/templates/gemfile.erb
132
187
  - lib/makanai/generator/application/templates/rakefile.erb
133
188
  - lib/makanai/main.rb
134
189
  - lib/makanai/migration.rb
@@ -139,6 +194,9 @@ files:
139
194
  - lib/makanai/router.rb
140
195
  - lib/makanai/settings.rb
141
196
  - lib/makanai/template.rb
197
+ - lib/makanai/template_engine/base.rb
198
+ - lib/makanai/template_engine/erb.rb
199
+ - lib/makanai/template_engine/haml.rb
142
200
  - lib/makanai/version.rb
143
201
  - makanai.gemspec
144
202
  - sample/Rakefile