draw_static 0.1.1 → 1.0.0
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.
- checksums.yaml +4 -4
- data/lib/draw_static.rb +7 -1
- data/lib/static_routes.rb +36 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ade5c4f57646f2b8460fcc508d3ca7f864215746aeba29705bf29f82a66436ea
|
4
|
+
data.tar.gz: 2accd3ae9f2aafec9f7f00699e703a92d2162b76f263c2cbcafa6b40db0c072f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 437520e18a1f74c59e73c0adbed5edb1af3b061d999cf152835e7ab47714168463538c7e618ad6c9c1b3f4220ead594a8934dbc2f5b03908534c46528dabf46e
|
7
|
+
data.tar.gz: d40fd23bb85cb29df78ef030651c857465016109dcb42e88ef6e4a2fc64ca907454512ca5b938c9636ac7e9069815afbd293b5ccc9735f62be1949ac040248ac
|
data/lib/draw_static.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
require 'pry'
|
2
3
|
|
3
4
|
require_relative 'static_routes'
|
4
5
|
|
5
6
|
module DrawStatic
|
6
7
|
def draw_static(*arguments)
|
7
8
|
generator = StaticRoutes.new(context: self)
|
8
|
-
|
9
|
+
|
10
|
+
generator.for(arguments: arguments, prefixed_modules: prefixed_modules)
|
11
|
+
end
|
12
|
+
|
13
|
+
def prefixed_modules
|
14
|
+
instance_variable_get('@scope')&.instance_variable_get('@hash')&.try(:[], :module)
|
9
15
|
end
|
10
16
|
end
|
11
17
|
|
data/lib/static_routes.rb
CHANGED
@@ -1,46 +1,48 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
# get all seperate modules + controller
|
4
|
+
# join by ::
|
3
5
|
class StaticRoutes
|
4
|
-
attr_accessor :context, :limits
|
6
|
+
attr_accessor :context, :limits, :prefixed_modules
|
5
7
|
def initialize(attributes = {})
|
6
8
|
@context = attributes[:context]
|
7
9
|
@limits = {}
|
8
10
|
end
|
9
11
|
|
12
|
+
def for(attributes)
|
13
|
+
splice_limits(attributes[:arguments])
|
14
|
+
|
15
|
+
self.prefixed_modules = attributes[:prefixed_modules]&.split('/') || []
|
16
|
+
|
17
|
+
attributes[:arguments].each do |controller|
|
18
|
+
write_routes_for(controller)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
10
24
|
def route_from_action(action)
|
11
25
|
action.to_s.gsub(/[^a-zA-Z]/, '-')
|
12
26
|
end
|
13
27
|
|
14
28
|
def controller_from_chars(chars)
|
15
|
-
|
29
|
+
create_namespaced_controller(chars).constantize
|
16
30
|
end
|
17
31
|
|
18
|
-
def
|
19
|
-
|
20
|
-
if limits[:only]
|
21
|
-
limits[:only].select { |method| instance_methods.include?(method) }
|
22
|
-
else
|
23
|
-
instance_methods.reject do |route|
|
24
|
-
limits[:except]&.include?(route)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
32
|
+
def create_namespaced_controller(chars)
|
33
|
+
main_controller = "#{chars.to_s.camelize}Controller"
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
if prefixed_modules
|
36
|
+
(prefixed_modules.map(&:camelize) << main_controller).join('::')
|
37
|
+
else
|
38
|
+
main_controller
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
|
-
|
42
|
+
def splice_limits(arguments)
|
43
|
+
return unless arguments.last.is_a? Hash
|
37
44
|
|
38
|
-
|
39
|
-
if arguments.last.is_a? Hash
|
40
|
-
self.limits = arguments.last
|
41
|
-
return arguments[0..-2]
|
42
|
-
end
|
43
|
-
arguments
|
45
|
+
self.limits = arguments.slice!(-1)
|
44
46
|
end
|
45
47
|
|
46
48
|
def write_routes_for(controller)
|
@@ -49,4 +51,15 @@ class StaticRoutes
|
|
49
51
|
context.get(route_from_action(action), to: "#{controller}##{action}")
|
50
52
|
end
|
51
53
|
end
|
54
|
+
|
55
|
+
def desired_routes_from(controller)
|
56
|
+
instance_methods = controller.instance_methods(false)
|
57
|
+
if limits[:only]
|
58
|
+
limits[:only].select { |method| instance_methods.include?(method) }
|
59
|
+
else
|
60
|
+
instance_methods.reject do |route|
|
61
|
+
limits[:except]&.include?(route)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
52
65
|
end
|