chusaku 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7fd38001d891049886a95d54019ce4636dee3762e19f50e83bf7b0d53732cd6
4
- data.tar.gz: a55e1bd009ad426e9258a6d6098d029d45bfb043a6736d9dda492a64b83d6ddf
3
+ metadata.gz: 519cb59d3970def8c48a1f9995f8208e2455d17c6f3061ff56ceed1e9cd2e593
4
+ data.tar.gz: 4de2d9aae8d0e9d97d998a53b47fd4428855ed7f5898926ae3e1f14d1b6fe90e
5
5
  SHA512:
6
- metadata.gz: f135d7d2979a67bf3b9f883f5775511a78088b9c8468676da7d028551d4bfbc80ba89b44c182c88da98bd3adbed2deed425a97f2d5fe7b46fe6ebdbac85dc966
7
- data.tar.gz: 7efc39dbd3e86b1f4672900535d1f3556b907d7c6f5220a61182a4aa3b44685a333d41c67fa78fc923e41df33b39ba49bc520743a3e9b5186179b86eb78cdbf7
6
+ metadata.gz: f619946e1d2a43aacec4d8bd2488d5b5af4c4e24f64bc233c294097ffc51e2c4bf3c3028530302397c026d099835d2587df1cc96356643102812bf93754a8265
7
+ data.tar.gz: 871f3aab82d32b5dff9dd508fbc8119ace1125cc88e14f0e36489b094228e3843bd47fa418a0314017fe974c5ee1a149fd86585f47f494e888de92e82fa3da54
data/README.md CHANGED
@@ -7,10 +7,16 @@
7
7
  Add comments above your Rails actions that look like:
8
8
 
9
9
  ```ruby
10
- # @route [GET] /waterlilies/:id (waterlily)
10
+ # @route GET /waterlilies/:id (waterlily)
11
11
  def show
12
12
  # ...
13
13
  end
14
+
15
+ # @route PATCH /waterlilies/:id (waterlily)
16
+ # @route PUT /waterlilies/:id (waterlily)
17
+ def update
18
+ # ...
19
+ end
14
20
  ```
15
21
 
16
22
  Based on your `routes.rb` file!
@@ -46,16 +52,14 @@ $ bundle exec chusaku
46
52
 
47
53
  ## Development
48
54
 
49
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
-
51
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
-
53
-
54
- ## Contributing
55
-
56
- Bug reports and pull requests are welcome on GitHub at https://github.com/nshki/chusaku.
57
-
55
+ Read the blog post explaining how the gem works at a high level:
56
+ https://nshki.com/chusaku-a-controller-annotation-gem/.
58
57
 
59
- ## License
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
59
+ `bundle exec rake test` to run the tests. You can also run `bin/console` for an
60
+ interactive prompt that will allow you to experiment.
60
61
 
