rapid_runty 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8e4a685ba91fc4049a3a5382aa56eb5923ebf10
4
- data.tar.gz: 10d2e2bb3caf6f36d0ae842b83fc63d0a464035b
3
+ metadata.gz: 552435a1122425b729618b289eac9ccfc527f738
4
+ data.tar.gz: 72cbc9234769d801db768c50bfeda93e335b48c8
5
5
  SHA512:
6
- metadata.gz: 568008def90b41b16a42e9bec335be81811f2d5775e6337529812e820ab016fcc6377d072c2a229abeabf072034f1809fdb534865853999846ef3cf93d487b17
7
- data.tar.gz: b09e7e72310f766892f19c7675eced90c7ffe7232b7407b67f96412f9c8ff34b9a0bac36379ccda6cc011faf0af96fcd5faa95464f14d3a6b2889acf286f655d
6
+ metadata.gz: d93783f423451db1c32708f7e7b63c16ee86a048367f9c5b376ea7ee355f46accb1244f88507aed0ef589f5911ab70a86bb7efb87555ed42688bd49dc5c7a97b
7
+ data.tar.gz: cbc6120fcc46feff483a022c66622ef920a4c76801ae1770e82808f245684e7364e87c7ffc038ea6c7cd5bde54769555eaaa280ac72259e6a2645d4dbf298186
@@ -1,5 +1,6 @@
1
1
  require 'rapid_runty/router/base_route'
2
2
  require 'rapid_runty/controller/base_controller'
3
+ require 'rapid_runty/model/base'
3
4
 
4
5
  module RapidRunty
5
6
  ##
@@ -21,10 +22,7 @@ module RapidRunty
21
22
  # @return [status, {headers}, [response]]
22
23
  def call(env)
23
24
  request = Rack::Request.new(env)
24
- response = Rack::Response.new
25
-
26
- handle(env, request, response)
27
- response.finish
25
+ handle(env, request)
28
26
  end
29
27
  end
30
28
  end
@@ -2,11 +2,17 @@ module RapidRunty
2
2
  ##
3
3
  # Application base controller
4
4
  class BaseController
5
- attr_reader :env, :response
5
+ attr_reader :env, :request
6
6
 
7
- def initialize(env, response)
7
+ def initialize(env, request)
8
8
  @env = env
9
- @response = response
9
+ @request = request
10
+ end
11
+
12
+ def call_action(action)
13
+ send(action)
14
+ render unless @response
15
+ @response
10
16
  end
11
17
 
12
18
  ##
@@ -14,7 +20,9 @@ module RapidRunty
14
20
  #
15
21
  # @return [Hash] Hash of url parameters
16
22
  def params
17
- @params ||= Rack::Utils.parse_nested_query(env['QUERY_STRING'])
23
+ @params ||= request.params.merge(
24
+ Rack::Utils.parse_nested_query(env['QUERY_STRING'])
25
+ )
18
26
  end
19
27
 
20
28
  ##
@@ -22,9 +30,11 @@ module RapidRunty
22
30
  #
23
31
  # @param [String] file name for the template
24
32
  def render(view = controller_action)
25
- render_template(layout) do
33
+ body = render_template(layout) do
26
34
  render_template(view)
27
35
  end
36
+
37
+ response(body, 200, {})
28
38
  end
29
39
 
30
40
  ##
@@ -53,8 +63,8 @@ module RapidRunty
53
63
 
54
64
  ##
55
65
  # Redirect response method
56
- def redirect_to(path)
57
- response.redirect path
66
+ def redirect_to(location)
67
+ response([], 302, "Location" => location)
58
68
  end
59
69
 
60
70
  private
@@ -62,5 +72,9 @@ module RapidRunty
62
72
  def controller_action
63
73
  File.join(env['controller'], env['action'])
64
74
  end
75
+
76
+ def response(body, status = 200, header = {})
77
+ @response = Rack::Response.new(body, status, header)
78
+ end
65
79
  end
66
80
  end
