railz_lite 0.1.4 → 0.1.9

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: 716364f3c368904ff0a5a37735400997167386db8029d6a7a6c9cb7325f125ea
4
- data.tar.gz: 04c3382909618767749b010ccc05ef3e08b53a87529050783a932b668109bac6
3
+ metadata.gz: 81d26ed037ad3c977f1cb81938ae67fe84e5d61bb003b56fc4a42cfefa66c85e
4
+ data.tar.gz: 83094af33c58caab3053c15a6325360d4d865739a127180cb8b601010e3f20ca
5
5
  SHA512:
6
- metadata.gz: 281fa461490bc827c717b91200665f81654a03b29c7b314e2545cf6dbcc10f3207afb9f1d63dd4d74e679c2b5c4de7138aaec3c15d11bd3a4ea3097f68e331db
7
- data.tar.gz: 0a57ecbba2796a2444fb137c7b8afe392dde2852f88c1d6e5d64f2e12a2d514e631adb09bc0b67fef57fdbec266447fa1a91dd68dbb85bd2b4733c58faa2718d
6
+ metadata.gz: 7fc0c8c888e5c2d906c0d3c7be43d240ad13197ce19681547232308eca1dc6fe06f5805d18a52743923445d9916408caddb95c043b8f1b865ae16bca8657c7ad
7
+ data.tar.gz: 36bef73dd88344e44ad8066f73489fc8bc2e9d709013eff7e16ddb098992d34840fe6230e35f8770e089bbecabc8c35c71e9a301e2b6c60191b46e4987fded59
@@ -1,17 +1,29 @@
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
7
8
  desc 'new', 'Generates a new RailzLite project'
8
- def new
9
- RailzLite::Generators::Project.start([])
9
+ def new(project_name)
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
- puts "hello world"
15
+ file = File.join(Dir.pwd, 'config', 'server.rb')
16
+ if File.exist?(file)
17
+ DBConnection.start # load our db connection
18
+ system('ruby', file)
19
+ else
20
+ raise "File not found at #{file}"
21
+ end
22
+ end
23
+
24
+ desc 'db_reset', 'Restarts the database and reads db/app.sql file'
25
+ def db_reset
26
+ DBConnection.reset
15
27
  end
16
28
  end
17
29
  end
@@ -45,8 +45,8 @@ module RailzLite
45
45
  # use ERB and binding to evaluate templates
46
46
  # pass the rendered html to render_content
47
47
  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")
48
+ dir_path = Dir.pwd
49
+ file_path = File.join(dir_path, 'views', "#{self.class.name.underscore}", "#{template_name.to_s}.html.erb")
50
50
  file = File.read(file_path)
51
51
  template = ERB.new(file).result(binding)
52
52
  render_content(template, 'text/html')
@@ -4,7 +4,7 @@ class Flash
4
4
  attr_reader :now
5
5
  def initialize(req)
6
6
  cookie = req.cookies['_rails_lite_app_flash']
7
- @now = cookie.present? ? JSON.parse(cookie) : {}
7
+ @now = !cookie.nil? ? JSON.parse(cookie) : {}
8
8
  @flash = {}
9
9
  end
10
10
 
@@ -58,11 +58,11 @@ class Router
58
58
  # either throw 404 or call run on a matched route
59
59
  def run(req, res)
60
60
  matching_route = match(req)
61
- if matching_route.present?
62
- matching_route.run(req, res)
63
- else
61
+ if matching_route.nil?
64
62
  res.status = 404
65
63
  res.write("Matching route not found for #{req.path}")
64
+ else
65
+ matching_route.run(req, res)
66
66
  end
67
67
  end
68
68
  end
@@ -5,7 +5,7 @@ class Session
5
5
  # deserialize the cookie into a hash
6
6
  def initialize(req)
7
7
  cookie = req.cookies['_rails_lite_app']
