nshki / chusaku

Annotate your Rails controllers with route info.
https://rubygems.org/gems/chusaku
MIT License
87 stars 4 forks source link

Modularize and fix edge cases #3

Closed nshki closed 5 years ago

nshki commented 5 years ago

This PR introduces two new service objects.

Chusaku::Routes

This module consumes the routes info available through Rails and spits out a consumable version of it.

Example output:

{
  'users' => {
    'edit'   => { verb: 'GET', path: '/users/:id', name: 'edit_user' },
    'update' => { verb: 'PUT', path: '/users',     name: nil }
  },
  'empanadas' => {
    'create' => { verb: 'POST', path: '/empanadas', name: nil }
  }
}

Chusaku::Parser

This module parses a file and groups its lines into categories.

Example input:

class Foo
  # Bar
  # Baz
  def action_name; end
end # vanilla is the best flavor

Example output:

[ { type: :code,
    body: 'class Foo\n',
    action: nil },
  { type: :comment,
    body: '  # Bar\n  # Baz\n',
    action: nil },
  { type: :action,
    body: '  def action_name; end\n',
    action: 'action_name' }
  { type: :code,
    body: 'end # vanilla is the best flavor\n',
    action: nil } ]

Edge Cases

I wanted to account for a few different scenarios:

  1. Duplicate @route comments (e.g. re-running bundle exec chusaku).
  2. @route comments that should be deleted because of updated routes.
  3. Stray and invalid @route comments.

This PR should fix these scenarios.