flame 5.0.0.rc4 → 5.0.0.rc7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +929 -0
  3. data/LICENSE.txt +19 -0
  4. data/README.md +135 -0
  5. data/lib/flame/application.rb +47 -46
  6. data/lib/flame/config.rb +73 -0
  7. data/lib/flame/controller/actions.rb +122 -0
  8. data/lib/flame/{dispatcher → controller}/cookies.rb +8 -3
  9. data/lib/flame/controller/path_to.rb +34 -10
  10. data/lib/flame/controller.rb +45 -78
  11. data/lib/flame/dispatcher/request.rb +22 -6
  12. data/lib/flame/dispatcher/routes.rb +22 -14
  13. data/lib/flame/dispatcher/static.rb +13 -9
  14. data/lib/flame/dispatcher.rb +15 -18
  15. data/lib/flame/errors/argument_not_assigned_error.rb +3 -8
  16. data/lib/flame/errors/config_file_not_found_error.rb +17 -0
  17. data/lib/flame/errors/controller_not_found_error.rb +19 -0
  18. data/lib/flame/errors/route_arguments_order_error.rb +3 -10
  19. data/lib/flame/errors/route_extra_arguments_error.rb +7 -20
  20. data/lib/flame/errors/route_not_found_error.rb +4 -9
  21. data/lib/flame/errors/template_not_found_error.rb +2 -8
  22. data/lib/flame/path.rb +36 -18
  23. data/lib/flame/render.rb +13 -5
  24. data/lib/flame/router/controller_finder.rb +56 -0
  25. data/lib/flame/router/route.rb +9 -0
  26. data/lib/flame/router/routes.rb +56 -9
  27. data/lib/flame/router/routes_refine/mounting.rb +57 -0
  28. data/lib/flame/router/routes_refine.rb +144 -0
  29. data/lib/flame/router.rb +7 -157
  30. data/lib/flame/validators.rb +14 -10
  31. data/lib/flame/version.rb +1 -1
  32. data/lib/flame.rb +12 -5
  33. metadata +107 -96
  34. data/bin/flame +0 -16
  35. data/lib/flame/application/config.rb +0 -49
  36. data/template/.editorconfig +0 -15
  37. data/template/.gitignore +0 -28
  38. data/template/.rubocop.yml +0 -14
  39. data/template/Gemfile +0 -55
  40. data/template/Rakefile +0 -824
  41. data/template/application.rb.erb +0 -10
  42. data/template/config/config.rb.erb +0 -56
  43. data/template/config/database.example.yml +0 -5
  44. data/template/config/deploy.example.yml +0 -2
  45. data/template/config/puma.rb +0 -56
  46. data/template/config/sequel.rb.erb +0 -22
  47. data/template/config/server.example.yml +0 -32
  48. data/template/config/session.example.yml +0 -7
  49. data/template/config.ru.erb +0 -72
  50. data/template/controllers/_controller.rb.erb +0 -14
  51. data/template/controllers/site/_controller.rb.erb +0 -18
  52. data/template/controllers/site/index_controller.rb.erb +0 -12
  53. data/template/db/.keep +0 -0
  54. data/template/filewatchers.yml +0 -12
  55. data/template/helpers/.keep +0 -0
  56. data/template/lib/.keep +0 -0
  57. data/template/locales/en.yml +0 -0
  58. data/template/models/.keep +0 -0
  59. data/template/public/.keep +0 -0
  60. data/template/server +0 -200
  61. data/template/services/.keep +0 -0
  62. data/template/views/.keep +0 -0
  63. data/template/views/site/index.html.erb.erb +0 -1
  64. data/template/views/site/layout.html.erb.erb +0 -10
data/lib/flame/router.rb CHANGED
@@ -3,16 +3,19 @@
3
3
  require 'gorilla_patch/deep_merge'
4
4
  require 'gorilla_patch/inflections'
5
5
  require 'gorilla_patch/namespace'
6
- require 'gorilla_patch/transform'
7
6
 
8
- require_relative 'router/routes'
9
- require_relative 'router/route'
7
+ require_relative 'router/controller_finder'
8
+ require_relative 'errors/controller_not_found_error'
10
9
 
11
10
  module Flame
12
11
  ## Router class for routing
13
12
  class Router
14
13
  HTTP_METHODS = %i[GET POST PUT PATCH DELETE].freeze
15
14
 
15
+ require_relative 'router/route'
16
+ require_relative 'router/routes'
17
+ require_relative 'router/routes_refine'
18
+
16
19
  extend Forwardable
17
20
  def_delegators :routes, :navigate
18
21
 
