jetski 0.4.6 → 0.4.8

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: 419176ab6c0bac6515d1f7f6136cd0ff800bad44fa34e41d882ef2bb6c59f46c
4
- data.tar.gz: a349cc717aab7101fea571ad0a12eeadc618a5136103ef388875b4ef2ca806f4
3
+ metadata.gz: 718959913722c396f5ada24d19c6997aee009d7a0aca229156a877def8dba9e4
4
+ data.tar.gz: 8ad9d4179950762af88e4f4df0a02ff4d8510050a5f7417af440b04f758a78dd
5
5
  SHA512:
6
- metadata.gz: eb5520b371047a616aecdd8b341ee17a4b1a7ac4349164639ce8024f7a485b79a51c75035a703ccba0e036c957ad16bdf2f8a12d13eea2e9366f9473c9674bfa
7
- data.tar.gz: 39643ec6fe8aa41363df7d1c3e627cc7aeb3e62a3ed2cbf3ba8d3ff94f19ed149d9d7ba8e5dedea245a2ad91b25fdce4d0a0e5c25dac73d99c86c48d35372fd1
6
+ metadata.gz: c1c685e092ab8da1cf08805874d24f3e2319982f11c1a17ce14e92f0342b8ef5622a24cdd4db89e325c36e0a8c842e547057468b592d9028fdbff1fff84963bf
7
+ data.tar.gz: 847df89c32ce237683bd953da15d589236fe7884707e088e09d0512cc7554a3df09b05a493dd537586e11ac7e094ffc1142e443ea5649d5f1311aea329a669a4
@@ -6,6 +6,16 @@ module Jetski
6
6
  def call
7
7
  model_file_paths.each do |path_to_model|
8
8
  require_relative path_to_model
9
+ # Call method to define model attributes after loading
10
+ model_name = path_to_model.split("app/models/")[-1]
11
+ .gsub(".rb", "")
12
+ .split("/")
13
+ .map(&:capitalize)
14
+ .join("::")
15
+ # posts/comment
16
+ # Post/comment
17
+ model_class = Object.const_get(model_name)
18
+ model_class.define_attribute_methods
9
19
  end
10
20
  end
11
21
 
@@ -3,11 +3,11 @@ module Jetski
3
3
  class BaseController
4
4
  RESERVED_INSTANCE_VARIABLES = [
5
5
  :@res, :@performed_render, :@action_name,
6
- :@controller_name, :@controller_path, :@cookies, :@root,
7
- :@request_method, :@path
6
+ :@controller_name, :@controller_path, :@cookies
8
7
  ]
9
8
 
10
9
  include ReactiveForm
10
+ extend Jetski::Helpers::RouteHelpers
11
11
  attr_accessor :action_name, :controller_name, :controller_path,
12
12
  :params, :cookies
13
13
  attr_reader :res, :performed_render
@@ -52,17 +52,5 @@ module Jetski
52
52
  def get_cookie(name)
53
53
  cookies&.find { |c| c.name == name.to_s }&.value
54
54
  end
55
-
56
- def is_root?
57
- @root == true
58
- end
59
-
60
- def custom_path
61
- @path
62
- end
63
-
64
- def custom_request_method
65
- @request_method
66
- end
67
55
  end
68
56
  end
@@ -18,7 +18,6 @@ module Jetski
18
18
  _gen_sql += "create table #{pluralized_table_name} (\n"
19
19
 
20
20
  # Default fields on all models
21
-
22
21
  _gen_sql += " created_at datetime,\n"
23
22
  _gen_sql += " updated_at datetime,\n"
24
23
  _gen_sql += " id integer,\n"