61
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+ To release a new version, update the version number in `version.rb`, and then
63
+ run `bundle exec rake release`, which will create a git tag for the version,
64
+ git commits and tags, and push the `.gem` file to
65
+ [rubygems.org](https://rubygems.org).
@@ -45,6 +45,5 @@ Gem::Specification.new do |spec|
45
45
  spec.add_development_dependency 'minitest', '~> 5.0'
46
46
  spec.add_development_dependency 'rake', '~> 10.0'
47
47
 
48
- spec.add_dependency 'rails', '> 4.2'
49
- spec.add_dependency 'ruby-progressbar', '~> 1.10.1'
48
+ spec.add_dependency 'rails', '> 2.0'
50
49
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ruby-progressbar'
4
3
  require 'chusaku/version'
5
4
  require 'chusaku/parser'
6
5
  require 'chusaku/routes'
@@ -9,7 +8,7 @@ module Chusaku
9
8
  # The main method to run Chusaku. Annotate all actions in your Rails project
10
9
  # as follows:
11
10
  #
12
- # # @route [GET] /waterlilies/:id (waterlilies)
11
+ # # @route GET /waterlilies/:id (waterlilies)
13
12
  # def show
14
13
  # # ...
15
14
  # end
@@ -18,14 +17,8 @@ module Chusaku
18
17
  controller_pattern = 'app/controllers/**/*_controller.rb'
19
18
  controller_paths = Dir.glob(Rails.root.join(controller_pattern))
20
19
 
21
- # Start progress bar.
22
- progressbar = ProgressBar.create \
23
- title: 'Chusaku',
24
- total: controller_paths.count
25
-
26
20
  # Loop over all controller file paths.
27
21
  controller_paths.each do |path|
28
- progressbar.increment
29
22
  controller = /controllers\/(.*)_controller\.rb/.match(path)[1]
30
23
  actions = routes[controller]
31
24
  next if actions.nil?
@@ -42,18 +35,31 @@ module Chusaku
42
35
  # Only proceed if we are currently looking at an action.
43
36
  next unless curr[:type] == :action
44
37
 
45
- # Insert annotation comment.
38
+ # Fetch current action in routes.
46
39
  action = curr[:action]
47
- annotation = annotate(routes[controller][action])
40
+ data = routes[controller][action]
41
+ next unless data.any?
42
+
43
+ # Add annotations.
48
44
  whitespace = /^(\s*).*$/.match(curr[:body])[1]
49
- comment = "#{whitespace}# #{annotation}\n"
50
- curr[:body] = comment + curr[:body]
45
+ data.reverse.each do |datum|
46
+ annotation = annotate(datum)
47
+ comment = "#{whitespace}# #{annotation}\n"
48
+ curr[:body] = comment + curr[:body]
49
+ end
51
50
  end
52
51
 
53
52
  # Write to file.
54
53
  parsed_content = parsed_file.map { |pf| pf[:body] }
55
54
  write(path, parsed_content.join)
56
55
  end
56
+
57
+ # Output results to user.
58
+ if controller_paths.any?
59
+ puts "Annotated #{controller_paths.join(', ')}"
60
+ else
61
+ puts "Nothing to annotate"
62
+ end
57
63
  end
58
64
 
59
65
  # Write given content to a file. If we're using an overridden version of File,
@@ -74,15 +80,15 @@ module Chusaku
74
80
 
75
81
  # Given a hash describing an action, generate an annotation in the form:
76
82
  #
77
- # @route [GET] /waterlilies/:id (waterlilies)
83
+ # @route GET /waterlilies/:id (waterlilies)
78
84
  #
79
85
  # @param {Hash} action_info
80
86
  # @return {String}
81
87
  def self.annotate(action_info)
82
- verbs = action_info[:verbs]
88
+ verb = action_info[:verb]
83
89
  path = action_info[:path]
84
90
  name = action_info[:name]
85
- annotation = "@route [#{verbs.join(', ')}] #{path}"
91
+ annotation = "@route #{verb} #{path}"
86
92
  annotation += " (#{name})" unless name.nil?
87
93
  annotation
88
94
  end
@@ -8,27 +8,20 @@ module Chusaku
8
8
  #
9
9
  # {
10
10
  # 'users' => {
11
- # 'edit' => {
12
- # verbs: ['GET'],
13
- # path: '/users/:id',
14
- # name: 'edit_user'
15
- # },
16
- # 'update' => {
17
- # verbs: ['PUT', 'PATCH'],
18
- # path: '/users',
19
- # name: nil
20
- # }
11
+ # 'edit' => [
12
+ # { verb: ['GET'], path: '/users/:id', name: ['edit_user'] }],
13
+ # 'update' => [
14
+ # { verb: ['PUT', 'PATCH'],
15
+ # path: '/users',
16
+ # name: ['edit_user', 'edit_user2'] }]
21
17
  # },
22
18
  # 'empanadas' => {
23
- # 'create' => {
24
- # verbs: ['POST'],
25
- # path: '/empanadas',
26
- # name: nil
27
- # }
19
+ # 'create' => [
20
+ # { verb: 'POST', path: '/empanadas', name: nil }]
28
21
  # }
29
22
  # }
30
23
  #
31
- # @return {Hash}
24
+ # @return {Hash} Routes hash
32
25
  def self.call
33
26
  routes = {}
34
27
 
@@ -38,28 +31,45 @@ module Chusaku
38
31
  action = defaults[:action]
39
32
 
40
33
  routes[controller] ||= {}
41
- if routes[controller][action].nil?
42
- routes[controller][action] = format_action(route)
43
- else
44
- routes[controller][action][:verbs].push(route.verb)
45
- end
34
+ routes[controller][action] ||= []
35
+ routes[controller][action].push(format_route(route))
46
36
  end
47
37
 
48
- routes
38
+ backfill_routes(routes)
49
39
  end
50
40
 
51
41
  private
52
42
 
53
43
  # Extract information of a given route.
54
44
  #
55
- # @param {ActionDispatch::Journey::Route} route
56
- # @return {Hash}
57
- def self.format_action(route)
45
+ # @param {ActionDispatch::Journey::Route} route - Route given by Rails
46
+ # @return {Hash} Formatted hash for given route
47
+ def self.format_route(route)
58
48
  {
59
- verbs: [route.verb],
49
+ verb: route.verb,
60
50
  path: route.path.spec.to_s.gsub('(.:format)', ''),
61
51
  name: route.name
62
52
  }
63
53
  end
54
+
55
+ # Given a routes hash, backfill entries that aren't already filled by
56
+ # `Rails.application.routes`.
57
+ #
58
+ # @param {Hash} routes - Routes hash generated by this class
59
+ # @return {Hash} Backfilled routes
60
+ def self.backfill_routes(routes)
61
+ paths = {}
62
+
63
+ routes.each do |_controller, actions|
64
+ actions.each do |action, data|
65
+ data.each do |datum|
66
+ paths[datum[:path]] ||= datum[:name]
67
+ datum[:name] ||= paths[datum[:path]]
68
+ end
69
+ end
70
+ end
71
+
72
+ routes
73
+ end
64
74
  end
65
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chusaku
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
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.1.3
4
+ version: 0.1.4
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-07-13 00:00:00.000000000 Z
11
+ date: 2019-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,28 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">"
60
60
  - !ruby/object:Gem::Version
61
- version: '4.2'
61
+ version: '2.0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">"
67
67
  - !ruby/object:Gem::Version
68
- version: '4.2'
69
- - !ruby/object:Gem::Dependency
70
- name: ruby-progressbar
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: 1.10.1
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: 1.10.1
68
+ version: '2.0'
83
69
  description: Annotate your Rails controllers with route info.
84
70
  email:
85
71
  - nishiki.liu@gmail.com