gazebo 0.1.2 → 0.1.3

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
  SHA1:
3
- metadata.gz: a7e1c03436e96b489059927089d42de8a98f07ff
4
- data.tar.gz: f01dc87cbb489db33f283a2467a5f884992ad235
3
+ metadata.gz: 2343322e65130a3a649ce4a71bbd9fe4471f15f0
4
+ data.tar.gz: 23efbd0419d865922d69e21be572141089975769
5
5
  SHA512:
6
- metadata.gz: 2ac34da08d89d7f35e323af6eda2800c72187d66d8a0207bafe65d73591b771b57549031dd5f6720c2f831cede9fc990ae8442b824d8031082cb6c6f8c373d8c
7
- data.tar.gz: 61aff4beade614102cef92417327088d6f7abe458c30caca8e363f539b72851c344e6d9f7beb5de5d84f06cc7210dee1ab053cb2809696183efa6768a6002cae
6
+ metadata.gz: 6d5e94fcbda1de26d97edaf888995f56f5ed0bb1087166aaf5a0469d3b2ab517fe3fe7b5cdede22b3a3ac5ace3e60f7e05c67ac17f711b8dcebcf8bd7b1b8eb7
7
+ data.tar.gz: 8d98941122b0b4134ae0d7a5b60953ea50362eef60f6b24a17193c2df5d6c57171c81f64c0813d7125e929a117214178005563546ee07623ee4aed0f4c0ef917
@@ -6,7 +6,6 @@ class ControllerBase
6
6
  @@protect_from_forgery = true
7
7
  end
8
8
 
9
- # Setup the controller
10
9
  def initialize(req, res, route_params = {})
11
10
  @req = req
12
11
  @res = res
@@ -18,12 +17,10 @@ class ControllerBase
18
17
  @flash ||= Flash.new(req)
19
18
  end
20
19
 
21
- # Helper method to alias @already_built_response
22
20
  def already_built_response?
23
21
  @already_built_response || false
24
22
  end
25
23
 
26
- # Set the response status code and header
27
24
  def redirect_to(url)
28
25
  check_for_repeat_action!
29
26
  res.status = 302
@@ -34,9 +31,6 @@ class ControllerBase
34
31
  @already_built_response = true
35
32
  end
36
33
 
37
- # Populate the response with content.
38
- # Set the response's content type to the given type.
39
- # Raise an error if the developer tries to double render.
40
34
  def render_content(content, content_type)
41
35
  check_for_repeat_action!
42
36
  res['Content-Type'] = content_type
@@ -47,13 +41,11 @@ class ControllerBase
47
41
  @already_built_response = true
48
42
  end
49
43
 
50
- #raise error if already_built_response
44
+ #change to custom doubleRender error
51
45
  def check_for_repeat_action!
52
46
  raise "Cannot call render/redirect twice in one action" if already_built_response?
53
47
  end
54
48
 
55
- # use ERB and binding to evaluate templates
56
- # pass the rendered html to render_content
57
49
  def render(template_name)
58
50
  controller_name = self.class.to_s.underscore[0..-("_controller".length + 1)]
59
51
  file_path = "app/views/#{controller_name}/#{template_name}.html.erb"
@@ -66,12 +58,10 @@ class ControllerBase
66
58
  render_content(content, 'text/html')
67
59
  end
68
60
 
69
- # method exposing a `Session` object
70
61
  def session
71
62
  @session ||= Session.new(req)
72
63
  end
73
64
 
74
- # use this with the router to call action_name (:index, :show, :create...)
75
65
  def invoke_action(name)
76
66
  if protect_from_forgery? && req.request_method != 'GET'
77
67
  check_authenticity_token
@@ -19,8 +19,6 @@ class Flash
19
19
  @flash[key.to_s] = val
20
20
  end
21
21
 
22
- # serialize the hash into json and save in a cookie
23
- # add to the responses cookies
24
22
  def store_flash(res)