@@ -0,0 +1,18 @@
1
+ # This is responsible for methods for routing in base_controller like the route class method
2
+
3
+ module Jetski
4
+ module Helpers
5
+ module RouteHelpers
6
+ def route(method_name, root: false, path: nil, request_method: nil)
7
+ # TODO: write code to set route
8
+ @custom_route_opts ||= {}
9
+ @custom_route_opts[method_name] = {
10
+ method_name: method_name,
11
+ root: root,
12
+ path: path,
13
+ request_method: request_method,
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -2,9 +2,50 @@ module Jetski
2
2
  module Helpers
3
3
  module ViewHelpers
4
4
  # Expecting a relative path.
5
- def render(partial_path)
6
- path_to_file = File.join(Jetski.app_root, 'app/views', path_to_controller, "_#{partial_path}.html.erb")
7
- File.read(path_to_file)
5
+ def render(partial_path, **locals)
6
+ path_to_file = File.join(Jetski.app_root, 'app/views',
7
+ path_to_controller, "_#{partial_path}.html.erb")
8
+ content = File.read(path_to_file)
9
+ bind = binding
10
+ locals.each do |k, v|
11
+ bind.local_variable_set k, v
12
+ end
13
+ template = ERB.new(content)
14
+ template.result(bind)
15
+ end
16
+
17
+ # link_to "New post", "/posts/new"
18
+ def link_to(link_text, url, **html_opts)
19
+ # TODO: Allow passing block to link_to
20
+ "<a href='#{url}' #{format_html_options(**html_opts)}>#{link_text}</a>"
21
+ end
22
+
23
+ # button_to("Click me", class: "cool-btn")
24
+ def button_to(button_text, **html_opts)
25
+ "<button #{format_html_options(**html_opts)}>#{button_text}</button>"
26
+ end
27
+
28
+ def input_tag(**html_opts)
29
+ "<input #{format_html_options(**html_opts)}>"
30
+ end
31
+
32
+ def textarea_tag(**html_opts)
33
+ "<textarea #{format_html_options(**html_opts.except(:value))}>#{html_opts[:value]}</textarea>"
34
+ end
35
+
36
+ def image_tag(image_path, **html_opts)
37
+ "<img src='#{image_path}' #{format_html_options(**html_opts)}></img>"
38
+ end
39
+
40
+ def favicon_tag(url, **html_opts)
41
+ "<link rel='icon' type='image/x-icon' #{format_html_options(**html_opts)} href='#{url}'>"
42
+ end
43
+
44
+ def format_html_options(**html_opts)
45
+ html_opts.map do |k, v|
46
+ formatted_key = k.to_s.gsub("_", "-")
47
+ "#{formatted_key}='#{v}'"
48
+ end.join(" ")
8
49
  end
9
50
  end
10
51
  end
data/lib/jetski/model.rb CHANGED
@@ -3,33 +3,18 @@ module Jetski
3
3
  extend Jetski::Database::Base
4
4
 
5
5
  def initialize(**args)
6
- @virtual_attributes = {}
7
-
8
- args.each do |k, v|
9
- @virtual_attributes[k] = v
10
- end
11
-
12
- @virtual_attributes["id"] ||= ""
13
- @virtual_attributes["created_at"] ||= ""
14
- @virtual_attributes["updated_at"] ||= ""
15
-
16
- @virtual_attributes.each do |k, v|
17
- self.class.class_eval do
18
- define_method(k) { v }
19
- end
20
- end
6
+ @virtual_attributes = args
7
+ end
21
8
 
22
- self.class.class_eval do
23
- define_method(:inspect) do
24
- post_obj_id = object_id
25
- inspect_str = "#<Post:#{post_obj_id}"
26
- @virtual_attributes.each do |k, v|
27
- inspect_str += " #{k}=\"#{v}\""
28
- end
29
- inspect_str += ">"
30
- inspect_str
31
- end
9
+ def inspect
10
+ post_obj_id = object_id
11
+ inspect_str = "#<#{self.class.to_s}:#{post_obj_id}"
12
+ self.class.attributes.each do |attribute_name|
13
+ attribute_value = @virtual_attributes[attribute_name]
14
+ inspect_str += " #{attribute_name}=\"#{attribute_value}\""
32
15
  end
16
+ inspect_str += ">"
17
+ inspect_str
33
18
  end
34
19
 
35
20
  def destroy!
@@ -52,8 +37,10 @@ module Jetski
52
37
  key_names.append "created_at"
53
38
  data_values.append Time.now.to_s
54
39
 
40
+ current_post_count = (count || 0)
41
+ post_id = current_post_count + 1
55
42
  key_names.append "id"
56
- data_values.append(count + 1)
43
+ data_values.append(post_id)
57
44
 
58
45
  sql_command = <<~SQL
59
46
  INSERT INTO #{pluralized_table_name} (#{key_names.join(", ")})
@@ -70,6 +57,19 @@ module Jetski
70
57
  new(**post_attributes)
71
58
  end
72
59
 
60
+ # Careful methods / delete all records from table
61
+ def destroy_all!
62
+ db.execute("DELETE from #{pluralized_table_name}")
63
+ end
64
+
65
+ def define_attribute_methods
66
+ attributes.each do |attribute|
67
+ define_method attribute do
68
+ @virtual_attributes[attribute]
69
+ end
70
+ end
71
+ end
72
+
73
73
  def all
74
74
  columns, *rows = db.execute2( "select * from #{pluralized_table_name}" )
75
75
  _all = []
@@ -88,6 +88,7 @@ module Jetski
88
88
  end
89
89
 
90
90
  def attributes
91
+ # TODO: Find a more performant way to get the column names from a table
91
92
  columns, *rows = db.execute2( "select * from #{pluralized_table_name}" )
92
93
  columns
93
94
  end
@@ -111,7 +112,6 @@ module Jetski
111
112
  table_name + "s"
112
113
  end
113
114
  end
114
-
115
115
  private
116
116
  def format_model_obj(row, columns = nil)
117
117
  return unless row
@@ -24,18 +24,34 @@ module Jetski
24
24
  controller_name: controller_name,
25
25
  controller_path: controller_path,
26
26
  }
27
+ route_opts_hash = controller_class.instance_variable_get(:@custom_route_opts)
27
28
  action_names.each do |action_name|
28
29
  action_name = action_name.to_s