@@ -40,7 +43,7 @@ module Flame
40
43
  def find_nearest_route(path)
41
44
  path_parts = path.parts.dup
42
45
  loop do
43
- route = routes.navigate(*path_parts)&.values&.grep(Route)&.first
46
+ route = routes.navigate(*path_parts)&.first_route
44
47
  break route if route || path_parts.pop.nil?
45
48
  end
46
49
  end
@@ -60,158 +63,5 @@ module Flame
60
63
  end
61
64
  reverse_routes.dig(controller.to_s, action)
62
65
  end
63
-
64
- ## Helper class for controller routing refine
65
- class RoutesRefine
66
- attr_reader :routes, :reverse_routes
67
-
68
- ## Defaults REST routes (methods, pathes, controllers actions)
69
- def self.rest_routes
70
- @rest_routes ||= [
71
- { method: :GET, path: '/', action: :index },
72
- { method: :POST, path: '/', action: :create },
73
- { method: :GET, path: '/', action: :show },
74
- { method: :PUT, path: '/', action: :update },
75
- { method: :DELETE, path: '/', action: :delete }
76
- ]
77
- end
78
-
79
- def initialize(router, namespace_name, controller_name, path, &block)
80
- @router = router
81
- @controller = constantize_controller namespace_name, controller_name
82
- @path = Flame::Path.new(path || @controller.default_path)
83
- @routes, @endpoint = @path.to_routes_with_endpoint
84
- @reverse_routes = {}
85
- execute(&block)
86
- end
87
-
88
- private
89
-
90
- using GorillaPatch::Inflections
91
-
92
- def constantize_controller(namespace_name, controller_name)
93
- controller_name = controller_name.to_s.camelize
94
- namespace =
95
- namespace_name.empty? ? Object : Object.const_get(namespace_name)
96
- if namespace.const_defined?(controller_name)
97
- controller = namespace.const_get(controller_name)
98
- return controller if controller < Flame::Controller
99
- controller::IndexController
100
- else
101
- namespace.const_get("#{controller_name}Controller")
102
- end
103
- end
104
-
105
- HTTP_METHODS.each do |http_method|
106
- ## Define refine methods for all HTTP methods
107
- ## @overload post(path, action)
108
- ## Execute action on requested path and HTTP method
109
- ## @param path [String] path of method for the request
110
- ## @param action [Symbol] name of method for the request
111
- ## @example Set path to '/bye' and method to :POST for action `goodbye`
112
- ## post '/bye', :goodbye
113
- ## @overload post(action)
114
- ## Execute action on requested HTTP method
115
- ## @param action [Symbol] name of method for the request
116
- ## @example Set method to :POST for action `goodbye`
117
- ## post :goodbye
118
- define_method(http_method.downcase) do |action_path, action = nil|
119
- ## Swap arguments if action in path variable
120
- unless action
121
- action = action_path.to_sym
122
- action_path = nil
123
- end
124
- ## Initialize new route
125
- route = Route.new(@controller, action)
126
- ## Make path by controller method with parameners
127
- action_path = Flame::Path.new(action_path).adapt(@controller, action)
128
- ## Validate action path
129
- validate_action_path(action, action_path)
130
- ## Merge action path with controller path
131
- path = Flame::Path.new(@path, action_path)
132
- ## Remove the same route if needed
133
- remove_old_routes(action, route)
134
- ## Add new route
135
- add_new_route(route, action, path, http_method)
136
- end
137
- end
138
-
139
- ## Assign remaining methods of the controller
140
- ## to defaults pathes and HTTP methods
141
- def defaults
142
- rest
143
- @controller.actions.each do |action|
144
- next if find_reverse_route(action)
145
- send(:GET.downcase, action)
146
- end
147
- end
148
-
149
- ## Assign methods of the controller to REST architecture
150
- def rest
151
- self.class.rest_routes.each do |rest_route|
152
- action = rest_route[:action]
153
- next if !@controller.actions.include?(action) ||
154
- find_reverse_route(action)
155
- send(*rest_route.values.map(&:downcase))
156
- end
157
- end
158
-
159
- using GorillaPatch::Namespace
160
- using GorillaPatch::Transform
161
- using GorillaPatch::DeepMerge
162
-
163
- ## Mount controller inside other (parent) controller
164
- ## @param controller [Flame::Controller] class of mounting controller
165
- ## @param path [String, nil] root path for mounting controller
166
- ## @yield Block of code for routes refine
167
- def mount(controller_name, path = nil, &block)
168
- routes_refine = self.class.new(
169
- @router, @controller.deconstantize, controller_name, path, &block
170
- )
171
-
172
- @endpoint.deep_merge! routes_refine.routes
173
-
174
- @reverse_routes.merge!(
175
- routes_refine.reverse_routes.transform_values do |hash|
176
- hash.transform_values { |action_path| @path + action_path }
177
- end
178
- )
179
- end
180
-
181
- # private
182
-
183
- ## Execute block of refinings end sorting routes
184
- def execute(&block)
185
- @controller.refined_http_methods
186
- .each do |http_method, action_path, action|
187
- send(http_method, action_path, action)
188
- end
189
- instance_exec(&block) if block
190
- defaults
191
- end
192
-
193
- def find_reverse_route(action)
194
- @reverse_routes.dig(@controller.to_s, action)
195
- end
196
-
197
- def validate_action_path(action, action_path)
198
- Validators::RouteArgumentsValidator.new(
199
- @controller, action_path, action
200
- ).valid?
201
- end
202
-
203
- def remove_old_routes(action, new_route)
204
- return unless (old_path = @reverse_routes[@controller.to_s]&.delete(action))
205
- @routes.dig(*old_path.parts)
206
- .delete_if { |_method, old_route| old_route == new_route }
207
- end
208
-
209
- def add_new_route(route, action, path, http_method)
210
- path_routes, endpoint = path.to_routes_with_endpoint
211
- endpoint[http_method] = route
212
- @routes.deep_merge!(path_routes)
213
- (@reverse_routes[@controller.to_s] ||= {})[action] = path
214
- end
215
- end
216
66
  end
