trains 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/trains/dto/callback.rb +7 -0
- data/lib/trains/dto/controller.rb +1 -1
- data/lib/trains/utils/args.rb +43 -0
- data/lib/trains/version.rb +1 -1
- data/lib/trains/visitor/controller.rb +23 -3
- data/lib/trains/visitor/route.rb +6 -29
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b1af8d7a53681c21f561432534383da1d83bf4615bc8e85bdfa6c8ce7b210aa
|
4
|
+
data.tar.gz: aa4798c33b271c590c5b966078eb7a6f9fd603a069842ce454cadb14de8cc08d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6faddb254499e05f02a13def98dc7ab1f7df69d2ff694617cbd24410e2cb23266c7fd4c067cb2e4e2dd4fd7e7552f7d5ba79c995f2f951ff452150b8d800222
|
7
|
+
data.tar.gz: 95748dab8e2f16aed324d9c55a8d14c0eeec265361d49794bf7d81f21f6b42b8a25ba7426f719fea673efc982864ff49c745d3cdeb6888780b63a3bdf81038b9
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Trains
|
4
|
+
module Utils
|
5
|
+
# utility module to deal with parsing of arguments
|
6
|
+
module Args
|
7
|
+
def parse_args(node)
|
8
|
+
case node.type
|
9
|
+
when :hash
|
10
|
+
parse_hash(node)
|
11
|
+
else
|
12
|
+
parse_value(node)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse_hash(node)
|
17
|
+
options = {}
|
18
|
+
return options unless node.type == :hash
|
19
|
+
|
20
|
+
node.each_pair { |key, value| options[key.value] = parse_value(value) }
|
21
|
+
rescue StandardError => e
|
22
|
+
puts node.parent
|
23
|
+
ensure
|
24
|
+
return options
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_value(node)
|
28
|
+
case node.type
|
29
|
+
when :hash
|
30
|
+
parse_hash(node)
|
31
|
+
when :array
|
32
|
+
node.values.map { |value| parse_value(value) }
|
33
|
+
when :send
|
34
|
+
if node.method_name == :redirect
|
35
|
+
{ redirect: node.arguments.first.value }
|
36
|
+
end
|
37
|
+
else
|
38
|
+
node.value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/trains/version.rb
CHANGED
@@ -1,14 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../dto/callback'
|
4
|
+
require_relative '../dto/controller'
|
5
|
+
require_relative '../dto/method'
|
3
6
|
|
4
7
|
module Trains
|
5
8
|
module Visitor
|
6
9
|
# Visitor that parses controllers and returns a DTO::Controller object
|
7
10
|
class Controller < Base
|
11
|
+
include Utils::Args
|
12
|
+
|
13
|
+
TRACKED_CALLBACKS = [
|
14
|
+
:http_basic_authenticate_with
|
15
|
+
].freeze
|
16
|
+
|
8
17
|
def initialize
|
9
18
|
@method_list = []
|
10
19
|
@methods = {}
|
11
20
|
@class_name = nil
|
21
|
+
@callbacks = []
|
12
22
|
end
|
13
23
|
|
14
24
|
def on_class(node)
|
@@ -17,15 +27,25 @@ module Trains
|
|
17
27
|
return unless controller?(parent_class)
|
18
28
|
|
19
29
|
@class_name = class_name
|
30
|
+
find_callbacks(node)
|
20
31
|
parse_body(node.body) unless node.body.nil?
|
21
32
|
end
|
22
33
|
|
23
34
|
def result
|
24
|
-
DTO::Controller.new(name: @class_name, method_list: @method_list.uniq)
|
35
|
+
DTO::Controller.new(name: @class_name, method_list: @method_list.uniq, callbacks: @callbacks)
|
25
36
|
end
|
26
37
|
|
27
38
|
private
|
28
39
|
|
40
|
+
def find_callbacks(klass_node)
|
41
|
+
klass_node.each_descendant(:send) do |node|
|
42
|
+
if node.receiver.nil? && TRACKED_CALLBACKS.include?(node.method_name)
|
43
|
+
@callbacks << DTO::Callback.new(method: node.method_name,
|
44
|
+
arguments: node.arguments.map { |arg| parse_args(arg) })
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
29
49
|
def parse_body(body)
|
30
50
|
body.each_child_node do |child|
|
31
51
|
@method_list << parse_method(child) if child.type == :def
|
data/lib/trains/visitor/route.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative '../dto/route'
|
2
2
|
|
3
3
|
module Trains
|
4
4
|
module Visitor
|
5
|
+
# Visitor for Parsing Rails routes
|
5
6
|
class Route < Base
|
7
|
+
include Utils::Args
|
8
|
+
|
6
9
|
def_node_matcher :route_parent?, <<~PATTERN
|
7
10
|
(block (send (send (send
|
8
11
|
(const nil? :Rails) :application) :routes) :draw)
|
@@ -13,7 +16,7 @@ module Trains
|
|
13
16
|
(send nil? %1 ...)
|
14
17
|
PATTERN
|
15
18
|
|
16
|
-
ALLOWED_VERBS = %i[get put post update delete resources scope]
|
19
|
+
ALLOWED_VERBS = %i[get put post update delete resources scope].freeze
|
17
20
|
|
18
21
|
def initialize
|
19
22
|
@route_list = []
|
@@ -46,33 +49,7 @@ module Trains
|
|
46
49
|
node.arguments[0].value
|
47
50
|
end
|
48
51
|
options = parse_hash(node.arguments[1])
|
49
|
-
DTO::Route.new(method
|
50
|
-
end
|
51
|
-
|
52
|
-
def parse_hash(node)
|
53
|
-
options = {}
|
54
|
-
return options unless node.type == :hash
|
55
|
-
|
56
|
-
node.each_pair { |key, value| options[key.value] = parse_value(value) }
|
57
|
-
rescue StandardError => e
|
58
|
-
puts node.parent
|
59
|
-
ensure
|
60
|
-
return options
|
61
|
-
end
|
62
|
-
|
63
|
-
def parse_value(node)
|
64
|
-
case node.type
|
65
|
-
when :hash
|
66
|
-
parse_hash(node)
|
67
|
-
when :array
|
68
|
-
node.values.map { |value| parse_value(value) }
|
69
|
-
when :send
|
70
|
-
if node.method_name == :redirect
|
71
|
-
{ redirect: node.arguments.first.value }
|
72
|
-
end
|
73
|
-
else
|
74
|
-
node.value
|
75
|
-
end
|
52
|
+
DTO::Route.new(method:, param:, options:)
|
76
53
|
end
|
77
54
|
end
|
78
55
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trains
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Syed Faraaz Ahmad
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- Rakefile
|
86
86
|
- lib/trains.rb
|
87
87
|
- lib/trains/dto/app.rb
|
88
|
+
- lib/trains/dto/callback.rb
|
88
89
|
- lib/trains/dto/controller.rb
|
89
90
|
- lib/trains/dto/field.rb
|
90
91
|
- lib/trains/dto/method.rb
|
@@ -92,6 +93,7 @@ files:
|
|
92
93
|
- lib/trains/dto/model.rb
|
93
94
|
- lib/trains/dto/route.rb
|
94
95
|
- lib/trains/scanner.rb
|
96
|
+
- lib/trains/utils/args.rb
|
95
97
|
- lib/trains/utils/logger.rb
|
96
98
|
- lib/trains/utils/migration_tailor.rb
|
97
99
|
- lib/trains/utils/rails_dir.rb
|