8
- @data = cookie.present? ? JSON.parse(cookie) : {}
8
+ @data = !cookie.nil? ? JSON.parse(cookie) : {}
9
9
  end
10
10
 
11
11
  def [](key)
@@ -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
@@ -4,34 +4,38 @@ module RailzLite
4
4
  module Generators
5
5
  class Project < Thor::Group
6
6
  include Thor::Actions
7
+ argument :project_name, type: :string
7
8
 
8
9
  def self.source_root
9
10
  File.dirname(__FILE__) + "/templates"
10
11
  end
11
12
 
12
13
  def self.destination_root
13
- puts "destination for folder #{Dir.pwd}"
14
14
  Dir.pwd
15
15
  end
16
16
 
17
17
  def add_controllers
18
- empty_directory("controllers")
18
+ empty_directory("#{project_name}/controllers")
19
19
  end
20
20
 
21
21
  def add_models
22
- empty_directory("models")
22
+ empty_directory("#{project_name}/models")
23
23
  end
24
24
 
25
25
  def add_server
26
- template("server.rb", "config/server.rb")
26
+ template('server.rb', "#{project_name}/config/server.rb")
27
27
  end
28
28
 
29
- def add_views
30
- empty_directory("views")
29
+ def add_welcome_view
30
+ template('welcome_view.index.html.erb', "#{project_name}/views/welcome_controller/welcome_view.index.html.erb")
31
31
  end
32
32
 
33
33
  def add_public
34
- empty_directory("public")
34
+ copy_file('winter_fox_large.jpg', "#{project_name}/public")
35
+ end
36
+
37
+ def create_sql_file
38
+ create_file("#{project_name}/db/app.sql")
35
39
  end
36
40
  end
37
41
  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|
@@ -16,7 +23,7 @@ app = Proc.new do |env|
16
23
  end
17
24
 
18
25
  app = Rack::Builder.new do
19
- use Show_Exceptions # generates helpful error messages
26
+ use ShowExceptions # generates helpful error messages
20
27
  use Static # serves static assets from /public
21
28
  run app
22
29
  end.to_app
@@ -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>
@@ -1,12 +1,15 @@
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
+ DBConnection.open(DB_FILE)
11
+ end
12
+
10
13
  def self.open(db_file_name)
11
14
  @db = SQLite3::Database.new(db_file_name)
12
15
  @db.results_as_hash = true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailzLite
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.9"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railz_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - bryan lynch
@@ -141,7 +141,6 @@ email:
141
141
  - bml312@nyu.edu
142
142
  executables:
143
143
  - railz_lite
144
- - railz_lite.rb~
145
144
  extensions: []
146
145
  extra_rdoc_files: []
147
146
  files:
@@ -156,7 +155,6 @@ files:
156
155
  - bin/console
157
156
  - bin/setup
158
157
  - exe/railz_lite
159
- - exe/railz_lite.rb~
160
158
  - lib/railz_lite.rb
161
159
  - lib/railz_lite/cli.rb
162
160
  - lib/railz_lite/controllers/controller_base.rb
@@ -166,20 +164,17 @@ files:
166
164
  - lib/railz_lite/controllers/show_exceptions.rb
167
165
  - lib/railz_lite/controllers/static.rb
168
166
  - lib/railz_lite/controllers/templates/rescue.html.erb
169
- - lib/railz_lite/controllers/templates/rescue.html.erb~
170
167
  - lib/railz_lite/generators/project.rb
171
168
  - lib/railz_lite/generators/project.rb~
172
169
  - lib/railz_lite/generators/templates/application_controller.rb~
173
- - lib/railz_lite/generators/templates/routes.rb
174
170
  - lib/railz_lite/generators/templates/server.rb
171
+ - lib/railz_lite/generators/templates/welcome_view.index.html.erb
172
+ - lib/railz_lite/generators/templates/winter_fox_large.jpg
175
173
  - lib/railz_lite/models/associatable.rb
