flame 1.0.6 → 1.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: 384b92d10d4900497a38949a18f91ac6ac823132
4
- data.tar.gz: c0fc2d6ce00fc8d604e3e0a80e87561e7f16c98b
3
+ metadata.gz: 9af50356cd5318c1e13712a1acb95b4ac713a5df
4
+ data.tar.gz: b6fc7c64f4e3f381448e9b4dff1079e8519ad79c
5
5
  SHA512:
6
- metadata.gz: 61e9d6978c5ef17f5610e95b429cecc2933e876c506d045e352f18d76d0c95b710e986ad3ced8606c9f954f75b2f22bb8f0054b438cf6afac43cde7b6f350f99
7
- data.tar.gz: 636d98627dec1e1e85be4f8af0cf847bd6207381d68b02120454c3c5f2bf78a8efffc4e734a8a330a6c2eb9ded1e1175448dc343a50411f54a945787b05f0164
6
+ metadata.gz: 1cdc29130ab8009c1309876e1e74e58a98b3e4709227b0cd3715abdf86f253bb19dada401c22d9fd46d249092155af3b792d2eb9b89e00709ce2789aa4ba2fec
7
+ data.tar.gz: b7555fa03d18d8a1fb9f43af2fb7a142dc44abd4bc67f8ef05d692b5bf9f7ec0811f7e2cc73541c88b3cc4cfe8a266a76a4d6efa7477aa67c38360d99a1a8f1a
data/lib/flame.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'gorilla-patch/string'
2
2
 
3
- require_relative './flame/application'
4
- require_relative './flame/controller'
3
+ require_relative 'flame/application'
4
+ require_relative 'flame/controller'
@@ -1,5 +1,5 @@
1
- require_relative './router'
2
- require_relative './dispatcher'
1
+ require_relative 'router'
2
+ require_relative 'dispatcher'
3
3
 
4
4
  module Flame
5
5
  ## Core class, like Framework::Application
@@ -1,4 +1,4 @@
1
- require_relative './render'
1
+ require_relative 'render'
2
2
 
3
3
  module Flame
4
4
  ## Class for controllers helpers, like Framework::Controller
@@ -1,4 +1,5 @@
1
1
  require 'rack'
2
+ require_relative 'request'
2
3
 
3
4
  module Flame
4
5
  ## Class initialize when Application.call(env) invoked
@@ -25,8 +26,12 @@ module Flame
25
26
  @app.config
26
27
  end
27
28
 
29
+ def router
30
+ @app.router
31
+ end
32
+
28
33
  def request
29
- @request ||= Rack::Request.new(@env)
34
+ @request ||= Flame::Request.new(@env)
30
35
  end
31
36
 
32
37
  def params
@@ -63,10 +68,9 @@ module Flame
63
68
 
64
69
  def try_route
65
70
  method = params['_method'] || request.request_method
66
- path = request.path_info
67
71
  route = @app.router.find_route(
68
72
  method: method,
69
- path: path
73
+ path_parts: request.path_parts
70
74
  )
71
75
  # p route
72
76
  return nil unless route
@@ -0,0 +1,8 @@
1
+ module Flame
2
+ ## Class for requests
3
+ class Request < Rack::Request
4
+ def path_parts
5
+ @path_parts ||= path_info.to_s.split('/').reject(&:empty?)
6
+ end
7
+ end
8
+ end
data/lib/flame/route.rb CHANGED
@@ -4,21 +4,15 @@ module Flame
4
4
  attr_reader :attributes
5
5
 
6
6
  def initialize(attrs = {})
7
- @attributes = attrs
7
+ @attributes = attrs.merge(
8
+ path_parts: attrs[:path].to_s.split('/').reject(&:empty?)
9
+ )
8
10
  end
9
11
 
10
12
  def [](attribute)
11
13
  @attributes[attribute]
12
14
  end
13
15
 
14
- ## Arguments in order as parameters of method of controller
15
- def arrange_arguments(args = {})
16
- self[:controller].instance_method(self[:action]).parameters
17
- .each_with_object([]) do |par, arr|
18
- arr << args[par[1]] if par[0] == :req || args[par[1]]
19
- end
20
- end
21
-
22
16
  ## Compare attributes for `Router.find_route`
23
17
  def compare_attributes(attrs)
24
18
  attrs.each do |name, value|
@@ -29,37 +23,41 @@ module Flame
29
23
 
30
24
  ## Assign arguments to path for `Controller.path_to`
31
25
  def assign_arguments(args = {})
32
- extract_path_parts
26
+ self[:path_parts]
33
27
  .map { |path_part| assign_argument(path_part, args) }
34
28
  .unshift('').join('/').gsub(%r{\/{2,}}, '/')
35
29
  end
36
30
 
37
31
  ## Execute by Application.call
38
- def execute(app)
39
- ctrl = self[:controller].new(app)
40
- args = extract_arguments(app.request.path_info)
41
- app.params.merge!(args)
42
- ctrl.send(self[:action], *arrange_arguments(args))
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)
43
37
  end
44
38
 
45
39
  private
46
40
 
47
- ## Helpers for attributes accessors
48
- def extract_path_parts(other_path = self[:path])
49
- other_path.to_s.split('/').reject(&:empty?)
41
+ ## 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
51
+ end
52
+ @args
50
53
  end
51
54
 
52
- ## Extract arguments from request_path for `execute`
53
- def extract_arguments(request_path)
54
- args = {}
55
- request_parts = extract_path_parts(request_path)
56
- extract_path_parts.each_with_index do |path_part, i|
57
- next unless path_part[0] == ':'
58
- args[
59
- path_part[(path_part[1] == '?' ? 2 : 1)..-1].to_sym
60
- ] = URI.decode(request_parts[i])
61
- end
62
- args
55
+ ## Arguments in order as parameters of method of controller
56
+ def arranged_arguments
57
+ self[:controller].instance_method(self[:action]).parameters
58
+ .each_with_object([]) do |par, arr|
59
+ arr << @args[par[1]] if par[0] == :req || @args[par[1]]
60
+ end
63
61
  end