@@ -0,0 +1,52 @@
1
+ require 'rapid_runty/model/db'
2
+ require 'rapid_runty/model/base_queries'
3
+
4
+ module RapidRunty
5
+ module Model
6
+ class Base
7
+ include RapidRunty::Model::BaseQueries
8
+
9
+ def self.to_table(name)
10
+ @table = name.to_s
11
+ end
12
+
13
+ def self.property(field, attr)
14
+ @property ||= {}
15
+ @property[field] = attr
16
+ end
17
+
18
+ def self.create_table
19
+ DB.execute_query(
20
+ "CREATE TABLE IF NOT EXISTS #{@table}" \
21
+ "(#{column_names.join(', ')})"
22
+ )
23
+ @property.keys.each(&method(:attr_accessor))
24
+ end
25
+
26
+ def self.primary_key(primary)
27
+ 'PRIMARY KEY AUTOINCREMENT' if primary
28
+ end
29
+
30
+ def self.nullable(is_null)
31
+ 'NOT NULL' unless is_null
32
+ end
33
+
34
+ def self.type(value)
35
+ value.to_s
36
+ end
37
+
38
+ def self.column_names
39
+ columns = []
40
+ @property.each do |column_name, constraints|
41
+ properties_and_constraints = []
42
+ properties_and_constraints << column_name.to_s
43
+ constraints.each do |attribute, value|
44
+ properties_and_constraints << send(attribute.downcase.to_s, value)
45
+ end
46
+ columns << properties_and_constraints.join(' ')
47
+ end
48
+ columns
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,118 @@
1
+ module RapidRunty
2
+ module Model
3
+ module BaseQueries
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def find(id)
10
+ row = DB.execute_query(
11
+ "SELECT * FROM #{@table} WHERE id = ?",
12
+ id.to_i
13
+ ).first
14
+ model_object(row)
15
+ end
16
+
17
+ def find_by(search_params)
18
+ param, value = search_params.first
19
+ row = DB.execute_query(
20
+ "SELECT * FROM #{@table} WHERE #{param} = ?",
21
+ value
22
+ ).first
23
+ model_object(row)
24
+ end
25
+
26
+ def first
27
+ row = DB.execute_query(
28
+ "SELECT * FROM #{@table} ORDER BY id LIMIT 1 "
29
+ ).first
30
+ model_object(row)
31
+ end
32
+
33
+ def last
34
+ row = DB.execute_query(
35
+ "SELECT * FROM #{@table} ORDER BY id DESC LIMIT 1"
36
+ ).first
37
+ model_object(row)
38
+ end
39
+
40
+ def all
41
+ DB.execute_query(
42
+ "SELECT * FROM #{@table} ORDER BY id"
43
+ ).map(&method(:model_object))
44
+ end
45
+
46
+ def count
47
+ all.size
48
+ end
49
+
50
+ def destroy_all
51
+ DB.execute_query("DELETE from #{@table}")
52
+ end
53
+
54
+ def model_object(row)
55
+ return nil unless row
56
+ model ||= new
57
+ @property.keys.each_with_index do |key, index|
58
+ model.send("#{key}=", row[index])
59
+ end
60
+ model
61
+ end
62
+ end
63
+
64
+ def save
65
+ if id
66
+ DB.execute_query(
67
+ "UPDATE #{get_table_name} SET #{update_placeholders} WHERE id = ?",
68
+ entity_values << id
69
+ )
70
+ else
71
+ DB.execute_query(
72
+ "INSERT INTO #{get_table_name} (#{get_table_columns}) VALUES (#{new_placeholders})",
73
+ entity_values
74
+ )
75
+ end
76
+ true
77
+ end
78
+
79
+ alias save! save
80
+
81
+ def destroy
82
+ DB.execute_query("DELETE FROM #{get_table_name} WHERE id = ?", id)
83
+ end
84
+
85
+ private
86
+
87
+ def get_properties
88
+ self.class.instance_variable_get(:@property)
89
+ end
90
+
91
+ def get_table_name
92
+ self.class.instance_variable_get(:@table)
93
+ end
94
+
95
+ def entity_values
96
+ get_columns.map(&method(:send))
97
+ end
98
+
99
+ def update_placeholders
100
+ get_columns.map { |column| "#{column} = ?" }.join(',')
101
+ end
102
+
103
+ def new_placeholders
104
+ (['?'] * (get_properties.keys.size - 1)).join(', ')
105
+ end
106
+
107
+ def get_columns
108
+ columns ||= get_properties.keys
109
+ columns.delete(:id)
110
+ columns
111
+ end
112
+
113
+ def get_table_columns
114
+ get_columns.map(&:to_s).join(', ')
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,16 @@
1
+ require 'sqlite3'
2
+
3
+ module RapidRunty
4
+ module Model
5
+ class DB
6
+ def self.create_db
7
+ SQLite3::Database.new(File.join(ROOT_DIR, 'db', 'rapid.db'))
8
+ end
9
+
10
+ def self.execute_query(*query)
11
+ @db ||= create_db
12
+ @db.execute(*query)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -14,27 +14,27 @@ module RapidRunty
14
14
  # @param env
