annotate_routes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 70629e32fc5b2f0f2106104f5023729ece47254a
4
+ data.tar.gz: 42f516780b3fd6dcb51466b346e863270306217d
5
+ SHA512:
6
+ metadata.gz: 6055d4aef71ada5297e80dea666e7a70a0e28a44ecb720bb6957ae7046d51639ba49901490fc9a2bdb82b4382ce4fb09a9838e16ae10dca533d66ddb272cfff0
7
+ data.tar.gz: 05099bcf0603a111bce38a72a3180eb90774d89fb9c864ac62e86c7b93e1ba6d2b87af30af3707a100fd683600a572ef8a15cb4b972c2e3d777f3e2c4fec03ac
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in annotate_routes.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Fumiaki MATSUSHIMA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ # AnnotateRoutes
2
+
3
+ Annotate routes information to your controllers.
4
+
5
+ ```ruby
6
+ class CommentsController < ApplicationController
7
+ # == Route Info
8
+ # comments GET /comments(.:format)
9
+ def index
10
+ end
11
+
12
+ # == Route Info
13
+ # POST /comments(.:format)
14
+ def create
15
+ end
16
+
17
+ # == Route Info
18
+ # PATCH /comments/:id(.:format)
19
+ # PUT /comments/:id(.:format)
20
+ def update
21
+ end
22
+
23
+ # == Route Info
24
+ # DELETE /comments/:id(.:format)
25
+ def destroy
26
+ end
27
+ end
28
+ ```
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'annotate_routes'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install annotate_routes
45
+
46
+ ## Usage
47
+
48
+ $ rake annotate_routes
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/[my-github-username]/annotate_routes/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'annotate_routes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "annotate_routes"
8
+ spec.version = AnnotateRoutes::VERSION
9
+ spec.authors = ["Fumiaki MATSUSHIMA"]
10
+ spec.email = ["mtsmfm@gmail.com"]
11
+ spec.summary = %q{Annotate Rails routes}
12
+ spec.description = %q{Annotate Rails routes to your controllers}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency 'rails', '> 3.2'
25
+ end
@@ -0,0 +1,9 @@
1
+ require "annotate_routes/version"
2
+
3
+ module AnnotateRoutes
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ require 'annotate_routes/tasks'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ require 'annotate_routes/exceptions'
2
+
3
+ module AnnotateRoutes
4
+ class Annotator
5
+ COMMENT = '== Route Info'.freeze
6
+
7
+ def initialize(root, route)
8
+ @root = root
9
+ @reqs = route.fetch(:reqs)
10
+ @info = route.fetch(:info)
11
+ end
12
+
13
+ def annotate!
14
+ raise ControllerNotFound.new(controller_file_path) unless File.exists?(controller_file_path)
15
+
16
+ File.open(controller_file_path, 'r+') do |f|
17
+ body = f.read
18
+
19
+ raise ActionNotFound.new(@reqs) unless body =~ /def #{action}\W/
20
+
21
+ new_body = body.gsub(/(?:^ *# #{COMMENT}\n(?: *#.*\n)+)?( *def #{action}\W)/, comment + "\\1")
22
+
23
+ f.rewind
24
+ f.write(new_body)
25
+ end
26
+ end
27
+
28
+ def comment
29
+ ["#{COMMENT}", *@info.map {|i| i.join(' ')} ].map {|c| " # " + c }.join("\n") + "\n"
30
+ end
31
+
32
+ def controller_file_path
33
+ @root.join("app/controllers/#{controller}_controller.rb")
34
+ end
35
+
36
+ def controller
37
+ @controller ||= @reqs.split('#').first
38
+ end
39
+
40
+ def action
41
+ @action ||= @reqs.split('#').last
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ module AnnotateRoutes
2
+ class ControllerNotFound < StandardError
3
+ attr_reader :file
4
+
5
+ def initialize(file)
6
+ @file = file
7
+ end
8
+
9
+ def message
10
+ "No such controller - #{file}"
11
+ end
12
+ end
13
+
14
+ class ActionNotFound < StandardError
15
+ attr_reader :reqs
16
+
17
+ def initialize(reqs)
18
+ @reqs = reqs
19
+ end
20
+
21
+ def message
22
+ "No such action - #{reqs}"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ require 'annotate_routes/route_wrapper'
2
+
3
+ module AnnotateRoutes
4
+ class Inspector
5
+ def initialize(routes_stream)
6
+ @routes_stream = routes_stream
7
+ end
8
+
9
+ def routes
10
+ @routes_stream.each_line
11
+ .map {|l| RouteWrapper.new(l) }
12
+ .select(&:reqs)
13
+ .group_by(&:reqs)
14
+ .map {|reqs, r| {reqs: reqs, info: r.map(&:info)} }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module AnnotateRoutes
2
+ class RouteWrapper
3
+ attr_reader :info, :reqs
4
+
5
+ def initialize(route_str)
6
+ if route_str =~ /(.*) +([^[:upper:]]+#[^[:upper:]]+)\n/
7
+ @info = $1.strip.split(' ')
8
+ @reqs = $2
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ load 'annotate_routes/tasks/annotate_routes.rake'
@@ -0,0 +1,22 @@
1
+ namespace :annotate_routes do
2
+ task :run => [:environment, :setup] do
3
+ routes_stream = capture(:stdout) { Rake::Task['routes'].invoke }
4
+ inspector = AnnotateRoutes::Inspector.new(routes_stream)
5
+ inspector.routes.each do |route|
6
+ begin
7
+ AnnotateRoutes::Annotator.new(Rails.root, route).annotate!
8
+ puts "#{route[:reqs]} written"
9
+ rescue => e
10
+ puts e.message
11
+ end
12
+ end
13
+ end
14
+
15
+ task :setup do
16
+ require 'annotate_routes/annotator'
17
+ require 'annotate_routes/inspector'
18
+ end
19
+ end
20
+
21
+ desc 'Annotate routes'
22
+ task :annotate_routes => 'annotate_routes:run'
@@ -0,0 +1,3 @@
1
+ module AnnotateRoutes
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: annotate_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Fumiaki MATSUSHIMA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: Annotate Rails routes to your controllers
56
+ email:
57
+ - mtsmfm@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - annotate_routes.gemspec
68
+ - lib/annotate_routes.rb
69
+ - lib/annotate_routes/annotator.rb
70
+ - lib/annotate_routes/exceptions.rb
71
+ - lib/annotate_routes/inspector.rb
72
+ - lib/annotate_routes/route_wrapper.rb
73
+ - lib/annotate_routes/tasks.rb
74
+ - lib/annotate_routes/tasks/annotate_routes.rake
75
+ - lib/annotate_routes/version.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.5
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Annotate Rails routes
100
+ test_files: []
101
+ has_rdoc: