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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2ebf1f78b9263164fa6d9c864a3344f834171df91856e8f5cfb18b318b7c215
4
- data.tar.gz: 8844959b357a2ad711666cff1eadfdfd5a963621d09f1d035442162eb4ccd0f1
3
+ metadata.gz: c174580952b1c2956cf1d6bdbe7fbc0c7991a8b4401c98d63d3d3a9718ab4319
4
+ data.tar.gz: 3e32d2b2cc43e7ed406095e6934798e3c5fbafbe55f4965e3f08b732932d92ec
5
5
  SHA512:
6
- metadata.gz: 3e2f0f87d0a1cbb225d5987518e3f025446b04c075e28c6faf749f616cc3ac0393f12c09b963e08aaa33ac93fd29b8c9c6cbae43aebd0baa766615b62b4b5c50
7
- data.tar.gz: 144b760958c29eaf21ba4c4977af41e47fe068c20eb651798c4fa751e5a2c791e2afd81e24d430a05a506482078f480b0931a9efb53f23846f674ec32d7ea2cc
6
+ metadata.gz: dca26645dca90b0a88e43ff6790083c7df11d1cff5b1b880d832156d1675e71f50e5310d2e01815b83b6f93c75f38a9c36b0b3e4ea21225e855d40b677647bfb
7
+ data.tar.gz: 6b682f976740cc0b9127bd7673fd520c7cbff38392aa54c3c7079bc5f1dc14274f452cefa3f6fdaf4388e317f73b9b97cc8b8f773c9e92499ba8e03ff0f5b93c
@@ -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 = File.dirname(__FILE__)
49
- file_path = File.join(dir_path, '..', 'views', "#{self.class.name.underscore}", "#{template_name.to_s}.html.erb")
50
- file = File.read(file_path)
51
- template = ERB.new(file).result(binding)
52
- render_content(template, 'text/html')
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
@@ -61,7 +61,7 @@ class FileServer
61
61
  def requested_file_name(env)
62
62
  req = Rack::Request.new(env)
63
63
  path = req.path
64
- dir = File.dirname(__FILE__)
65
- File.join(dir, '..', path)
64
+ dir = Dir.pwd
65
+ File.join(dir, path)
66
66
  end
67
67
  end
@@ -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 add_views
31
- empty_directory("#{project_name}/views")
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
- empty_directory("#{project_name}/public")
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
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>App Title</title>
5
+ <link rel="stylesheet" href="application.css">
6
+ </head>
7
+ <body>
8
+ <%= yield %>
9
+ </body>
10
+ </html>
@@ -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>
@@ -46,34 +46,32 @@ class HasManyOptions < AssocOptions
46
46
  end
47
47
  end
48
48
 
49
- module Associatable
50
- def belongs_to(name, options = {})
51
- options = BelongsToOptions.new(name, options)
52
- assoc_options[name] = options
53
- define_method(name) do
54
- foreign_key = send(options.foreign_key)
55
- primary_key = options.primary_key
56
- params = [[primary_key, foreign_key]].to_h
57
- options.model_class.where(params).first
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
- def has_many(name, options = {})
62
- options = HasManyOptions.new(name, self.name, options)
63
- define_method(name) do
64
- foreign_key = options.foreign_key
65
- primary_key = send(options.primary_key)
66
- params = [[foreign_key, primary_key]].to_h
67
- options.model_class.where(params)
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
- def assoc_options
72
- @assoc_options ||= {}
73
- @assoc_options
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
- reset if @db.nil?
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 Searchable
5
- def where(params)
6
- where_line = params.keys.map { |attr_name| "#{attr_name} = ?" }.join(" AND ")
7
- results = DBConnection.execute(<<-SQL, *params.values)
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
- results.map { |attrs| self.new(attrs) }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailzLite
4
- VERSION = "0.1.7"
4
+ VERSION = "0.2.2"
5
5
  end
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.1.7
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-02 00:00:00.000000000 Z
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