railz_lite 0.1.7 → 0.2.2
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 +4 -4
- data/lib/railz_lite/cli.rb +7 -1
- data/lib/railz_lite/controllers/controller_base.rb +21 -5
- data/lib/railz_lite/controllers/static.rb +2 -2
- data/lib/railz_lite/generators/project.rb +9 -4
- data/lib/railz_lite/generators/templates/application.html.erb +10 -0
- data/lib/railz_lite/generators/templates/server.rb +7 -0
- data/lib/railz_lite/generators/templates/welcome_view.index.html.erb +12 -0
- data/lib/railz_lite/generators/templates/winter_fox_large.jpg +0 -0
- data/lib/railz_lite/models/associatable.rb +23 -25
- data/lib/railz_lite/models/db_connection.rb +11 -7
- data/lib/railz_lite/models/searchable.rb +7 -10
- data/lib/railz_lite/models/sql_object.rb +5 -0
- data/lib/railz_lite/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c174580952b1c2956cf1d6bdbe7fbc0c7991a8b4401c98d63d3d3a9718ab4319
|
4
|
+
data.tar.gz: 3e32d2b2cc43e7ed406095e6934798e3c5fbafbe55f4965e3f08b732932d92ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dca26645dca90b0a88e43ff6790083c7df11d1cff5b1b880d832156d1675e71f50e5310d2e01815b83b6f93c75f38a9c36b0b3e4ea21225e855d40b677647bfb
|
7
|
+
data.tar.gz: 6b682f976740cc0b9127bd7673fd520c7cbff38392aa54c3c7079bc5f1dc14274f452cefa3f6fdaf4388e317f73b9b97cc8b8f773c9e92499ba8e03ff0f5b93c
|
data/lib/railz_lite/cli.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require 'railz_lite'
|
3
3
|
require 'railz_lite/generators/project'
|
4
|
+
require 'railz_lite/models/db_connection'
|
4
5
|
|
5
6
|
module RailzLite
|
6
7
|
class CLI < Thor
|
@@ -9,7 +10,7 @@ module RailzLite
|
|
9
10
|
RailzLite::Generators::Project.start([project_name])
|
10
11
|
end
|
11
12
|
|
12
|
-
desc 'server', 'Starts up a puma server within RailzLite project'
|
13
|
+
desc 'server', 'Starts up a puma server within RailzLite project. Also initializes '
|
13
14
|
def server
|
14
15
|
file = File.join(Dir.pwd, 'config', 'server.rb')
|
15
16
|
if File.exist?(file)
|
@@ -18,5 +19,10 @@ module RailzLite
|
|
18
19
|
raise "File not found at #{file}"
|
19
20
|
end
|
20
21
|
end
|
22
|
+
|
23
|
+
desc 'db_reset', 'Restarts the database and reads db/app.sql file'
|
24
|
+
def db_reset
|
25
|
+
DBConnection.reset
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
@@ -5,6 +5,9 @@ require_relative './session'
|
|
5
5
|
require_relative './flash'
|
6
6
|
|
7
7
|
module RailzLite
|
8
|
+
class LayoutRenderer # dummy class used to pass yield blocks into ERB method results https://hostiledeveloper.com/2015/05/28/working-with-templates-in-ruby-erb.html
|
9
|
+
end
|
10
|
+
|
8
11
|
class ControllerBase
|
9
12
|
attr_reader :req, :res, :params
|
10
13
|
|
@@ -45,11 +48,24 @@ module RailzLite
|
|
45
48
|
# use ERB and binding to evaluate templates
|
46
49
|
# pass the rendered html to render_content
|
47
50
|
def render(template_name)
|
48
|
-
dir_path =
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
dir_path = Dir.pwd
|
52
|
+
|
53
|
+
layout_path = File.join(dir_path, 'views', 'application', 'application.html.erb')
|
54
|
+
inner_file_path = File.join(dir_path, 'views', "#{self.class.name.underscore.split('_controller').first}", "#{template_name.to_s}.html.erb")
|
55
|
+
|
56
|
+
layout_template = File.read(layout_path)
|
57
|
+
inner_template = File.read(inner_file_path)
|
58
|
+
|
59
|
+
layout = ERB.new(layout_template)
|
60
|
+
inner = ERB.new(inner_template)
|
61
|
+
|
62
|
+
layout.def_method(LayoutRenderer, 'render') # dummy method used so that blocks can be passed to ERB result
|
63
|
+
|
64
|
+
result = LayoutRenderer.new.render do
|
65
|
+
inner.result(binding)
|
66
|
+
end
|
67
|
+
|
68
|
+
render_content(result, 'text/html')
|
53
69
|
end
|
54
70
|
|
55
71
|
# method exposing a `Session` object
|
@@ -11,7 +11,6 @@ module RailzLite
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.destination_root
|
14
|
-
puts "destination for folder #{Dir.pwd}"
|
15
14
|
Dir.pwd
|
16
15
|
end
|
17
16
|
|
@@ -27,12 +26,18 @@ module RailzLite
|
|
27
26
|
template('server.rb', "#{project_name}/config/server.rb")
|
28
27
|
end
|
29
28
|
|
30
|
-
def
|
31
|
-
|
29
|
+
def setup_views
|
30
|
+
template('welcome_view.index.html.erb', "#{project_name}/views/welcome/index.html.erb")
|
31
|
+
template('application.html.erb', "#{project_name}/views/application/application.html.erb")
|
32
|
+
create_file("#{project_name}/views/application/application.css")
|
32
33
|
end
|
33
34
|
|
34
35
|
def add_public
|
35
|
-
|
36
|
+
copy_file('winter_fox_large.jpg', "#{project_name}/public/winter_fox_large.jpg")
|
37
|
+
end
|
38
|
+
|
39
|
+
def create_sql_file
|
40
|
+
create_file("#{project_name}/db/app.sql")
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -2,10 +2,17 @@ require 'rack'
|
|
2
2
|
require 'railz_lite/controllers/static'
|
3
3
|
require 'railz_lite/controllers/show_exceptions'
|
4
4
|
require 'railz_lite/controllers/router'
|
5
|
+
require 'railz_lite'
|
6
|
+
|
7
|
+
# example controller config
|
8
|
+
class WelcomeController < RailzLite::ControllerBase
|
9
|
+
def index; end
|
10
|
+
end
|
5
11
|
|
6
12
|
router = Router.new
|
7
13
|
router.draw do
|
8
14
|
# add routes here
|
15
|
+
get Regexp.new('^/$'), WelcomeController, :index
|
9
16
|
end
|
10
17
|
|
11
18
|
app = Proc.new do |env|
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<div style="
|
2
|
+
width: 50%;
|
3
|
+
margin: 0 auto;
|
4
|
+
margin-top: 18px;
|
5
|
+
text-align: center;
|
6
|
+
">
|
7
|
+
<h1>Welcome To Railz Lite!</h1>
|
8
|
+
|
9
|
+
<img src="../public/winter_fox_large.jpg" />
|
10
|
+
|
11
|
+
<h3>For guidelines on how to build an app with this framework see <a href="https://github.com/bryanqb07/railz_lite">here</a>.</h3>
|
12
|
+
</div>
|
Binary file
|
@@ -46,34 +46,32 @@ class HasManyOptions < AssocOptions
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
module
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
49
|
+
module RailzLite
|
50
|
+
module Associatable
|
51
|
+
def belongs_to(name, options = {})
|
52
|
+
options = BelongsToOptions.new(name, options)
|
53
|
+
assoc_options[name] = options
|
54
|
+
define_method(name) do
|
55
|
+
foreign_key = send(options.foreign_key)
|
56
|
+
primary_key = options.primary_key
|
57
|
+
params = [[primary_key, foreign_key]].to_h
|
58
|
+
options.model_class.where(params).first
|
59
|
+
end
|
58
60
|
end
|
59
|
-
end
|
60
61
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
62
|
+
def has_many(name, options = {})
|
63
|
+
options = HasManyOptions.new(name, self.name, options)
|
64
|
+
define_method(name) do
|
65
|
+
foreign_key = options.foreign_key
|
66
|
+
primary_key = send(options.primary_key)
|
67
|
+
params = [[foreign_key, primary_key]].to_h
|
68
|
+
options.model_class.where(params)
|
69
|
+
end
|
68
70
|
end
|
69
|
-
end
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
def assoc_options
|
73
|
+
@assoc_options ||= {}
|
74
|
+
@assoc_options
|
75
|
+
end
|
74
76
|
end
|
75
77
|
end
|
76
|
-
|
77
|
-
class SQLObject
|
78
|
-
extend Associatable
|
79
|
-
end
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'sqlite3'
|
2
2
|
|
3
|
-
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
4
|
-
# https://tomafro.net/2010/01/tip-relative-paths-with-file-expand-path
|
5
|
-
ROOT_FOLDER = File.join(File.dirname(__FILE__), '..')
|
6
|
-
SQL_FILE = File.join(ROOT_FOLDER, 'app.sql')
|
7
|
-
DB_FILE = File.join(ROOT_FOLDER, 'app.db')
|
8
|
-
|
9
3
|
class DBConnection
|
4
|
+
PRINT_QUERIES = ENV['PRINT_QUERIES'] == 'true'
|
5
|
+
ROOT_FOLDER = Dir.pwd
|
6
|
+
SQL_FILE = File.join(ROOT_FOLDER, 'db', 'app.sql')
|
7
|
+
DB_FILE = File.join(ROOT_FOLDER, 'db', 'app.db')
|
8
|
+
|
9
|
+
def self.start
|
10
|
+
`sqlite3 #{DB_FILE}`
|
11
|
+
DBConnection.open(DB_FILE)
|
12
|
+
end
|
13
|
+
|
10
14
|
def self.open(db_file_name)
|
11
15
|
@db = SQLite3::Database.new(db_file_name)
|
12
16
|
@db.results_as_hash = true
|
@@ -26,7 +30,7 @@ class DBConnection
|
|
26
30
|
end
|
27
31
|
|
28
32
|
def self.instance
|
29
|
-
|
33
|
+
start if @db.nil?
|
30
34
|
|
31
35
|
@db
|
32
36
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require_relative 'db_connection'
|
2
|
-
require_relative 'sql_object'
|
3
2
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module RailzLite
|
4
|
+
module Searchable
|
5
|
+
def where(params)
|
6
|
+
where_line = params.keys.map { |attr_name| "#{attr_name} = ?" }.join(" AND ")
|
7
|
+
results = DBConnection.execute(<<-SQL, *params.values)
|
8
8
|
SELECT
|
9
9
|
*
|
10
10
|
FROM
|
@@ -12,10 +12,7 @@ module Searchable
|
|
12
12
|
WHERE
|
13
13
|
#{where_line}
|
14
14
|
SQL
|
15
|
-
|
15
|
+
results.map { |attrs| self.new(attrs) }
|
16
|
+
end
|
16
17
|
end
|
17
18
|
end
|
18
|
-
|
19
|
-
class SQLObject
|
20
|
-
extend Searchable
|
21
|
-
end
|
@@ -1,8 +1,13 @@
|
|
1
1
|
require_relative 'db_connection'
|
2
|
+
require_relative 'associatable'
|
3
|
+
require_relative 'searchable'
|
2
4
|
require 'active_support/inflector'
|
3
5
|
|
4
6
|
module RailzLite
|
5
7
|
class SQLObject
|
8
|
+
extend RailzLite::Associatable
|
9
|
+
extend RailzLite::Searchable
|
10
|
+
|
6
11
|
def self.columns
|
7
12
|
@columns ||= DBConnection.execute2(<<-SQL)
|
8
13
|
SELECT
|
data/lib/railz_lite/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: railz_lite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- bryan lynch
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -166,8 +166,11 @@ files:
|
|
166
166
|
- lib/railz_lite/controllers/templates/rescue.html.erb
|
167
167
|
- lib/railz_lite/generators/project.rb
|
168
168
|
- lib/railz_lite/generators/project.rb~
|
169
|
+
- lib/railz_lite/generators/templates/application.html.erb
|
169
170
|
- lib/railz_lite/generators/templates/application_controller.rb~
|
170
171
|
- lib/railz_lite/generators/templates/server.rb
|
172
|
+
- lib/railz_lite/generators/templates/welcome_view.index.html.erb
|
173
|
+
- lib/railz_lite/generators/templates/winter_fox_large.jpg
|
171
174
|
- lib/railz_lite/models/associatable.rb
|
172
175
|
- lib/railz_lite/models/associatable2.rb
|
173
176
|
- lib/railz_lite/models/attr_accessor_object.rb
|