makanai 0.1.5 → 0.1.6

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: 6c81e0de73a48146dc929239f380cba64b004c3517e9efc6f005d2c6a1f29f49
4
- data.tar.gz: be85163a11c4e40cc0f3a43b122ce6396674dccb7fcd4bc14339888c9f45ef39
3
+ metadata.gz: 8c7b32701bd75c6ea7cae7f893310a50e5e70a9fea7c14e9a99ba9559b2e7a39
4
+ data.tar.gz: 8890f0956507a1d0bfa92197718f40b4ed7da313737fe4501032565154f68e08
5
5
  SHA512:
6
- metadata.gz: ebf556b401b54ee54b7921d907e324c03f5926f780c3fe9f39881d6a59a9727cb4d742f8f8f85fe4006626b24bad64b6c262c9725cb7fc31108f0861703c2f44
7
- data.tar.gz: 5174bf19553fd347e15b27469e63224f1552288912b150f739f5b251f63227da3f3a88a408c2e305ecde170d0ee2aa1dc60f5750a765a398c8a32b9f1a769d9a
6
+ metadata.gz: 268af34e32c0cfd48753227a04bf604f6b1fe3fae0a2ef4bb709965c8d6ac73ec3b78a6c6813a2576c33e69e9c00506d60ef1edecd8fd4046bdb013f96081574
7
+ data.tar.gz: d112687c6207e860f5c0513f2530c26c0d388472d3058aae069dc98771c7e637636ef61dfb2300727fa7952c8d03d018d3649b7d086a3b395d98a4c33bb2e8eb
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.7.0
3
+ NewCops: enable
3
4
  Exclude:
4
5
  - './makanai.gemspec'
5
6
  - 'vendor/**/*'
@@ -14,7 +14,11 @@ before_install:
14
14
  - gem install bundler -v 2.1.4
15
15
  - mysql -e 'CREATE DATABASE IF NOT EXISTS makanai;'
16
16
  before_script:
17
+ - psql -c "create user makanai WITH PASSWORD 'password';" -U postgres
17
18
  - psql -c 'create database makanai;' -U postgres
19
+ script:
20
+ - bundle exec rubocop --parallel
21
+ - bundle exec rspec
18
22
  jobs:
19
23
  allow_failures:
20
24
  - rvm: ruby-head
@@ -39,3 +39,10 @@
39
39
 
40
40
  ## :gift: 2020/06/16 `v0.1.5` released.
41
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
@@ -6,4 +6,4 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  gemspec
8
8
 
9
- gem 'codecov', :require => false
9
+ gem 'codecov', require: false
@@ -1,26 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- makanai (0.1.5)
4
+ makanai (0.1.6)
5
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)
13
- codecov (0.1.17)
11
+ ast (2.4.1)
12
+ codecov (0.2.3)
13
+ colorize
14
14
  json
15
15
  simplecov
16
- url
16
+ colorize (0.8.1)
17
17
  diff-lcs (1.3)
18
18
  docile (1.3.2)
19
- json (2.3.0)
19
+ haml (5.1.2)
20
+ temple (>= 0.8.0)
21
+ tilt
22
+ json (2.3.1)
20
23
  mysql2 (0.5.3)
21
- parallel (1.19.1)
22
- parser (2.7.1.3)
23
- ast (~> 2.4.0)
24
+ parallel (1.19.2)
25
+ parser (2.7.1.4)
26
+ ast (~> 2.4.1)
24
27
  pg (1.2.3)
25
28
  rack (2.2.3)
26
29
  rainbow (3.0.0)
@@ -40,25 +43,26 @@ GEM
40
43
  diff-lcs (>= 1.2.0, < 2.0)
41
44
  rspec-support (~> 3.9.0)
42
45
  rspec-support (3.9.2)
43
- rubocop (0.85.1)
46
+ rubocop (0.89.0)
44
47
  parallel (~> 1.10)
45
- parser (>= 2.7.0.1)
48
+ parser (>= 2.7.1.1)
46
49
  rainbow (>= 2.2.2, < 4.0)
47
50
  regexp_parser (>= 1.7)
48
51
  rexml
49
- rubocop-ast (>= 0.0.3)
52
+ rubocop-ast (>= 0.1.0, < 1.0)
50
53
  ruby-progressbar (~> 1.7)
51
54
  unicode-display_width (>= 1.4.0, < 2.0)