176
- - lib/railz_lite/models/associatable.rb~
177
174
  - lib/railz_lite/models/associatable2.rb
178
- - lib/railz_lite/models/associatable2.rb~
179
175
  - lib/railz_lite/models/attr_accessor_object.rb
180
176
  - lib/railz_lite/models/db_connection.rb
181
177
  - lib/railz_lite/models/searchable.rb
182
- - lib/railz_lite/models/searchable.rb~
183
178
  - lib/railz_lite/models/sql_object.rb
184
179
  - lib/railz_lite/models/validatable.rb
185
180
  - lib/railz_lite/version.rb
data/exe/railz_lite.rb~ DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- puts "hello world"
File without changes
@@ -1,79 +0,0 @@
1
- require_relative '02_searchable'
2
- require 'active_support/inflector'
3
-
4
- class AssocOptions
5
- attr_accessor(
6
- :foreign_key,
7
- :class_name,
8
- :primary_key
9
- )
10
-
11
- def model_class
12
- class_name.constantize
13
- end
14
-
15
- def table_name
16
- model_class.table_name
17
- end
18
- end
19
-
20
- class BelongsToOptions < AssocOptions
21
- def initialize(name, options = {})
22
- foreign_key_sym = "#{name}_id".to_sym
23
- class_name_val = name.to_s.camelcase.singularize
24
- send("primary_key=", :id)
25
- send("foreign_key=", foreign_key_sym)
26
- send("class_name=", class_name_val)
27
-
28
- options.each do |attr, val|
29
- send("#{attr}=", val)
30
- end
31
- end
32
- end
33
-
34
- class HasManyOptions < AssocOptions
35
- def initialize(name, self_class_name, options = {})
36
- foreign_key_sym = "#{self_class_name.underscore}_id".to_sym
37
- class_name_val = name.to_s.camelcase.singularize
38
-
39
- send("primary_key=", :id)
40
- send("foreign_key=", foreign_key_sym)
41
- send("class_name=", class_name_val)
42
-
43
- options.each do |attr, val|
44
- send("#{attr}=", val)
45
- end
46
- end
47
- end
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
58
- end
59
- end
60
-
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)
68
- end
69
- end
70
-
71
- def assoc_options
72
- @assoc_options ||= {}
73
- @assoc_options
74
- end
75
- end
76
-
77
- class SQLObject
78
- extend Associatable
79
- end
@@ -1,35 +0,0 @@
1
- require_relative '03_associatable'
2
-
3
- module Associatable
4
- def has_one_through(name, through_name, source_name)
5
- define_method(name) do
6
- through_options = self.class.assoc_options[through_name]
7
- source_options =
8
- through_options.model_class.assoc_options[source_name]
9
-
10
- through_table = through_options.table_name
11
- through_pk = through_options.primary_key
12
- through_fk = through_options.foreign_key
13
-
14
- source_table = source_options.table_name
15
- source_pk = source_options.primary_key
16
- source_fk = source_options.foreign_key
17
-
18
- key_val = self.send(through_fk)
19
- results = DBConnection.execute(<<-SQL, key_val)
20
- SELECT
21
- #{source_table}.*
22
- FROM
23
- #{through_table}
24
- JOIN
25
- #{source_table}
26
- ON
27
- #{through_table}.#{source_fk} = #{source_table}.#{source_pk}
28
- WHERE
29
- #{through_table}.#{through_pk} = ?
30
- SQL
31
-
32
- source_options.model_class.parse_all(results).first
33
- end
34
- end
35
- end
@@ -1,21 +0,0 @@
1
- require_relative 'db_connection'
2
- require_relative '01_sql_object'
3
-
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
- SELECT
9
- *
10
- FROM
11
- #{self.table_name}
12
- WHERE
13
- #{where_line}
14
- SQL
15
- results.map { |attrs| self.new(attrs) }
16
- end
17
- end
18
-
19
- class SQLObject
20
- extend Searchable
21
- end