makanai 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c35e25ab94dce7eb78c2e81cd7d1086ba736c81d99ba81c17ae85cfaa88dcd25
4
- data.tar.gz: fa52e627e552253a97ce59065ab456406e75901667c093fb7c160ac243bb5a35
3
+ metadata.gz: 50ec7b4b39d4f324b5bb6d2c36c0f9f03274fd5b224862b4142d28cde7950a0f
4
+ data.tar.gz: d3b3716c39366f43b142c67f82ab7763ce6b65d07e3ef19a40347a56c71920a7
5
5
  SHA512:
6
- metadata.gz: 8b2661b6a8e5df30a371e60ad9123b3814df7db48f02c91a2eae1e3e2acb3961b711aa5880aa70c103b0d78345ffbd9e4b097196e519e12821dd81951c554552
7
- data.tar.gz: 0b91f2e2a8a3a03406506d8323ff05dccd22be62bf1d5ddb254b32771b8f99488094457bde8d5dd63367d73ac325d7d41a0e0d930ba4e5d7da75127e93118ab2
6
+ metadata.gz: 181e579b7e1d48853f06756c1f23f74b4ed15bcc083ac504df6fc716010ce99b3552216ebea3854a484a35256d6e7ef69031c2b8cc11c5d5c6165fcef3e53bc0
7
+ data.tar.gz: ed57e62dabd988e754728cd7677bfb397e1836bd499cde76ebaed7ccc27849e728fed84b473eb325ba7cb5e527d97a52cdcdf4cef183df4cb295f348827efae3
data/.travis.yml CHANGED
@@ -2,6 +2,13 @@
2
2
  sudo: false
3
3
  language: ruby
4
4
  cache: bundler
5
+ services:
6
+ - postgresql
7
+ - mysql
5
8
  rvm:
6
9
  - 2.7.0
7
- before_install: gem install bundler -v 2.0.2
10
+ before_install:
11
+ - gem install bundler -v 2.0.2
12
+ - mysql -e 'CREATE DATABASE IF NOT EXISTS makanai;'
13
+ before_script:
14
+ - psql -c 'create database makanai;' -U postgres
data/CHANGELOG.md CHANGED
@@ -19,3 +19,8 @@
19
19
  Changed the setting so that it is retained by the Class variable, and made it configurable.
20
20
  The big advantage is that you can use any web server(ex. puma) except WEBrick.
21
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
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- makanai (0.1.2)
4
+ makanai (0.1.3)
5
5
  rack (>= 2.0.7, < 2.3.0)
6
6
  rake (>= 10, < 14)
7
7
  sqlite3 (~> 1.4.1)
@@ -12,9 +12,11 @@ GEM
12
12
  ast (2.4.0)
13
13
  diff-lcs (1.3)
14
14
  jaro_winkler (1.5.4)
15
+ mysql2 (0.5.3)
15
16
  parallel (1.19.1)
16
- parser (2.7.0.2)
17
+ parser (2.7.0.4)
17
18
  ast (~> 2.4.0)
19
+ pg (1.2.2)
18
20
  rack (2.2.2)
19
21
  rainbow (3.0.0)
20
22
  rake (13.0.1)
@@ -32,7 +34,7 @@ GEM
32
34
  diff-lcs (>= 1.2.0, < 2.0)
33
35
  rspec-support (~> 3.9.0)
34
36
  rspec-support (3.9.0)
35
- rubocop (0.80.0)
37
+ rubocop (0.80.1)
36
38
  jaro_winkler (~> 1.5.1)
37
39
  parallel (~> 1.10)
38
40
  parser (>= 2.7.0.1)
@@ -50,6 +52,8 @@ PLATFORMS
50
52
  DEPENDENCIES
51
53
  bundler (~> 2.0)
52
54
  makanai!
55
+ mysql2 (~> 0.5)
56
+ pg (~> 1.2)
53
57
  rspec (~> 3.0)
54
58
  rubocop (~> 0.74)
55
59
 
data/README.md CHANGED
@@ -47,7 +47,7 @@ $ ruby app.rb
47
47
  create a ruby ​​file(ex. app.rb).
48
48
 