15
15
  # @param [Rack::Request] request
16
16
  # @param [Rack::Response] response
17
- def handle(env, request, response)
17
+ def handle(env, request)
18
18
  verb, path = route_args(request).values
19
19
 
20
20
  route = routes.find_route(verb, path)
21
21
  if route.nil?
22
- not_found(response, path)
22
+ not_found(path)
23
23
  else
24
24
  param = "&#{Rack::Utils.build_nested_query(route.placeholders)}"
25
25
  env['QUERY_STRING'] << param
26
26
  env.merge!(route.options)
27
- response.write dispatch(env, route, response)
27
+ dispatch(env, route, request)
28
28
  end
29
29
  end
30
30
 
31
31
  ##
32
32
  # Dispatch the Controller and it's action to be rendered
33
- def dispatch(env, route, response)
33
+ def dispatch(env, route, request)
34
34
  kontroller, action = route.options.values
35
35
 
36
36
  controller = Object.const_get("#{kontroller.camel_case}Controller")
37
- controller.new(env, response).public_send(action)
37
+ controller.new(env, request).call_action(action)
38
38
  end
39
39
 
40
40
  ##
@@ -43,17 +43,23 @@ module RapidRunty
43
43
  # @param [Rack::Response]
44
44
  #
45
45
  # @return [Rack::Response]
46
- def not_found(response, path)
47
- response.status = 404
48
- response.write "
49
- <html>
50
- <head>
51
- <body>
52
- <h1>404 Page not found for #{path}</h1>
53
- </body>
54
- </head>
55
- </html>
56
- "
46
+ def not_found(path)
47
+ [
48
+ 404,
49
+ {},
50
+ [
51
+ "
52
+ <html>
53
+ <head>
54
+ <body>
55
+ <h1>404 Page not found for #{path}</h1>
56
+ </body>
57
+ </head>
58
+ </html>
59
+ "
60
+ ]
61
+ ]
62
+
57
63
  end
58
64
 
59
65
  private
@@ -9,7 +9,7 @@ module RapidRunty
9
9
  self.verb = verb
10
10
  self.path = path
11
11
  self.options = options
12
- self.placeholders = nil
12
+ self.placeholders = {}
13
13
  end
14
14
  end
15
15
  end
@@ -32,7 +32,7 @@ module RapidRunty
32
32
  find_matching_route(path, urls, routes)
33
33
  end
34
34
 
35
- def select_routes_by_verb(_verb, routes)
35
+ def select_routes_by_verb(verb, routes)
36
36
  urls = routes.map { |route| { url: route.path }.merge route.options }
37
37
  urls
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module RapidRunty
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.4'.freeze
3
3
  end
data/rapid_runty.gemspec CHANGED
@@ -29,6 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.add_development_dependency 'simplecov', '~> 0.12'
30
30
  spec.add_development_dependency 'capybara', '~> 2.7'
31
31
  spec.add_development_dependency 'coveralls', '~> 0.8'
32
+ spec.add_development_dependency 'factory_girl', '~> 4.7'
33
+ spec.add_development_dependency 'faker', '~> 1.6'
32
34
 
33
35
  spec.add_development_dependency 'rake', '~> 10.0'
34
36
  spec.add_development_dependency 'guard', '~> 2.14'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapid_runty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Herbert Kagumba
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-23 00:00:00.000000000 Z
11
+ date: 2016-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -94,6 +94,34 @@ dependencies:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: factory_girl
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: faker
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.6'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.6'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: rake
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -289,6 +317,9 @@ files:
289
317
  - lib/rapid_runty/application.rb
290
318
  - lib/rapid_runty/controller/base_controller.rb
291
319
  - lib/rapid_runty/dependencies.rb
320
+ - lib/rapid_runty/model/base.rb
321
+ - lib/rapid_runty/model/base_queries.rb
322
+ - lib/rapid_runty/model/db.rb
292
323
  - lib/rapid_runty/router/base_route.rb
293
324
  - lib/rapid_runty/router/matcher.rb
294
325
  - lib/rapid_runty/router/route.rb