flame 1.1.9 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/flame.rb +0 -1
- data/lib/flame/application.rb +22 -1
- data/lib/flame/dispatcher.rb +21 -5
- data/lib/flame/render.rb +1 -1
- data/lib/flame/request.rb +4 -0
- data/lib/flame/route.rb +13 -22
- metadata +2 -3
- data/lib/flame/controller.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a8a6e9ec49e13ce6525784b5d0d7edf48611c8e
|
4
|
+
data.tar.gz: 0d8f202fcba084fa9ea5f6a345261988eaa243cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57d3bfaedc2df1e95e51f3c7945a51eb9e967a557fbe9107fcab8dcd66c5d3e93b67509b550c330f226fc4b4302707cdc97d9a171aa13114506cef487950c290
|
7
|
+
data.tar.gz: 9d1158d78e084299b29eaf9ccede5ccc29ea9587cd141531c94d151063af8545a8b535e9ea93fc29e6e25cebd66113b5f759a9193eff55dbd8bd1785a582c7a6
|
data/lib/flame.rb
CHANGED
data/lib/flame/application.rb
CHANGED
@@ -22,15 +22,36 @@ module Flame
|
|
22
22
|
)
|
23
23
|
end
|
24
24
|
|
25
|
+
def initialize
|
26
|
+
app = self
|
27
|
+
@builder = Rack::Builder.new do
|
28
|
+
app.class.middlewares.each { |m| use m[:class], *m[:args], &m[:block] }
|
29
|
+
run app
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
25
33
|
## Init function
|
26
34
|
def call(env)
|
27
|
-
|
35
|
+
if env[:FLAME_CALL]
|
36
|
+
Dispatcher.new(self, env).run!
|
37
|
+
else
|
38
|
+
env[:FLAME_CALL] = true
|
39
|
+
@builder.call env
|
40
|
+
end
|
28
41
|
end
|
29
42
|
|
30
43
|
def self.mount(ctrl, path = nil, &block)
|
31
44
|
router.add_controller(ctrl, path, block)
|
32
45
|
end
|
33
46
|
|
47
|
+
def self.use(middleware, *args, &block)
|
48
|
+
middlewares << { class: middleware, args: args, block: block }
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.middlewares
|
52
|
+
@middlewares ||= []
|
53
|
+
end
|
54
|
+
|
34
55
|
## Router for routing
|
35
56
|
def self.router
|
36
57
|
@router ||= Flame::Router.new
|
data/lib/flame/dispatcher.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
require 'rack'
|
2
2
|
require_relative 'request'
|
3
|
+
require_relative 'render'
|
3
4
|
|
4
5
|
module Flame
|
5
6
|
## Class initialize when Application.call(env) invoked
|
6
7
|
## For new request and response
|
7
8
|
class Dispatcher
|
9
|
+
attr_reader :controller
|
10
|
+
|
8
11
|
def initialize(app, env)
|
9
12
|
@app = app
|
10
13
|
@env = env
|
@@ -59,7 +62,7 @@ module Flame
|
|
59
62
|
end
|
60
63
|
|
61
64
|
def path_to(ctrl, action, args = {})
|
62
|
-
route =
|
65
|
+
route = router.find_route(controller: ctrl, action: action)
|
63
66
|
fail RouteNotFoundError.new(ctrl, action) unless route
|
64
67
|
path = route.assign_arguments(args)
|
65
68
|
path.empty? ? '/' : path
|
@@ -79,18 +82,31 @@ module Flame
|
|
79
82
|
@cookies ||= Cookies.new(request.cookies, response)
|
80
83
|
end
|
81
84
|
|
85
|
+
def view(path, options = {})
|
86
|
+
Flame::Render.new(self, path, options).render
|
87
|
+
end
|
88
|
+
alias_method :render, :view
|
89
|
+
|
82
90
|
private
|
83
91
|
|
84
92
|
def try_route
|
85
|
-
|
86
|
-
|
87
|
-
method: method,
|
93
|
+
route = router.find_route(
|
94
|
+
method: request.http_method,
|
88
95
|
path_parts: request.path_parts
|
89
96
|
)
|
90
97
|
# p route
|
91
98
|
return nil unless route
|
92
99
|
status 200
|
93
|
-
route.
|
100
|
+
params.merge!(route.arguments(request.path_parts))
|
101
|
+
# route.execute(self)
|
102
|
+
execute_route(route)
|
103
|
+
end
|
104
|
+
|
105
|
+
def execute_route(route)
|
106
|
+
@controller = route[:controller]
|
107
|
+
singleton_class.include @controller
|
108
|
+
router.find_befores(route).each { |before| send(before) }
|
109
|
+
send(route[:action], *route.arranged_params(params))
|
94
110
|
end
|
95
111
|
|
96
112
|
def try_static(dir = config[:public_dir])
|
data/lib/flame/render.rb
CHANGED
@@ -52,7 +52,7 @@ module Flame
|
|
52
52
|
def controller_dirs
|
53
53
|
## Build controller_dirs
|
54
54
|
controller_dir = (
|
55
|
-
@ctrl.
|
55
|
+
@ctrl.controller.name.underscore.split('_') - %w(controller ctrl)
|
56
56
|
).join('_')
|
57
57
|
[controller_dir, controller_dir.split('/').last]
|
58
58
|
end
|
data/lib/flame/request.rb
CHANGED
data/lib/flame/route.rb
CHANGED
@@ -28,38 +28,29 @@ module Flame
|
|
28
28
|
.unshift('').join('/').gsub(%r{\/{2,}}, '/')
|
29
29
|
end
|
30
30
|
|
31
|
-
## Execute by Application.call
|
32
|
-
def execute(dispatcher)
|
33
|
-
ctrl = self[:controller].new(dispatcher)
|
34
|
-
dispatcher.params.merge!(arguments(dispatcher.request.path_parts))
|
35
|
-
dispatcher.router.find_befores(self).each { |before| ctrl.send(before) }
|
36
|
-
ctrl.send(self[:action], *arranged_arguments)
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
31
|
## Extract arguments from request_parts for `execute`
|
42
|
-
def arguments(request_parts
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
32
|
+
def arguments(request_parts)
|
33
|
+
self[:path_parts].each_with_index.with_object({}) do |(path_part, i), args|
|
34
|
+
request_part = request_parts[i]
|
35
|
+
path_part_opt = path_part[1] == '?'
|
36
|
+
next args unless path_part[0] == ':'
|
37
|
+
break args if path_part_opt && request_part.nil?
|
38
|
+
args[
|
39
|
+
path_part[(path_part_opt ? 2 : 1)..-1].to_sym
|
40
|
+
] = URI.decode(request_part)
|
51
41
|
end
|
52
|
-
@args
|
53
42
|
end
|
54
43
|
|
55
44
|
## Arguments in order as parameters of method of controller
|
56
|
-
def
|
45
|
+
def arranged_params(params)
|
57
46
|
self[:controller].instance_method(self[:action]).parameters
|
58
47
|
.each_with_object([]) do |par, arr|
|
59
|
-
arr <<
|
48
|
+
arr << params[par[1]] if par[0] == :req || params[par[1]]
|
60
49
|
end
|
61
50
|
end
|
62
51
|
|
52
|
+
private
|
53
|
+
|
63
54
|
## Helpers for `compare_attributes`
|
64
55
|
def compare_attribute(name, value)
|
65
56
|
case name
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Popov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -74,7 +74,6 @@ extra_rdoc_files: []
|
|
74
74
|
files:
|
75
75
|
- lib/flame.rb
|
76
76
|
- lib/flame/application.rb
|
77
|
-
- lib/flame/controller.rb
|
78
77
|
- lib/flame/dispatcher.rb
|
79
78
|
- lib/flame/errors.rb
|
80
79
|
- lib/flame/render.rb
|
data/lib/flame/controller.rb
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
require_relative 'render'
|
2
|
-
|
3
|
-
module Flame
|
4
|
-
## Class for controllers helpers, like Framework::Controller
|
5
|
-
class Controller
|
6
|
-
def initialize(dispatcher)
|
7
|
-
@dispatcher = dispatcher
|
8
|
-
end
|
9
|
-
|
10
|
-
def config
|
11
|
-
@dispatcher.config
|
12
|
-
end
|
13
|
-
|
14
|
-
def request
|
15
|
-
@dispatcher.request
|
16
|
-
end
|
17
|
-
|
18
|
-
def response
|
19
|
-
@dispatcher.response
|
20
|
-
end
|
21
|
-
|
22
|
-
def params
|
23
|
-
@dispatcher.params
|
24
|
-
end
|
25
|
-
|
26
|
-
def view(path, options = {})
|
27
|
-
Flame::Render.new(self, path, options).render
|
28
|
-
end
|
29
|
-
alias_method :render, :view
|
30
|
-
|
31
|
-
def halt(*params)
|
32
|
-
@dispatcher.halt(*params)
|
33
|
-
end
|
34
|
-
|
35
|
-
def path_to(*params)
|
36
|
-
@dispatcher.path_to(*params)
|
37
|
-
end
|
38
|
-
|
39
|
-
def redirect(*params)
|
40
|
-
@dispatcher.redirect(*params)
|
41
|
-
end
|
42
|
-
|
43
|
-
def session
|
44
|
-
@dispatcher.session
|
45
|
-
end
|
46
|
-
|
47
|
-
def cookies
|
48
|
-
@dispatcher.cookies
|
49
|
-
end
|
50
|
-
|
51
|
-
## TODO: Add more helpers
|
52
|
-
end
|
53
|
-
end
|