49
49
  ``` ruby
50
- require_relative '../lib/main.rb'
50
+ require 'makanai/main'
51
51
 
52
52
  router.get '/' do
53
53
  'Hello Makanai!'
@@ -100,6 +100,26 @@ Overwrite the `handler` in `rack_app_config` with the created ruby ​​file(ex
100
100
  Makanai::Settings.rack_app_config = { handler: :puma, host: '0.0.0.0', port: '8080' }
101
101
  ```
102
102
 
103
+ ### use other dbms
104
+
105
+ install dbms(postgresql, mysql, sqlite). And add dbms gem (ex. pg) in your Gemfile.
106
+
107
+ ``` ruby
108
+ gem 'pg'
109
+ ```
110
+
111
+ Overwrite the `databse_client` and `databse_config` with the created ruby ​​file(ex. app.rb) and Rakefile.
112
+
113
+ ``` ruby
114
+ Makanai::Settings.databse_client = :postgres
115
+ Makanai::Settings.databse_config = {
116
+ host: 'localhost',
117
+ password:'password',
118
+ dbname: 'makanai',
119
+ port: 5432
120
+ }
121
+ ```
122
+
103
123
  ## routing
104
124
 
105
125
  ``` ruby
@@ -172,7 +192,7 @@ Create an erb file in `src/views` with the name specified in render.
172
192
 
173
193
  ## migration
174
194
 
175
- makanai uses `sqlite3` as db. create db and migrate schema when executed `rake makanai:db:migration`.
195
+ migrate schema when executed `rake makanai:db:migration`.
176
196
 
177
197
  ```
178
198
  # execute all migraiton sql
@@ -239,6 +259,22 @@ Resource.find(1).delete
239
259
 
240
260
  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.
241
261
 
262
+ ## setup develop enviroment
263
+
264
+ Use docker-compose to build `mysql` and` postgres` containers for development.
265
+
266
+ ``` sh
267
+ $ docker-compose up -d
268
+ Creating makanai_mysql_1 ... done
269
+ Creating makanai_postgres_1 ... done
270
+
271
+ $ docker-compose ps
272
+ Name Command State Ports
273
+ ----------------------------------------------------------------------------------------------
274
+ makanai_mysql_1 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp, 33060/tcp
275
+ makanai_postgres_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
276
+ ```
277
+
242
278
  # License
243
279
 
244
280
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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,27 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sqlite3'
3
+ require_relative './dbms/sqlite.rb'
4
4
  require_relative './settings.rb'
5
5
 
6
6
  module Makanai
7
7
  class Database
8
- def initialize(path: Settings.database_full_path)
9
- @handler = SQLite3::Database
10
- @db = handler.new path
11
- db.tap { |db| db.results_as_hash = true }
8
+ class UnsupportedException < StandardError; end
9
+
10
+ def initialize(client: Settings.databse_client, config: Settings.databse_config)
11
+ client_class = client_class(client)
12
+ @client = client_class.new(config)
12
13
  end
13
14
 
14
- attr_reader :handler, :db
15
+ attr_reader :client
15
16
 
16
17
  def execute_sql(sql)
17
18
  puts "SQL: #{sql.gsub("\n", ' ')}"
18
- db.execute(sql).tap { close_db }
19
+ client.execute_sql(sql)
19
20
  end
20
21
 
21
22
  private
22
23
 
23
- def close_db
24
- db.close
24
+ def client_class(client)
25
+ require_relative File.join('dbms', client.to_s)
26
+ Object.const_get("Makanai::Dbms::#{client.capitalize}")
27
+ rescue LoadError
28
+ raise UnsupportedException
25
29
  end
26
30
  end
27
31
  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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mysql2'
4
+ require_relative './base.rb'
5
+ module Makanai
6
+ module Dbms
7
+ class Mysql < Base
8
+ def initialize(config)
9
+ @db = Mysql2::Client.new(config || default_config)
10
+ end
11
+
12
+ attr_reader :db
13
+
14
+ def execute_sql(sql)
15
+ db.query(sql).to_a.tap { close_db }
16
+ end
17
+
18
+ private
19
+
20
+ def default_config
21
+ {
22
+ host: '0.0.0.0',
23
+ username: 'root',
24
+ password: 'password',
25
+ database: 'makanai',
26
+ port: 3306
27
+ }
28
+ end
29
+
30
+ def close_db
31
+ db.close
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg'
4
+ require_relative './base.rb'
5
+ module Makanai
6
+ module Dbms
7
+ class Postgres < Base
8
+ def initialize(config)
9
+ @db = PG.connect(config || default_config)
10
+ db.type_map_for_results = PG::BasicTypeMapForResults.new(db)
11
+ end
12
+
13
+ attr_reader :db
14
+
15
+ def execute_sql(sql)
16
+ db.exec(sql).each.to_a.tap { close_db }
17
+ end
18
+
19
+ private
20
+
21
+ def default_config
22
+ {
23
+ host: 'localhost',
24
+ password: nil,
25
+ dbname: 'makanai',
26
+ port: 5432
27
+ }
28
+ end
29
+
30
+ def close_db
31
+ db.finish
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sqlite3'
4
+ require_relative './base.rb'
5
+ module Makanai
6
+ module Dbms
7
+ class Sqlite < Base
8
+ def initialize(config)
9
+ @db = SQLite3::Database.new(config[:path])
10
+ db.tap { |db| db.results_as_hash = true }
11
+ end
12
+
13
+ attr_reader :db
14
+
15
+ def execute_sql(sql)
16
+ db.execute(sql).tap { close_db }
17
+ end
18
+
19
+ private
20
+
21
+ def close_db
22
+ db.close
23
+ end
24
+ end
25
+ end
26
+ end
@@ -29,7 +29,7 @@ namespace :makanai do
29
29
  sql_paths = Dir.glob("#{migration_root_path}*")
30
30
  sql_paths.each { |sql_path| execute_sql(sql_path: sql_path) }
31
31
  else
32
- execute_sql(sql_path: "#{migration_root_path}#{target}")
32
+ execute_sql(sql_path: File.join(migration_root_path, target))
33
33
  end
34
34
  puts "INFO: finished migration #{ENV['target']}"
35
35
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rack'
4
+ require_relative './dbms/sqlite.rb'
5
+ require_relative './dbms/postgres.rb'
4
6
 
5
7
  module Makanai
6
8
  class Settings
@@ -16,12 +18,37 @@ module Makanai
16
18
  @migration_root_path = DEFAULT_MIGRATION_ROOT_PATH
17
19
  @rack_app_config = DEFAULT_RACK_APP_CONFIG
18
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
+
19
44
  class << self
20
45
  attr_accessor :app_root_path,
21
46
  :database_path,
22
47
  :template_root_path,
23
48
  :migration_root_path,
24
- :rack_app_config
49
+ :rack_app_config,
50
+ :databse_client,
51
+ :databse_config
25
52
 
26
53
  def database_full_path
27
54
  File.join(app_root_path, database_path)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Makanai
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.3'
5
5
  end
data/makanai.gemspec CHANGED
@@ -31,4 +31,6 @@ Gem::Specification.new do |spec|
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"
34
36
  end
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Madogiwa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-22 00:00:00.000000000 Z
11
+ date: 2020-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -106,6 +106,34 @@ dependencies:
106
106
  - - "~>"
107
107
  - !ruby/object:Gem::Version
108
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'
109
137
  description: simple web application framework for learning.
110
138
  email:
111
139
  - madogiwa0124@gmail.com
@@ -127,10 +155,15 @@ files:
127
155
  - Rakefile
128
156
  - bin/console
129
157
  - bin/setup
158
+ - docker-compose.yml
130
159
  - exe/makanai
131
160
  - lib/makanai.rb
132
161
  - lib/makanai/application.rb
133
162
  - lib/makanai/database.rb
163
+ - lib/makanai/dbms/base.rb
164
+ - lib/makanai/dbms/mysql.rb
165
+ - lib/makanai/dbms/postgres.rb
166
+ - lib/makanai/dbms/sqlite.rb
134
167
  - lib/makanai/dsl.rb
135
168
  - lib/makanai/generator.rb
136
169
  - lib/makanai/generator/application/directories.yaml