flame 4.0.7.2 → 4.0.9

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: 9321147aff3df22a9bb2b87150e51c7399299660
4
- data.tar.gz: 54a8172fe1dda218fe68723b3d2ee1c0352ec5f9
3
+ metadata.gz: 75e02998d8d17eae77b354d3e0f728bf0fbf37e6
4
+ data.tar.gz: c6e806c54b89f1f89e3bad8334f654fad103278b
5
5
  SHA512:
6
- metadata.gz: b6468b13a0dc138a44b662897026b27105671be32d2109b8a02ef6261cb53982e8319c7982cb71614585cbd08c954507e9cf186b7ce53397f5f8c22752c5eb2b
7
- data.tar.gz: f025e272406a3ee31e6d898f16fcad395da8a4d4d4fcf25fd256a0065f7b810a04cb05a44214b184be4b58949db9a2f43fa8ec8a3265d9bf5bb80032b5ff12ed
6
+ metadata.gz: 357ad3becd4d875377fa55c76b23eb4f2ab7d1c94e9c8f3493751b125f4d085e747bce14d0aeac8463d74095ab433dd6e6842fccb122f8b39c295f8b2784f42b
7
+ data.tar.gz: 7fda6f630bedcd1755161218e567a9c0741536145213ccef10579562210e560b9a5c58ebccc6cd673d19c733fd2ae366afd9be34a5de3836c632c30bc052862e
@@ -171,15 +171,15 @@ module Flame
171
171
  route = @app.router.find_nearest_route(request.path_parts)
172
172
  ## Halt with default body if the route not found
173
173
  ## or it's `not_found` method not defined
174
- return halt unless route && route[:controller].method_defined?(:not_found)
174
+ return halt unless route && route.controller.method_defined?(:not_found)
175
175
  ## Execute `not_found` method for the founded route
176
176
  route_exec(route, :not_found)
177
177
  end
178
178
 
179
179
  ## Create controller object and execute method
180
180
  def route_exec(route, method = nil)
181
- method ||= route[:action]
182
- route[:controller].new(self).send(:execute, method)
181
+ method ||= route.action
182
+ route.controller.new(self).send(:execute, method)
183
183
  end
184
184
  end
185
185
  end
data/lib/flame/route.rb CHANGED
@@ -1,27 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Flame
2
4
  class Router
5
+ ARG_CHAR = ':'.freeze
6
+ ARG_CHAR_OPT = '?'.freeze
7
+
3
8
  ## Class for Route in Router.routes
4
9
  class Route
5
- attr_reader :attributes
6
-
7
- def initialize(attrs = {})
8
- @attributes = attrs.merge(
9
- ## Split path to parts (Array of String)
10
- path_parts: attrs[:path].to_s.split('/').reject(&:empty?).freeze
11
- )
12
- end
10
+ attr_reader :method, :controller, :action, :path, :path_parts
13
11
 
14
- ## Get the attribute of route
15
- ## @param key [Symbol] name of attribute
16
- def [](key)
17
- @attributes[key]
12
+ def initialize(controller, action, method, path)
13
+ @controller = controller
14
+ @action = action
15
+ @method = method.to_sym.upcase
16
+ ## MAKE PATH
17
+ @path = path
18
+ Validators::ArgumentsValidator.new(@controller, @path, @action).valid?
19
+ @path_parts = @path.to_s.split('/').reject(&:empty?)
20
+ freeze
18
21
  end
19
22
 
20
- ## Set the attribute of route
21
- ## @param key [Symbol] name of attribute
22
- ## @param value [Object] value of attribute
23
- def []=(key, value)
24
- @attributes[key] = value
23
+ def freeze
24
+ @path.freeze
25
+ @path_parts.freeze
26
+ super
25
27
  end
26
28
 
27
29
  ## Compare attributes for `Router.find_route`
@@ -36,25 +38,30 @@ module Flame
36
38
  ## Assign arguments to path for `Controller.path_to`
37
39
  ## @param args [Hash] arguments for assigning
38
40
  def assign_arguments(args = {})
39
- self[:path_parts]
40
- .map { |path_part| assign_argument(path_part, args) }
41
- .unshift('').join('/').gsub(%r{\/{2,}}, '/')
41
+ parts = @path_parts.map { |part| assign_argument(part, args) }
42
+ self.class.path_merge(parts.unshift(''))
42
43
  end
43
44
 
44
45
  ## Extract arguments from request_parts for `execute`
45
46
  ## @param request_parts [Array] parts of the request (Array of String)
46
47
  def arguments(request_parts)
47
- self[:path_parts].each_with_index.with_object({}) do |(path_part, i), args|
48
+ @path_parts.each_with_index.with_object({}) do |(path_part, i), args|
48
49
  request_part = request_parts[i]
49
- path_part_opt = path_part[1] == '?'
50
- next args unless path_part[0] == ':'
50
+ path_part_opt = path_part[1] == ARG_CHAR_OPT
51
+ next args unless path_part[0] == ARG_CHAR
51
52
  break args if path_part_opt && request_part.nil?