217
67
  end
@@ -7,6 +7,8 @@ module Flame
7
7
  module Validators
8
8
  ## Compare arguments from path and from controller's action
9
9
  class RouteArgumentsValidator
10
+ include Memery
11
+
10
12
  ## Create a new instance of validator
11
13
  ## @param ctrl [Flame::Controller] controller of route
12
14
  ## @param path [Flame::Path, String] path of route
@@ -29,6 +31,7 @@ module Flame
29
31
  extra_arguments = first_extra_arguments
30
32
  ## Raise error if extra arguments
31
33
  return true unless extra_arguments
34
+
32
35
  raise Errors::RouteExtraArgumentsError.new(
33
36
  @ctrl, @action, @path, extra_arguments
34
37
  )
@@ -37,27 +40,28 @@ module Flame
37
40
  def order_valid?
38
41
  wrong_ordered_arguments = first_wrong_ordered_arguments
39
42
  return true unless wrong_ordered_arguments
43
+
40
44
  raise Errors::RouteArgumentsOrderError.new(
41
45
  @path, wrong_ordered_arguments
42
46
  )
43
47
  end
44
48
 
45
49
  ## Split path to args array
46
- def path_arguments
47
- @path_arguments ||= @path.parts
48
- .each_with_object(req: [], opt: []) do |part, hash|
49
- ## Take only argument parts
50
- next unless part.arg?
51
- ## Memorize arguments
52
- hash[part.opt_arg? ? :opt : :req] << part.clean.to_sym
53
- end
50
+ memoize def path_arguments
51
+ @path.parts.each_with_object(req: [], opt: []) do |part, hash|
52
+ ## Take only argument parts
53
+ next unless part.arg?
54
+
55
+ ## Memorize arguments
56
+ hash[part.opt_arg? ? :opt : :req] << part.to_sym
57
+ end
54
58
  end
55
59
 
56
60
  ## Take args from controller's action
57
- def action_arguments
61
+ memoize def action_arguments
58
62
  ## Get all parameters (arguments) from method
59
63
  ## Than collect and sort parameters into hash
60
- @action_arguments ||= @ctrl.instance_method(@action).parameters
64
+ @ctrl.instance_method(@action).parameters
61
65
  .each_with_object(req: [], opt: []) do |param, hash|
62
66
  ## Only required parameters must be in `:req`
63
67
  hash[param[0]] << param[1]
data/lib/flame/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flame
4
- VERSION = '5.0.0.rc4'
4
+ VERSION = '5.0.0.rc7'
5
5
  end
data/lib/flame.rb CHANGED
@@ -1,8 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'addressable'
4
- require 'rack'
3
+ require 'gorilla_patch/inflections'
5
4
 
6
- require_relative 'flame/application'
7
- require_relative 'flame/controller'
8
- require_relative 'flame/version'
5
+ ## Base module
6
+ module Flame
7
+ using GorillaPatch::Inflections
8
+
9
+ %i[Config Application Controller VERSION]
10
+ .each do |constant_name|
11
+ autoload(
12
+ constant_name, "#{__dir__}/flame/#{constant_name.to_s.underscore}"
13
+ )
14
+ end
15
+ end