64
62
 
65
63
  ## Helpers for `compare_attributes`
@@ -67,8 +65,8 @@ module Flame
67
65
  case name
68
66
  when :method
69
67
  compare_method(value)
70
- when :path
71
- compare_path(value)
68
+ when :path_parts
69
+ compare_path_parts(value)
72
70
  else
73
71
  self[name] == value
74
72
  end
@@ -78,18 +76,13 @@ module Flame
78
76
  self[:method].upcase.to_sym == request_method.upcase.to_sym
79
77
  end
80
78
 
81
- def compare_path(request_path)
82
- path_parts = extract_path_parts
83
- request_parts = extract_path_parts(request_path)
79
+ def compare_path_parts(request_parts)
84
80
  # p route_path
85
- req_path_parts = path_parts.select { |part| part[1] != '?' }
81
+ req_path_parts = self[:path_parts].select { |part| part[1] != '?' }
86
82
  return false if request_parts.count < req_path_parts.count
87
- compare_parts(request_parts, path_parts)
88
- end
89
-
90
- def compare_parts(request_parts, path_parts)
83
+ # compare_parts(request_parts, self[:path_parts])
91
84
  request_parts.each_with_index do |request_part, i|
92
- path_part = path_parts[i]
85
+ path_part = self[:path_parts][i]
93
86
  # p request_part, path_part
94
87
  break false unless path_part
95
88
  next if path_part[0] == ':'
data/lib/flame/router.rb CHANGED
@@ -1,13 +1,14 @@
1
- require_relative './route.rb'
2
- require_relative './validators.rb'
1
+ require_relative 'route'
2
+ require_relative 'validators'
3
3
 
4
4
  module Flame
5
5
  ## Router class for routing
6
6
  class Router
7
- attr_accessor :routes
7
+ attr_accessor :routes, :befores
8
8
 
9
9
  def initialize
10
10
  @routes = []
11
+ @befores = {}
11
12
  end
12
13
 
13
14
  def add_controller(ctrl, path, block = nil)
@@ -15,9 +16,10 @@ module Flame
15
16
  ## TODO: Add `before` and `after` methods
16
17
 
17
18
  ## Add routes from controller to glob array
18
- ctrl_routes = RouteRefine.new(ctrl, path, block).routes
19
- ActionsValidator.new(ctrl_routes, ctrl).valid?
20
- routes.concat(ctrl_routes)
19
+ ctrl_routes = RouteRefine.new(ctrl, path, block)
20
+ ActionsValidator.new(ctrl_routes.routes, ctrl).valid?
21
+ routes.concat(ctrl_routes.routes)
22
+ befores[ctrl] = ctrl_routes.befores
21
23
  end
22
24
 
23
25
  ## Find route by any attributes
@@ -25,12 +27,18 @@ module Flame
25
27
  routes.find { |route| route.compare_attributes(attrs) }
26
28
  end
27
29
 
30
+ ## Find before hook by Route
31
+ def find_befores(route)
32
+ (befores[route[:controller]][:*] || []) +
33
+ (befores[route[:controller]][route[:action]] || [])
34
+ end
35
+
28
36
  private
29
37
 
30
38
  ## Helper module for routing refine
31
39
  class RouteRefine
32
40
  attr_accessor :rest_routes
33
- attr_reader :routes
41
+ attr_reader :routes, :befores
34
42
 
35
43
  def self.http_methods
36
44
  [:GET, :POST, :PUT, :DELETE]
@@ -50,6 +58,7 @@ module Flame
50
58
  @ctrl = ctrl
51
59
  @path = path || default_controller_path
52
60
  @routes = []
61
+ @befores = {}
53
62
  block.nil? ? defaults : instance_exec(&block)
54
63
  # p @routes
55
64
  @routes.sort! { |a, b| b[:path] <=> a[:path] }
@@ -62,6 +71,11 @@ module Flame
62
71
  end
63
72
  end
64
73
 
74
+ def before(actions, action)
75
+ actions = [actions] unless actions.is_a?(Array)
76
+ actions.each { |a| (@befores[a] ||= []).push(action) }
77
+ end
78
+
65
79
  def defaults
66
80
  @ctrl.public_instance_methods(false).each do |action|
67
81
  next if find_route_index(action: action)
@@ -84,11 +98,8 @@ module Flame
84
98
  using GorillaPatch::StringExt
85
99
 
86
100
  def default_controller_path
87
- @ctrl.name.underscore
88
- .split('_')
89
- .take_while { |part| part != 'controller' }
90
- .unshift(nil)
91
- .join('/')
101
+ (@ctrl.name.underscore.split('_') - %w(controller ctrl))
102
+ .unshift(nil).join('/')
92
103
  end
93
104
 
94
105
  def make_path(path, action = nil, force_params = false)
@@ -1,4 +1,4 @@
1
- require_relative './errors.rb'
1
+ require_relative 'errors'
2
2
 
3
3
  module Flame
4
4
  ## Compare arguments from path and from controller's action
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.0.6
4
+ version: 1.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-19 00:00:00.000000000 Z
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -84,6 +84,7 @@ files:
84
84
  - lib/flame/dispatcher.rb
85
85
  - lib/flame/errors.rb
86
86
  - lib/flame/render.rb
87
+ - lib/flame/request.rb
87
88
  - lib/flame/route.rb
88
89
  - lib/flame/router.rb
89
90
  - lib/flame/validators.rb