25
23
  res.set_cookie(
26
24
  '_gazebo_app_flash',
@@ -1,6 +1,4 @@
1
1
  class Session
2
- # find the cookie for this app
3
- # deserialize the cookie into a hash
4
2
  attr_reader :cookie
5
3
 
6
4
  def initialize(req)
@@ -17,8 +15,6 @@ class Session
17
15
  @cookie[key.to_s] = val
18
16
  end
19
17
 
20
- # serialize the hash into json and save in a cookie
21
- # add to the responses cookies
22
18
  def store_session(res)
23
19
  res.set_cookie(
24
20
  '_gazebo_app',
@@ -2,11 +2,55 @@ require 'pg'
2
2
  PRINT_QUERIES = true
3
3
 
4
4
  class DBConnection
5
+ def self.instance
6
+ open if @db.nil?
7
+
8
+ @db
9
+ end
10
+
11
+ def self.execute(*args)
12
+ print_query(*args)
13
+ instance.exec(*args)
14
+ end
15
+
16
+ def self.async_exec(*args)
17
+ print_query(*args)
18
+ instance.send_query(*args)
19
+ end
20
+
21
+ def self.get_first_row(*args)
22
+ print_query(*args)
23
+ instance.exec(*args).first
24
+ end
25
+
26
+ def self.run_migrations
27
+ ensure_migrations_table!
28
+ migrations = Dir.entries("db/migrations").reject { |fname| fname.start_with?('.') }
29
+ migrations.sort_by! { |fname| Integer(fname[0..1]) }
30
+
31
+ migrations.each do |file_name|
32
+ migration_name = file_name.match(/\w+/).to_s
33
+
34
+ next if migration_name.empty? || already_run?(migration_name)
35
+
36
+ file = File.join(Gazebo::ROOT, "db/migrations", file_name)
37
+ migration_sql = File.read(file)
38
+ execute(migration_sql)
39
+
40
+ record_migration!(migration_name)
41
+ end
42
+ end
43
+
44
+ private
45
+ def self.database_name
46
+ Gazebo::ROOT.split('/').last.gsub("-", "_") + '_development'
47
+ end
48
+
5
49
  def self.open
6
50
  if ENV['DATABASE_URL']
7
- self.open_production
51
+ open_production
8
52
  else
9
- self.open_development
53
+ open_development
10
54
  end
11
55
  run_migrations
12
56
  end
@@ -48,25 +92,6 @@ class DBConnection
48
92
  end
49
93
  end
50
94
 
51
- def self.run_migrations
52
- ensure_migrations_table!
53
- migrations = Dir.entries("db/migrations").reject { |fname| fname.start_with?('.') }
54
- migrations.sort_by! { |fname| Integer(fname[0..1]) }
55
-
56
- migrations.each do |file_name|
57
- migration_name = file_name.match(/\w+/).to_s
58
-
59
- next if migration_name.empty? || already_run?(migration_name)
60
-
61
- file = File.join(Gazebo::ROOT, "db/migrations", file_name)
62
- migration_sql = File.read(file)
63
-
64
- execute(migration_sql)
65
-
66
- record_migration!(migration_name)
67
- end
68
- end
69
-
70
95
  def self.record_migration!(migration_name)
71
96
  time = Time.new.strftime("%Y%m%dT%H%M")
72
97
  here_doc = <<-SQL
@@ -81,9 +106,9 @@ class DBConnection
81
106
 
82
107
  def self.already_run?(migration_name)
83
108
  !!execute(<<-SQL, [migration_name]).first
84
- SELECT *
85
- FROM migrations
86
- WHERE name = $1
109
+ SELECT *
110
+ FROM migrations
111
+ WHERE name = $1
87
112
  SQL
88
113
  end
89
114
 
@@ -92,33 +117,6 @@ class DBConnection
92
117
  master_conn.exec("CREATE DATABASE #{database_name}")
93
118
  end
94
119
 
95
- def self.database_name
96
- Gazebo::ROOT.split('/').last.gsub("-", "_") + '_development'
97
- end
98
-
99
- def self.instance
100
- open if @db.nil?
101
-
102
- @db
103
- end
104
-
105
- def self.execute(*args)
106
- print_query(*args)
107
- instance.exec(*args)
108
- end
109
-
110
- def self.async_exec(*args)
111
- print_query(*args)
112
- instance.send_query(*args)
113
- end
114
-
115
- def self.get_first_row(*args)
116
- print_query(*args)
117
- instance.exec(*args).first
118
- end
119
-
120
- private
121
-
122
120
  def self.random_color
123
121
  [:blue, :light_blue, :red, :green, :yellow].sample
124
122
  end
data/lib/cli.rb CHANGED
@@ -12,5 +12,4 @@ module Gazebo
12
12
  Gazebo.seed
13
13
  end
14
14
  end
15
-
16
15
  end
data/lib/gazebo.rb CHANGED
@@ -12,7 +12,7 @@ require_relative 'cli'
12
12
 
13
13
  module Gazebo
14
14
  Router = Router.new
15
- VERSION = "0.1.2"
15
+ VERSION = "0.1.3"
16
16
 
17
17
  def self.app
18
18
  fetch_routes
data/lib/router.rb CHANGED
@@ -8,18 +8,14 @@ class Route
8
8
  @action_name = action_name
9
9
  end
10
10
 
11
- # checks if pattern matches path and method matches request method
12
11
  def matches?(req)
13
12
  return false if (pattern =~ req.path).nil?
14
13
 
15
- # check if form is providing non-get/post method
16
14
  req_method = req.params["_method"] || req.request_method
17
15
 
18
16
  req_method.upcase == http_method.to_s.upcase
19
17
  end
20
18
 
21
- # use pattern to pull out route params (save for later?)
22
- # instantiate controller and call controller action
23
19
  def run(req, res)
24
20
  matched_params = pattern.match(req.path)
25
21
 
@@ -39,32 +35,16 @@ class Router
39
35
  @routes = []
40
36
  end
41
37
 
42
- # simply adds a new route to the list of routes
43
- def add_route(pattern, method, controller_class, action_name)
44
- route = Route.new(pattern, method, controller_class, action_name)
45
- @routes << route
46
- end
47
-
48
- # evaluate the proc in the context of the instance
49
- # for syntactic sugar :)
50
38
  def draw(&proc)
51
39
  self.instance_eval(&proc)
52
40
  end
53
41
 
54
- # make each of these methods that
55
- # when called add route
56
42
  [:get, :post, :put, :delete].each do |http_method|
57
43
  define_method(http_method) do |pattern, controller_class, action_name|
58
44
  add_route(pattern, http_method, controller_class, action_name)
59
45
  end
60
46
  end
61
47
 
62
- # should return the route that matches this request
63
- def match(req)
64
- routes.find { |route| route.matches?(req) }
65
- end
66
-
67
- # either throw 404 or call run on a matched route
68
48
  def run(req, res)
69
49
  matched_route = match(req)
70
50
 
@@ -75,4 +55,14 @@ class Router
75
55
  res.status = 404
76
56
  end
77
57
  end
58
+
59
+ private
60
+ def match(req)
61
+ routes.find { |route| route.matches?(req) }
62
+ end
63
+
64
+ def add_route(pattern, method, controller_class, action_name)
65
+ route = Route.new(pattern, method, controller_class, action_name)
66
+ @routes << route
67
+ end
78
68
  end
@@ -19,6 +19,7 @@ class StaticAssetServer
19
19
  res.finish
20
20
  end
21
21
 
22
+ private
22
23
  def servable?(path)
23
24
  path.match("#{root}")
24
25
  end
@@ -51,7 +52,7 @@ class FileServer
51
52
  def requested_file_name(env)
52
53
  req = Rack::Request.new(env)
53
54
  path = req.path
54
- dir = File.dirname(__FILE__)
55
- File.join(dir, '..', path)
55
+ dir = Gazebo::ROOT
56
+ File.join(dir, path)
56
57
  end
57
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gazebo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tassos Bareiss