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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b5ecbdc7264798f35f7eacbf0643a325aee0f3f48fae7f598c0f970daad547e
4
- data.tar.gz: 3af6890ffca08d81d561aa675b3980e8c00f5a0ec9efbbf26b6d24337f7c5a78
3
+ metadata.gz: ade5c4f57646f2b8460fcc508d3ca7f864215746aeba29705bf29f82a66436ea
4
+ data.tar.gz: 2accd3ae9f2aafec9f7f00699e703a92d2162b76f263c2cbcafa6b40db0c072f
5
5
  SHA512:
6
- metadata.gz: 1b72a0c3b99c459009c7ee1546263a19483c9d18d0a708bdc7349200339e302a4ec5a572570a54a76782a2928ffd6faed8a76a96150a0ff69be1232253afb09c
7
- data.tar.gz: a75abdb394ff578571798179491d8ecc50733aee7f05bb1d51d8a83c03695928343fea0db328e910400a972f0ad6deb3e09015e5d5d7d8f045f4981e2ffd831f
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
- generator.for(arguments)
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
- "#{chars.to_s.camelize}Controller".constantize
29
+ create_namespaced_controller(chars).constantize
16
30
  end
17
31
 
18
- def desired_routes_from(controller)
19
- instance_methods = controller.instance_methods(false)
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
- def for(arguments)
30
- controllers = split_controllers_and_limits(arguments)
31
- controllers.each do |controller|
32
- write_routes_for(controller)
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
- private
42
+ def splice_limits(arguments)
43
+ return unless arguments.last.is_a? Hash
37
44
 
38
- def split_controllers_and_limits(arguments)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draw_static
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Inou Ridder