52
53
  args[
53
- path_part[(path_part_opt ? 2 : 1)..-1].to_sym
54
+ path_part[(path_part_opt ? 2 : 1)..-1].to_sym
54
55
  ] = URI.decode(request_part)
55
56
  end
56
57
  end
57
58
 
59
+ def self.path_merge(*parts)
60
+ parts.join('/').gsub(%r{\/{2,}}, '/')
61
+ end
62
+
63
+ private
64
+
58
65
  ## Helpers for `compare_attributes`
59
66
  def compare_attribute(name, value)
60
67
  case name
@@ -63,24 +70,24 @@ module Flame
63
70
  when :path_parts
64
71
  compare_path_parts(value)
65
72
  else
66
- self[name] == value
73
+ send(name) == value
67
74
  end
68
75
  end
69
76
 
70
77
  def compare_method(request_method)
71
- self[:method].upcase.to_sym == request_method.upcase.to_sym
78
+ method.upcase.to_sym == request_method.upcase.to_sym
72
79
  end
73
80
 
74
81
  def compare_path_parts(request_parts)
75
82
  # p route_path
76
- req_path_parts = self[:path_parts].select { |part| part[1] != '?' }
83
+ req_path_parts = @path_parts.select { |part| part[1] != ARG_CHAR_OPT }
77
84
  return false if request_parts.count < req_path_parts.count
78
85
  # compare_parts(request_parts, self[:path_parts])
79
86
  request_parts.each_with_index do |request_part, i|
80
- path_part = self[:path_parts][i]
87
+ path_part = @path_parts[i]
81
88
  # p request_part, path_part
82
89
  break false unless path_part
83
- next if path_part[0] == ':'
90
+ next if path_part[0] == ARG_CHAR
84
91
  break false unless request_part == path_part
85
92
  end
86
93
  end
@@ -88,13 +95,13 @@ module Flame
88
95
  ## Helpers for `assign_arguments`
89
96
  def assign_argument(path_part, args = {})
90
97
  ## Not argument
91
- return path_part unless path_part[0] == ':'
98
+ return path_part unless path_part[0] == ARG_CHAR
92
99
  ## Not required argument
93
- return args[path_part[2..-1].to_sym] if path_part[1] == '?'
100
+ return args[path_part[2..-1].to_sym] if path_part[1] == ARG_CHAR_OPT
94
101
  ## Required argument
95
102
  param = args[path_part[1..-1].to_sym]
96
103
  ## Required argument is nil
97
- error = Errors::ArgumentNotAssignedError.new(self[:path], path_part)
104
+ error = Errors::ArgumentNotAssignedError.new(path, path_part)
98
105
  fail error if param.nil?
99
106
  ## All is ok
100
107
  param
data/lib/flame/router.rb CHANGED
@@ -67,9 +67,9 @@ module Flame
67
67
  @rest_routes ||= [
68
68
  { method: :GET, path: '/', action: :index },
69
69
  { method: :POST, path: '/', action: :create },
70
- { method: :GET, path: '/:id', action: :show },
71
- { method: :PUT, path: '/:id', action: :update },
72
- { method: :DELETE, path: '/:id', action: :delete }
70
+ { method: :GET, path: '/', action: :show },
71
+ { method: :PUT, path: '/', action: :update },
72
+ { method: :DELETE, path: '/', action: :delete }
73
73
  ]
74
74
  end
75
75
 
@@ -94,13 +94,17 @@ module Flame
94
94
  ## @param action [Symbol] name of method for the request
95
95
  ## @example Set method to :POST for action `goodbye`
96
96
  ## post :goodbye
97
- define_method(request_method.downcase) do |path, action = nil|
98
- if action.nil?
97
+ method = request_method.downcase
98
+ define_method(method) do |path, action = nil, prefix: false|
99
+ unless action
99
100
  action = path.to_sym
100
- path = "/#{path}"
101
+ path = nil
101
102
  end
102
- Validators::ArgumentsValidator.new(@ctrl, path, action).valid?
103
- add_route(request_method, path, action)
103
+ path = default_action_path(action, path) if prefix || path.nil?
104
+ path = Route.path_merge(@path, path)
105
+ route = Route.new(@ctrl, action, method, path)
106
+ index = find_route_index(action: action)
107
+ index ? @routes[index] = route : @routes.push(route)
104
108
  end
105
109
  end
106
110
 
@@ -110,7 +114,7 @@ module Flame
110
114
  rest
111
115
  @ctrl.public_instance_methods(false).each do |action|
112
116
  next if find_route_index(action: action)
113
- add_route(:GET, nil, action)
117
+ send(:GET.downcase, action)
114
118
  end
115
119
  end
116
120
 
@@ -118,10 +122,9 @@ module Flame
118
122
  def rest
119
123
  rest_routes.each do |rest_route|
120
124
  action = rest_route[:action]
