chusaku 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +4 -0
- data/.rubocop.yml +13 -19
- data/README.md +17 -5
- data/chusaku.gemspec +2 -0
- data/lib/chusaku.rb +103 -86
- data/lib/chusaku/cli.rb +77 -48
- data/lib/chusaku/parser.rb +29 -20
- data/lib/chusaku/routes.rb +50 -37
- data/lib/chusaku/version.rb +1 -1
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf3fc6d454cdeaf9c8c31019c1b0ec6dc4c676203a8c44cd40e24dee49ace832
|
4
|
+
data.tar.gz: ba06bb7b2868c6c2d53ac93504eb00fe0ef5527b1224f04c6c862fd27e7734fe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b932a1b72743a06701d7fa3d7a80068baf0e31df76d78a0dea7b892a46bb663c3dcd9e444599b3942a9a5eb3289b5b90b962ef39216e8f15ff04bde5bff0946b
|
7
|
+
data.tar.gz: c609bb26e27dc4018766e84d55aa38715385bff1fc444468481ff781649f9af3037e2078ada729814f9d92963e9b3752f48b1bb6cbdbe05a00000eae5ea169b4
|
data/.codeclimate.yml
ADDED
data/.rubocop.yml
CHANGED
@@ -1,27 +1,21 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
|
1
4
|
AllCops:
|
2
5
|
Exclude:
|
3
6
|
- 'bin/**/*'
|
4
|
-
|
5
|
-
|
6
|
-
EnforcedStyle: rails
|
7
|
+
- 'test/mock/app/**/*'
|
8
|
+
- 'test/mock/examples/**/*'
|
7
9
|
|
8
10
|
Metrics/AbcSize:
|
9
|
-
|
11
|
+
Exclude:
|
12
|
+
- 'test/**/*'
|
10
13
|
|
11
14
|
Metrics/MethodLength:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
Enabled: false
|
16
|
-
|
17
|
-
Style/ClassVars:
|
18
|
-
Enabled: false
|
19
|
-
|
20
|
-
Style/CommentedKeyword:
|
21
|
-
Enabled: false
|
22
|
-
|
23
|
-
Style/Documentation:
|
24
|
-
Enabled: false
|
15
|
+
Max: 25
|
16
|
+
Exclude:
|
17
|
+
- 'test/**/*'
|
25
18
|
|
26
|
-
Style/
|
27
|
-
|
19
|
+
Style/ClassAndModuleChildren:
|
20
|
+
Exclude:
|
21
|
+
- 'test/**/*'
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Chusaku
|
2
2
|
|
3
|
-
|
|
4
|
-
|
5
|
-
|[![CircleCI](https://circleci.com/gh/nshki/chusaku.svg?style=svg&circle-token=e1917972632f242932171de0ca5443148e83151c)](https://circleci.com/gh/nshki/chusaku)|[![
|
3
|
+
| Gem | CI | Quality |
|
4
|
+
|-----|----|---------|
|
5
|
+
|[![Gem Version](https://badge.fury.io/rb/chusaku.svg)](https://badge.fury.io/rb/chusaku)|[![CircleCI](https://circleci.com/gh/nshki/chusaku.svg?style=svg&circle-token=e1917972632f242932171de0ca5443148e83151c)](https://circleci.com/gh/nshki/chusaku)|[![Maintainability](https://api.codeclimate.com/v1/badges/e21235bd513aadf0407b/maintainability)](https://codeclimate.com/github/nshki/chusaku/maintainability)
|
6
6
|
|
7
7
|
Add comments above your Rails actions that look like:
|
8
8
|
|
@@ -61,8 +61,20 @@ Usage: chusaku [options]
|
|
61
61
|
-h, --help Show this help message and quit
|
62
62
|
```
|
63
63
|
|
64
|
-
|
65
|
-
|
64
|
+
|
65
|
+
## Pre-commit Hook
|
66
|
+
|
67
|
+
Here's an example setup that you could use for automating Chusaku as a Git hook
|
68
|
+
with the [Lefthook](https://github.com/Arkweid/lefthook) gem.
|
69
|
+
|
70
|
+
```yaml
|
71
|
+
pre-commit:
|
72
|
+
commands:
|
73
|
+
chusaku:
|
74
|
+
run: eval "! git diff --staged --name-only | grep -q 'routes.rb' && exit 0 || bundle exec chusaku --exit-with-error-on-annotation"
|
75
|
+
```
|
76
|
+
|
77
|
+
This example config only runs Chusaku if `routes.rb` was modified.
|
66
78
|
|
67
79
|
|
68
80
|
## Development
|
data/chusaku.gemspec
CHANGED
@@ -44,6 +44,8 @@ Gem::Specification.new do |spec|
|
|
44
44
|
spec.add_development_dependency 'bundler', '~> 2.0'
|
45
45
|
spec.add_development_dependency 'minitest', '~> 5.0'
|
46
46
|
spec.add_development_dependency 'rake', '~> 10.0'
|
47
|
+
spec.add_development_dependency 'rubocop', '~> 0.77'
|
48
|
+
spec.add_development_dependency 'rubocop-performance', '~> 1.5'
|
47
49
|
|
48
50
|
spec.add_dependency 'rails', '> 2.0'
|
49
51
|
end
|
data/lib/chusaku.rb
CHANGED
@@ -4,106 +4,123 @@ require 'chusaku/version'
|
|
4
4
|
require 'chusaku/parser'
|
5
5
|
require 'chusaku/routes'
|
6
6
|
|
7
|
+
# Handles core functionality of annotating projects.
|
7
8
|
module Chusaku
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
if prev[:type] == :comment
|
36
|
-
prev[:body] = prev[:body].gsub(/^\s*#\s*@route.*$\n/, '')
|
37
|
-
end
|
9
|
+
class << self
|
10
|
+
# The main method to run Chusaku. Annotate all actions in a Rails project as
|
11
|
+
# follows:
|
12
|
+
#
|
13
|
+
# # @route GET /waterlilies/:id (waterlilies)
|
14
|
+
# def show
|
15
|
+
# # ...
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# @param {Hash} flags - CLI flags
|
19
|
+
# @return {Integer} - 0 on success, 1 on error
|
20
|
+
def call(flags = {})
|
21
|
+
@flags = flags
|
22
|
+
@routes = Chusaku::Routes.call
|
23
|
+
@annotated_paths = []
|
24
|
+
controllers_pattern = 'app/controllers/**/*_controller.rb'
|
25
|
+
|
26
|
+
Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
|
27
|
+
controller = %r{controllers\/(.*)_controller\.rb}.match(path)[1]
|
28
|
+
actions = @routes[controller]
|
29
|
+
next if actions.nil?
|
30
|
+
|
31
|
+
annotate_file(path: path, controller: controller, actions: actions.keys)
|
32
|
+
end
|
33
|
+
|
34
|
+
output_results
|
35
|
+
end
|
38
36
|
|
39
|
-
|
37
|
+
private
|
38
|
+
|
39
|
+
# Adds annotations to the given file.
|
40
|
+
#
|
41
|
+
# @param {String} path - Path to file
|
42
|
+
# @param {String} controller - Controller name
|
43
|
+
# @param {Array<String>} actions - List of valid actions for the controller
|
44
|
+
# @return {void}
|
45
|
+
def annotate_file(path:, controller:, actions:)
|
46
|
+
parsed_file = Chusaku::Parser.call(path: path, actions: actions)
|
47
|
+
parsed_file[:groups].each_cons(2) do |prev, curr|
|
48
|
+
clean_group(prev)
|
40
49
|
next unless curr[:type] == :action
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
# Add annotations.
|
48
|
-
whitespace = /^(\s*).*$/.match(curr[:body])[1]
|
49
|
-
data.reverse.each do |datum|
|
50
|
-
annotation = annotate(datum)
|
51
|
-
comment = "#{whitespace}# #{annotation}\n"
|
52
|
-
curr[:body] = comment + curr[:body]
|
53
|
-
end
|
51
|
+
route_data = @routes[controller][curr[:action]]
|
52
|
+
next unless route_data.any?
|
53
|
+
|
54
|
+
annotate_group(group: curr, route_data: route_data)
|
54
55
|
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
57
|
+
write_to_file(path: path, parsed_file: parsed_file)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Given a parsed group, clean out its contents.
|
61
|
+
#
|
62
|
+
# @param {Hash} group - { type: Symbol, body: String }
|
63
|
+
# @return {void}
|
64
|
+
def clean_group(group)
|
65
|
+
return unless group[:type] == :comment
|
66
|
+
|
67
|
+
group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, '')
|
68
|
+
group[:body] =
|
69
|
+
group[:body].gsub(%r{^\s*# (GET|POST|PATCH\/PUT|DELETE) \/\S+$\n}, '')
|
70
|
+
end
|
71
|
+
|
72
|
+
# Add an annotation to the given group given by Chusaku::Parser that looks
|
73
|
+
# like:
|
74
|
+
#
|
75
|
+
# @route GET /waterlilies/:id (waterlilies)
|
76
|
+
#
|
77
|
+
# @param {Hash} group - Parsed content given by Chusaku::Parser
|
78
|
+
# @param {Hash} route_data - Individual route data given by Chusaku::Routes
|
79
|
+
# @return {void}
|
80
|
+
def annotate_group(group:, route_data:)
|
81
|
+
whitespace = /^(\s*).*$/.match(group[:body])[1]
|
82
|
+
route_data.reverse_each do |datum|
|
83
|
+
name = datum[:name]
|
84
|
+
annotation = "@route #{datum[:verb]} #{datum[:path]}"
|
85
|
+
annotation += " (#{name})" unless name.nil?
|
86
|
+
comment = "#{whitespace}# #{annotation}\n"
|
87
|
+
group[:body] = comment + group[:body]
|
62
88
|
end
|
63
89
|
end
|
64
90
|
|
65
|
-
#
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
91
|
+
# Write annotated content to a file if it differs from the original.
|
92
|
+
#
|
93
|
+
# @param {String} path - File path to write to
|
94
|
+
# @param {Hash} parsed_file - Hash mutated by `annotate_group`
|
95
|
+
# @return {void}
|
96
|
+
def write_to_file(path:, parsed_file:)
|
97
|
+
content = parsed_file[:groups].map { |pf| pf[:body] }.join
|
98
|
+
return unless parsed_file[:content] != content
|
99
|
+
|
100
|
+
unless @flags.include?(:dry)
|
101
|
+
File.open(path, 'r+') do |file|
|
102
|
+
if file.respond_to?(:test_write)
|
103
|
+
file.test_write(content, path)
|
104
|
+
else
|
105
|
+
file.write(content)
|
106
|
+
end
|
107
|
+
end
|
72
108
|
end
|
73
|
-
|
74
|
-
|
75
|
-
0
|
109
|
+
|
110
|
+
@annotated_paths.push(path)
|
76
111
|
end
|
77
|
-
end
|
78
112
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
File.open(path, 'r+') do |file|
|
87
|
-
if file.respond_to?(:test_write)
|
88
|
-
file.test_write(content, path)
|
113
|
+
# Output results to user.
|
114
|
+
#
|
115
|
+
# @return {Integer} - 0 for success, 1 for error
|
116
|
+
def output_results
|
117
|
+
if @annotated_paths.any?
|
118
|
+
puts("Annotated #{@annotated_paths.join(', ')}")
|
119
|
+
@flags.include?(:error_on_annotation) ? 1 : 0
|
89
120
|
else
|
90
|
-
|
121
|
+
puts('Nothing to annotate')
|
122
|
+
0
|
91
123
|
end
|
92
124
|
end
|
93
125
|
end
|
94
|
-
|
95
|
-
# Given a hash describing an action, generate an annotation in the form:
|
96
|
-
#
|
97
|
-
# @route GET /waterlilies/:id (waterlilies)
|
98
|
-
#
|
99
|
-
# @param {Hash} action_info - Parsed line given by Chusaku::Parser
|
100
|
-
# @return {String} Annotation for given parsed line
|
101
|
-
def self.annotate(action_info)
|
102
|
-
verb = action_info[:verb]
|
103
|
-
path = action_info[:path]
|
104
|
-
name = action_info[:name]
|
105
|
-
annotation = "@route #{verb} #{path}"
|
106
|
-
annotation += " (#{name})" unless name.nil?
|
107
|
-
annotation
|
108
|
-
end
|
109
126
|
end
|
data/lib/chusaku/cli.rb
CHANGED
@@ -4,18 +4,19 @@ require 'optparse'
|
|
4
4
|
require 'chusaku'
|
5
5
|
|
6
6
|
module Chusaku
|
7
|
+
# Enables flags for the `chusaku` executable.
|
7
8
|
class CLI
|
9
|
+
attr_reader :options
|
10
|
+
|
8
11
|
STATUS_SUCCESS = 0
|
9
12
|
STATUS_ERROR = 1
|
10
13
|
|
11
|
-
attr_reader :options
|
12
|
-
|
13
14
|
Finished = Class.new(RuntimeError)
|
14
15
|
NotARailsProject = Class.new(RuntimeError)
|
15
16
|
|
16
17
|
# Initializes a new instance of Chusaku::CLI.
|
17
18
|
#
|
18
|
-
# @return {Chusaku::CLI} Instance of this class
|
19
|
+
# @return {Chusaku::CLI} - Instance of this class
|
19
20
|
def initialize
|
20
21
|
@options = {}
|
21
22
|
end
|
@@ -23,7 +24,7 @@ module Chusaku
|
|
23
24
|
# Parse CLI flags, if any, and handle applicable behaviors.
|
24
25
|
#
|
25
26
|
# @param {Array<String>} args - CLI arguments
|
26
|
-
# @return {Integer} 0 on success, 1 on error
|
27
|
+
# @return {Integer} - 0 on success, 1 on error
|
27
28
|
def call(args = ARGV)
|
28
29
|
optparser.parse!(args)
|
29
30
|
check_for_rails_project
|
@@ -37,55 +38,83 @@ module Chusaku
|
|
37
38
|
|
38
39
|
private
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
41
|
+
# Raises exception if Rails project cannot be detected.
|
42
|
+
#
|
43
|
+
# @raise {Chusaku::CLI::NotARailsProject} - Exception if not Rails project
|
44
|
+
# @return {void}
|
45
|
+
def check_for_rails_project
|
46
|
+
has_controllers = File.directory?('./app/controllers')
|
47
|
+
has_rakefile = File.exist?('./Rakefile')
|
48
|
+
raise NotARailsProject unless has_controllers && has_rakefile
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
# Returns an instance of OptionParser with supported flags.
|
52
|
+
#
|
53
|
+
# @return {OptionParser} - Preconfigured OptionParser instance
|
54
|
+
def optparser
|
55
|
+
OptionParser.new do |opts|
|
56
|
+
opts.banner = 'Usage: chusaku [options]'
|
57
|
+
add_error_on_annotation_flag(opts)
|
58
|
+
add_dry_run_flag(opts)
|
59
|
+
add_version_flag(opts)
|
60
|
+
add_help_flag(opts)
|
61
|
+
end
|
62
|
+
end
|
56
63
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
64
|
+
# Adds `--exit-with-error-on-annotation` flag.
|
65
|
+
#
|
66
|
+
# @param {OptionParser} opts - OptionParser instance
|
67
|
+
# @return {void}
|
68
|
+
def add_error_on_annotation_flag(opts)
|
69
|
+
opts.on(
|
70
|
+
'--exit-with-error-on-annotation',
|
71
|
+
'Fail if any file was annotated'
|
72
|
+
) do
|
73
|
+
@options[:error_on_annotation] = true
|
74
|
+
end
|
75
|
+
end
|
63
76
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
77
|
+
# Adds `--dry-run` flag.
|
78
|
+
#
|
79
|
+
# @param {OptionParser} opts - OptionParser instance
|
80
|
+
# @return {void}
|
81
|
+
def add_dry_run_flag(opts)
|
82
|
+
opts.on(
|
83
|
+
'--dry-run',
|
84
|
+
'Run without file modifications'
|
85
|
+
) do
|
86
|
+
@options[:dry] = true
|
87
|
+
end
|
88
|
+
end
|
70
89
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
90
|
+
# Adds `--version` flag.
|
91
|
+
#
|
92
|
+
# @param {OptionParser} opts - OptionParser instance
|
93
|
+
# @return {void}
|
94
|
+
def add_version_flag(opts)
|
95
|
+
opts.on(
|
96
|
+
'-v',
|
97
|
+
'--version',
|
98
|
+
'Show Chusaku version number and quit'
|
99
|
+
) do
|
100
|
+
puts(Chusaku::VERSION)
|
101
|
+
raise Finished
|
102
|
+
end
|
103
|
+
end
|
79
104
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
105
|
+
# Adds `--help` flag.
|
106
|
+
#
|
107
|
+
# @param {OptionParser} opts - OptionParser instance
|
108
|
+
# @return {void}
|
109
|
+
def add_help_flag(opts)
|
110
|
+
opts.on(
|
111
|
+
'-h',
|
112
|
+
'--help',
|
113
|
+
'Show this help message and quit'
|
114
|
+
) do
|
115
|
+
puts(opts)
|
116
|
+
raise Finished
|
89
117
|
end
|
118
|
+
end
|
90
119
|
end
|
91
120
|
end
|
data/lib/chusaku/parser.rb
CHANGED
@@ -1,27 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Chusaku
|
4
|
+
# Handles parsing a file and groups its lines into categories.
|
4
5
|
module Parser
|
5
|
-
# Parses a file and groups its lines into categories:
|
6
|
-
#
|
7
6
|
# Example output:
|
8
7
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
8
|
+
# {
|
9
|
+
# content: <Original file content>,
|
10
|
+
# groups: [
|
11
|
+
# {
|
12
|
+
# type: :code,
|
13
|
+
# body: 'class Foo\n',
|
14
|
+
# action: nil
|
15
|
+
# },
|
16
|
+
# {
|
17
|
+
# type: :comment,
|
18
|
+
# body: ' # Bar\n # Baz\n',
|
19
|
+
# action: nil
|
20
|
+
# },
|
21
|
+
# {
|
22
|
+
# type: :action,
|
23
|
+
# body: ' def action_name; end\n',
|
24
|
+
# action: 'action_name'
|
25
|
+
# }
|
26
|
+
# {
|
27
|
+
# type: :code,
|
28
|
+
# body: 'end # vanilla is the best flavor\n',
|
29
|
+
# action: nil
|
30
|
+
# }
|
31
|
+
# ]
|
32
|
+
# }
|
21
33
|
#
|
22
34
|
# @param {String} path - File path to parse
|
23
35
|
# @param {Array<String>} actions - List of valid actions for this route
|
24
|
-
# @return {Hash}
|
36
|
+
# @return {Hash} - { content: String, groups: Array<Hash> }
|
25
37
|
def self.call(path:, actions:)
|
26
38
|
groups = []
|
27
39
|
group = {}
|
@@ -43,10 +55,7 @@ module Chusaku
|
|
43
55
|
|
44
56
|
# Push the last group onto the array and return.
|
45
57
|
groups.push(group)
|
46
|
-
{
|
47
|
-
content: content,
|
48
|
-
groups: groups
|
49
|
-
}
|
58
|
+
{ content: content, groups: groups }
|
50
59
|
end
|
51
60
|
|
52
61
|
# Given a line and actions, returns the line's type:
|
@@ -62,7 +71,7 @@ module Chusaku
|
|
62
71
|
#
|
63
72
|
# @param {String} line - A line of a file
|
64
73
|
# @param {Array<String>} actions - List of valid actions for this route
|
65
|
-
# @return {Hash}
|
74
|
+
# @return {Hash} - { type: Symbol, body: String, action: String }
|
66
75
|
def self.parse_line(line:, actions:)
|
67
76
|
comment_match = /^\s*#.*$/.match(line)
|
68
77
|
def_match = /^\s*def\s+(\w*)\s*\w*.*$/.match(line)
|
data/lib/chusaku/routes.rb
CHANGED
@@ -1,49 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Chusaku
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
4
|
+
# Handles extracting information about the Rails project's routes.
|
5
|
+
class Routes
|
6
|
+
class << self
|
7
|
+
# Example output:
|
8
|
+
#
|
9
|
+
# {
|
10
|
+
# 'users' => {
|
11
|
+
# 'edit' => [
|
12
|
+
# { verb: 'GET', path: '/users/:id', name: 'edit_user' }
|
13
|
+
# ],
|
14
|
+
# 'update' => [
|
15
|
+
# { verb: 'PATCH', path: '/users', name: 'edit_user' },
|
16
|
+
# { verb: 'PUT', path: '/users', name: 'edit_user' }
|
17
|
+
# ]
|
18
|
+
# },
|
19
|
+
# 'empanadas' => {
|
20
|
+
# 'create' => [
|
21
|
+
# { verb: 'POST', path: '/empanadas', name: nil }
|
22
|
+
# ]
|
23
|
+
# }
|
24
|
+
# }
|
25
|
+
#
|
26
|
+
# @return {Hash} - Routes hash
|
27
|
+
def call
|
28
|
+
routes = {}
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
Rails.application.routes.routes.each do |route|
|
31
|
+
controller, action = extract_controller_and_action_from(route)
|
32
|
+
routes[controller] ||= {}
|
33
|
+
routes[controller][action] ||= []
|
34
|
+
routes[controller][action].push(format_route(route))
|
35
|
+
end
|
31
36
|
|
32
|
-
routes
|
33
|
-
routes[controller][action] ||= []
|
34
|
-
routes[controller][action].push(format_route(route))
|
37
|
+
backfill_routes(routes)
|
35
38
|
end
|
36
39
|
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
40
|
+
private
|
41
41
|
|
42
42
|
# Extract information of a given route.
|
43
43
|
#
|
44
44
|
# @param {ActionDispatch::Journey::Route} route - Route given by Rails
|
45
|
-
# @return {Hash}
|
46
|
-
def
|
45
|
+
# @return {Hash} - { verb: String, path: String, name: String }
|
46
|
+
def format_route(route)
|
47
47
|
{
|
48
48
|
verb: route.verb,
|
49
49
|
path: route.path.spec.to_s.gsub('(.:format)', ''),
|
@@ -55,8 +55,8 @@ module Chusaku
|
|
55
55
|
# `Rails.application.routes`.
|
56
56
|
#
|
57
57
|
# @param {Hash} routes - Routes hash generated by this class
|
58
|
-
# @return {Hash} Backfilled routes
|
59
|
-
def
|
58
|
+
# @return {Hash} - Backfilled routes hash
|
59
|
+
def backfill_routes(routes)
|
60
60
|
paths = {}
|
61
61
|
|
62
62
|
routes.each do |_controller, actions|
|
@@ -70,5 +70,18 @@ module Chusaku
|
|
70
70
|
|
71
71
|
routes
|
72
72
|
end
|
73
|
+
|
74
|
+
# Given a route, extract the controller and action strings.
|
75
|
+
#
|
76
|
+
# @param {ActionDispatch::Journey::Route} route - Route instance
|
77
|
+
# @return {Array<String>} - [String, String]
|
78
|
+
def extract_controller_and_action_from(route)
|
79
|
+
defaults = route.defaults
|
80
|
+
controller = defaults[:controller]
|
81
|
+
action = defaults[:action]
|
82
|
+
|
83
|
+
[controller, action]
|
84
|
+
end
|
85
|
+
end
|
73
86
|
end
|
74
87
|
end
|
data/lib/chusaku/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chusaku
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nishiki Liu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.77'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.77'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-performance
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.5'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: rails
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,6 +103,7 @@ extensions: []
|
|
75
103
|
extra_rdoc_files: []
|
76
104
|
files:
|
77
105
|
- ".circleci/config.yml"
|
106
|
+
- ".codeclimate.yml"
|
78
107
|
- ".gitignore"
|
79
108
|
- ".rubocop.yml"
|
80
109
|
- Gemfile
|