52
- rubocop-ast (0.0.3)
53
- parser (>= 2.7.0.1)
55
+ rubocop-ast (0.3.0)
56
+ parser (>= 2.7.1.4)
54
57
  ruby-progressbar (1.10.1)
55
58
  simplecov (0.18.5)
56
59
  docile (~> 1.1)
57
60
  simplecov-html (~> 0.11)
58
61
  simplecov-html (0.12.2)
59
62
  sqlite3 (1.4.2)
63
+ temple (0.8.2)
64
+ tilt (2.0.10)
60
65
  unicode-display_width (1.7.0)
61
- url (0.3.2)
62
66
 
63
67
  PLATFORMS
64
68
  ruby
@@ -66,11 +70,13 @@ PLATFORMS
66
70
  DEPENDENCIES
67
71
  bundler (~> 2.0)
68
72
  codecov
73
+ haml (~> 5.0)
69
74
  makanai!
70
75
  mysql2 (~> 0.5)
71
76
  pg (~> 1.2)
72
77
  rspec (~> 3.0)
73
78
  rubocop (~> 0.74)
79
+ sqlite3 (~> 1.4.1)
74
80
 
75
81
  BUNDLED WITH
76
82
  2.1.4
data/README.md CHANGED
@@ -24,12 +24,6 @@ Or install it yourself as:
24
24
 
25
25
  $ gem install makanai
26
26
 