30
+ route_opts = route_opts_hash.fetch(action_name.to_sym, {})
31
+ custom_request_method = route_opts.fetch(:request_method, nil)
32
+ is_root = route_opts.fetch(:root, nil)
33
+ custom_path = route_opts.fetch(:path, nil)
34
+
35
+ url_to_use = proc do |url|
36
+ if is_root
37
+ "/"
38
+ elsif custom_path
39
+ custom_path
40
+ else
41
+ url
42
+ end
43
+ end
44
+
29
45
  case action_name
30
46
  when "new"
31
47
  auto_found_routes << base_opts.merge({
32
- url: controller_path + "/new",
48
+ url: url_to_use.call(controller_path + "/new"),
33
49
  method: "GET",
34
50
  action_name: action_name,
35
51
  })
36
52
  when "create"
37
53
  auto_found_routes << base_opts.merge({
38
- url: controller_path,
54
+ url: url_to_use.call(controller_path),
39
55
  method: "POST",
40
56
  action_name: action_name,
41
57
  })
@@ -53,21 +69,23 @@ module Jetski
53
69
  })
54
70
  when "update"
55
71
  auto_found_routes << base_opts.merge({
56
- url: controller_path + "/:id",
72
+ url: url_to_use.call(controller_path + "/:id"),
57
73
  method: "PUT",
58
74
  action_name: action_name,
59
75
  })
60
76
  when "destroy"
61
77
  auto_found_routes << base_opts.merge({
62
- url: controller_path + "/:id",
78
+ url: url_to_use.call(controller_path + "/:id"),
63
79
  method: "DELETE",
64
80
  action_name: action_name,
65
81
  })
82
+ when "index"
83
+ auto_found_routes << base_opts.merge({
84
+ url: url_to_use.call(controller_path),
85
+ method: "GET",
86
+ action_name: action_name,
87
+ })
66
88
  else
67
- tmp_controller_instance = controller_class.new("")
68
- tmp_controller_instance.send(action_name)
69
- is_root = tmp_controller_instance.is_root?
70
- custom_path = tmp_controller_instance.custom_path
71
89
  url_to_use = if is_root
72
90
  "/"
73
91
  elsif custom_path
@@ -76,7 +94,6 @@ module Jetski
76
94
  url_friendly_action_name = action_name.split("_").join("-")
77
95
  controller_path + "/#{url_friendly_action_name}"
78
96
  end
79
- custom_request_method = tmp_controller_instance.custom_request_method
80
97
 
81
98
  auto_found_routes << base_opts.merge({
82
99
  url: url_to_use,
@@ -1,3 +1,3 @@
1
1
  module Jetski
2
- VERSION = "0.4.6"
2
+ VERSION = "0.4.8"
3
3
  end
@@ -33,7 +33,7 @@ module Jetski
33
33
  template = ERB.new(content)
34
34
  # Perserve instance variables to view render
35
35
  # @posts from controller to posts/index.html.erb
36
- # controller.class.class_eval problem is you still have render defined there
36
+
37
37
  grab_instance_variables
38
38
  template.result(binding)
39
39
  rescue => e
data/lib/jetski.rb CHANGED
@@ -6,8 +6,10 @@ require "rack"
6
6
  require "sqlite3"
7
7
 
8
8
  require_relative './jetski/version'
9
- require_relative './jetski/frontend/reactive_form'
10
9
  require_relative './jetski/helpers/view_helpers'
10
+ require_relative './jetski/helpers/route_helpers'
11
+ require_relative './jetski/helpers/delegatable'
12
+ require_relative './jetski/frontend/reactive_form'
11
13
  require_relative './jetski/base_controller'
12
14
  require_relative './jetski/router/shared_methods'
13
15
  require_relative './jetski/router/parser'
@@ -16,7 +18,6 @@ require_relative './jetski/autoloader'
16
18
  require_relative './jetski/server'
17
19
  require_relative './jetski/database/base'
18
20
  require_relative './jetski/model'
19
- require_relative './jetski/helpers/delegatable'
20
21
  require_relative './jetski/view_renderer'
21
22
 
22
23
  module Jetski
@@ -1,5 +1,5 @@
1
1
  class PagesController < Jetski::BaseController
2
+ route :home, root: true
2
3
  def home
3
- @root = true
4
4
  end
5
5
  end
@@ -4,7 +4,7 @@
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
6
  <title> APP_NAME </title>
7
- <link rel="icon" type="image/x-icon" href="/jetski-logo.png">
7
+ <%= favicon_tag "jetski-logo.png" %>
8
8
  </head>
9
9
  <body>
10
10
  <%= yield %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jetski
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo Tech Tutorials
@@ -127,6 +127,7 @@ files:
127
127
  - lib/jetski/frontend/reactive_form.js
128
128
  - lib/jetski/frontend/reactive_form.rb
129
129
  - lib/jetski/helpers/delegatable.rb
130
+ - lib/jetski/helpers/route_helpers.rb
130
131
  - lib/jetski/helpers/view_helpers.rb
131
132
  - lib/jetski/model.rb
132
133
  - lib/jetski/router.rb