121
- if @ctrl.public_instance_methods.include?(action) &&
125
+ next unless @ctrl.public_instance_methods.include?(action) &&
122
126
  find_route_index(action: action).nil?
123
- add_route(*rest_route.values, true)
124
- end
127
+ send(*rest_route.values.map(&:downcase), prefix: true)
125
128
  end
126
129
  end
127
130
 
@@ -130,10 +133,7 @@ module Flame
130
133
  ## @param path [String, nil] root path for mounting controller
131
134
  ## @yield Block of code for routes refine
132
135
  def mount(ctrl, path = nil, &block)
133
- path = path_merge(
134
- @path,
135
- (path || ctrl.default_path)
136
- )
136
+ path = Route.path_merge(@path, path || ctrl.default_path)
137
137
  @router.add_controller(ctrl, path, block)
138
138
  end
139
139
 
@@ -142,48 +142,27 @@ module Flame
142
142
  ## Execute block of refinings end sorting routes
143
143
  def execute(&block)
144
144
  block.nil? ? defaults : instance_exec(&block)
145
- # instance_exec(&@ctrl.mounted) if @ctrl.respond_to? :mounted
146
- # @router.app.helpers.each do |helper|
147
- # instance_exec(&helper.mount) if helper.respond_to?(:mount)
148
- # end
149
- # p @routes
150
- @routes.sort! { |a, b| b[:path] <=> a[:path] }
145
+ @routes.sort! { |a, b| b.path <=> a.path }
146
+ end
147
+
148
+ def find_route_index(attrs)
149
+ @routes.find_index { |route| route.compare_attributes(attrs) }
151
150
  end
152
151
 
153
152
  ## Build path for the action of controller
154
- def make_path(path, action = nil, force_params = false)
155
- ## TODO: Add :arg:type support (:id:num, :name:str, etc.)
156
- unshifted = force_params ? path : action_path(action)
157
- if path.nil? || force_params
158
- parameters = @ctrl.instance_method(action).parameters
159
- parameters.map! { |par| ":#{par[0] == :req ? '' : '?'}#{par[1]}" }
160
- path = parameters.unshift(unshifted).join('/')
153
+ ## @todo Add :arg:type support (:id:num, :name:str, etc.)
154
+ def default_action_path(action, prefix)
155
+ unshifted = prefix ? prefix : action_prefix(action)
156
+ parameters = @ctrl.instance_method(action).parameters
157
+ parameters.map! do |par|
158
+ ":#{par[0] == :req ? '' : ARG_CHAR_OPT}#{par[1]}"
161
159
  end
162
- path_merge(@path, path)
160
+ Route.path_merge(parameters.unshift(unshifted))
163
161
  end
164
162
 
165
- def action_path(action)
163
+ def action_prefix(action)
166
164
  action == :index ? '/' : action
167
165
  end
168
-
169
- def path_merge(*parts)
170
- parts.join('/').gsub(%r{\/{2,}}, '/')
171
- end
172
-
173
- def add_route(method, path, action, force_params = false)
174
- route = Route.new(
175
- method: method,
176
- path: make_path(path, action, force_params),
177
- controller: @ctrl,
178
- action: action
179
- )
180
- index = find_route_index(action: action)
181
- index ? @routes[index] = route : @routes.push(route)
182
- end
183
-
184
- def find_route_index(attrs)
185
- @routes.find_index { |route| route.compare_attributes(attrs) }
186
- end
187
166
  end
188
167
  end
189
168
  end
@@ -1,3 +1,4 @@
1
+ require_relative 'route'
1
2
  require_relative 'errors'
2
3
 
3
4
  module Flame
@@ -23,8 +24,11 @@ module Flame
23
24
 
24
25
  ## Split path to args array
25
26
  def path_arguments(path)
26
- args = path.split('/').select { |part| part[0] == ':' }
27
- args.map { |arg| arg[1..-1].to_sym }
27
+ args = path.split('/').select { |part| part[0] == Router::ARG_CHAR }
28
+ args.map do |arg|
29
+ opt_arg = arg[1] == Router::ARG_CHAR_OPT
30
+ arg[(opt_arg ? 2 : 1)..-1].to_sym
31
+ end
28
32
  end
29
33
 
30
34
  ## Take args from controller's action
@@ -59,7 +63,7 @@ module Flame
59
63
  ## Compare actions from routes and from controller
60
64
  class ActionsValidator
61
65
  def initialize(route_refine)
62
- @routes_actions = route_refine.routes.map { |route| route[:action] }
66
+ @routes_actions = route_refine.routes.map { |route| route.action }
63
67
  @ctrl = route_refine.ctrl
64
68
  @ctrl_actions = {
65
69
  public: @ctrl.public_instance_methods(false),
data/lib/flame/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flame
4
- VERSION = '4.0.7.2'.freeze
4
+ VERSION = '4.0.9'.freeze
5
5
  end
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.0.7.2
4
+ version: 4.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-07 00:00:00.000000000 Z
11
+ date: 2016-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack