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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d769cfd063c4c30f57f93caf9bcea00762ee88b
4
- data.tar.gz: cf5f851acfee41d52b948842bf05c490bda8874a
3
+ metadata.gz: 7a8a6e9ec49e13ce6525784b5d0d7edf48611c8e
4
+ data.tar.gz: 0d8f202fcba084fa9ea5f6a345261988eaa243cc
5
5
  SHA512:
6
- metadata.gz: 9aed5f8f99ca2673c6a33082b3480e7dba6e6cd4ea96ac5d5b83199bfdb10f870e2b95982c8da2974fed7fed9a4d6f950201155d48318ac64cf51d972f8fb77a
7
- data.tar.gz: 52f450070696fcc94174d9dfdc03dc3a92d265adecd7c6de414b2e9d4182ff58232bd1b261b2a63740335484fdd25dcb8ae7f106051997dce6a3d861d6cc0426
6
+ metadata.gz: 57d3bfaedc2df1e95e51f3c7945a51eb9e967a557fbe9107fcab8dcd66c5d3e93b67509b550c330f226fc4b4302707cdc97d9a171aa13114506cef487950c290
7
+ data.tar.gz: 9d1158d78e084299b29eaf9ccede5ccc29ea9587cd141531c94d151063af8545a8b535e9ea93fc29e6e25cebd66113b5f759a9193eff55dbd8bd1785a582c7a6
data/lib/flame.rb CHANGED
@@ -1,4 +1,3 @@
1
1
  require 'gorilla-patch/string'
2
2
 
3
3
  require_relative 'flame/application'
4
- require_relative 'flame/controller'
@@ -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
- Dispatcher.new(self, env).run!
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
@@ -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 = @app.router.find_route(controller: ctrl, action: action)
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
- method = params['_method'] || request.request_method
86
- route = @app.router.find_route(
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.execute(self)
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.class.name.underscore.split('_') - %w(controller 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
@@ -4,5 +4,9 @@ module Flame
4
4
  def path_parts
5
5
  @path_parts ||= path_info.to_s.split('/').reject(&:empty?)
6
6
  end
7
+
8
+ def http_method
9
+ params['_method'] || request_method
10
+ end
7
11
  end
8
12
  end
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 = nil)
43
- unless request_parts.nil?
44
- @args = {}
45
- self[:path_parts].each_with_index do |path_part, i|
46
- next unless path_part[0] == ':'
47
- @args[
48
- path_part[(path_part[1] == '?' ? 2 : 1)..-1].to_sym
49
- ] = URI.decode(request_parts[i])
50
- end
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 arranged_arguments
45
+ def arranged_params(params)
57
46
  self[:controller].instance_method(self[:action]).parameters
58
47
  .each_with_object([]) do |par, arr|
59
- arr << @args[par[1]] if par[0] == :req || @args[par[1]]
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: 1.1.9
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-10-25 00:00:00.000000000 Z
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
@@ -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