27
- ## Dependencies
28
-
29
- Makanai depends on the following, so please install it in advance.
30
-
31
- * [SQLite3](https://www.sqlite.org/index.html)
32
-
33
27
  ## Start sample project
34
28
  Getting started with Makanai is easy.
35
29
 
@@ -101,9 +95,11 @@ Overwrite the `handler` in `rack_app_config` with the created ruby ​​file(ex
101
95
  Makanai::Settings.rack_app_config = { handler: :puma, host: '0.0.0.0', port: '8080' }
102
96
  ```
103
97
 
104
- ### use other dbms
98
+ ### use dbms
99
+
100
+ Makanai use `sqlite` by default, and make `db/makanai.db`.
105
101
 
106
- install dbms(postgresql, mysql, sqlite). And add dbms gem (ex. pg) in your Gemfile.
102
+ If you use another dbms, install dbms(PostgreSQL or MySQL). And add dbms gem (`pg` or `mysql2`) in your Gemfile.
107
103
 
108
104
  ``` ruby
109
105
  gem 'pg'
@@ -169,22 +165,33 @@ router.delete '/resources' do |request|
169
165
  end
170
166
  ```
171
167
 
172
- ## erb render
168
+ ## rendering
173
169
 
174
- Define instance variables used in the routing view.
170
+ Define instance variables used in the routing view. And specify template engine(`ERB` or `Haml`).
175
171
 
176
172
  ``` ruby
177
173
  require 'makanai/main'
178
174
 
175
+ # setting template engine(default: :erb).
176
+ Makanai::Settings.template_engine = :haml
177
+
179
178
  router.get '/index' do
180
179
  @title = 'Makanai title'
181
180
  @body = 'Makanai body'
182
- render :index
181
+ render :index # render default template engine.
182
+ end
183
+
184
+ router.get '/index' do
185
+ @title = 'Makanai title'
186
+ @body = 'Makanai body'
187
+ render :index, :haml # render specified template engine.
183
188
  end
184
189
  ```
185
190
 
186
191
  Create an erb file in `src/views` with the name specified in render.
187
192
 
193
+ erb
194
+
188
195
  ``` html
189
196
  <!-- src/views/index.erb -->
190
197
  <html>
@@ -198,6 +205,19 @@ Create an erb file in `src/views` with the name specified in render.
198
205
  </html>
199
206
  ```
200
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
+
201
221
  ## migration
202
222
 
203
223
  migrate schema when executed `rake makanai:db:migration`.
@@ -3,10 +3,10 @@
3
3
  require 'json'
4
4
  require 'uri'
5
5
  require 'rack'
6
- require_relative './settings.rb'
7
- require_relative './request.rb'
8
- require_relative './response.rb'
9
- require_relative './router.rb'
6
+ require_relative './settings'
7
+ require_relative './request'
8
+ require_relative './response'
9
+ require_relative './router'
10
10
 
11
11
  module Makanai
12
12
  class Application
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './dbms/sqlite.rb'
4
- require_relative './settings.rb'
3
+ require_relative './settings'
5
4
 
6
5
  module Makanai
7
6
  class Database
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'mysql2'
4
- require_relative './base.rb'
4
+ require_relative './base'
5
5
  module Makanai
6
6
  module Dbms
7
7
  class Mysql < Base
8
8
  def initialize(config)
9
+ super()
9
10
  @db = Mysql2::Client.new(config || default_config)
10
11
  end
11
12
 
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pg'
4
- require_relative './base.rb'
4
+ require_relative './base'
5
5
  module Makanai
6
6
  module Dbms
7
7
  class Postgres < Base
8
8
  def initialize(config)
9
+ super()
9
10
  @db = PG.connect(config || default_config)
10
11
  db.type_map_for_results = PG::BasicTypeMapForResults.new(db)
11
12
  end
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'sqlite3'
4
- require_relative './base.rb'
4
+ require_relative './base'
5
5
  module Makanai
6
6
  module Dbms
7
7
  class Sqlite < Base
8
8
  def initialize(config)
9
+ super()
9
10
  @db = SQLite3::Database.new(config[:path])
10
11
  db.tap { |db| db.results_as_hash = true }
11
12
  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)
7
+ def render(path, engine = Makanai::Settings.template_engine)
8
8
  template_root_path = Makanai::Settings.template_full_path
9
- full_path = File.join(template_root_path, "#{path}.erb")
10
- Makanai::Template.new(path: full_path).render
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
@@ -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,6 +25,7 @@ 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) }
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rack'
4
- require_relative './dbms/sqlite.rb'
5
- require_relative './dbms/postgres.rb'
6
4
 
7
5
  module Makanai
8
6
  class Settings
@@ -11,10 +9,12 @@ module Makanai
11
9
  DEFAULT_TEMPLATE_ROOT_PATH = '/views/'
12
10
  DEFAULT_MIGRATION_ROOT_PATH = '/migration/'
13
11
  DEFAULT_RACK_APP_CONFIG = { handler: :webrick, host: '0.0.0.0', port: '8080' }.freeze
12
+ DEFAULT_TEMPLATE_ENGINE = :erb
14
13
 
15
14
  @app_root_path = DEFAULT_APP_ROOT_PATH
16
15
  @database_path = DEFAULT_DATABASE_PATH
17
16
  @template_root_path = DEFAULT_TEMPLATE_ROOT_PATH
17
+ @template_engine = DEFAULT_TEMPLATE_ENGINE
18
18
  @migration_root_path = DEFAULT_MIGRATION_ROOT_PATH
19
19
  @rack_app_config = DEFAULT_RACK_APP_CONFIG
20
20
 
@@ -45,6 +45,7 @@ module Makanai
45
45
  attr_accessor :app_root_path,
46
46
  :database_path,
47
47
  :template_root_path,
48
+ :template_engine,
48
49
  :migration_root_path,
49
50
  :rack_app_config,
50
51
  :databse_client,
@@ -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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Makanai
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
@@ -27,10 +27,11 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_dependency "rake", ">= 10", "< 14"
29
29
  spec.add_dependency "rack", ">= 2.0.7", "< 2.3.0"
30
- spec.add_dependency "sqlite3", "~> 1.4.1"
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
34
  spec.add_development_dependency "pg", "~> 1.2"
35
35
  spec.add_development_dependency "mysql2", "~> 0.5"
36
+ spec.add_development_dependency "haml", "~> 5.0"
36
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.5
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: 2020-06-16 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
@@ -57,7 +57,7 @@ dependencies:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: 1.4.1
60
- type: :runtime
60
+ type: :development
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
@@ -134,6 +134,20 @@ dependencies:
134
134
  - - "~>"
135
135
  - !ruby/object:Gem::Version
136
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'
137
151
  description: simple web application framework for learning.
138
152
  email:
139
153
  - madogiwa0124@gmail.com
@@ -169,6 +183,7 @@ files:
169
183
  - lib/makanai/generator.rb
170
184
  - lib/makanai/generator/application/directories.yaml
171
185
  - lib/makanai/generator/application/templates/app.erb
186
+ - lib/makanai/generator/application/templates/gemfile.erb
172
187
  - lib/makanai/generator/application/templates/rakefile.erb
173
188
  - lib/makanai/main.rb
174
189
  - lib/makanai/migration.rb
@@ -179,6 +194,9 @@ files:
179
194
  - lib/makanai/router.rb
180
195
  - lib/makanai/settings.rb
181
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
182
200
  - lib/makanai/version.rb
183
201
  - makanai.gemspec
184
202
  - sample/Rakefile