grep_routes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@grep_routes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in the gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ grep_routes (0.0.0)
5
+ actionpack (~> 3.1.3)
6
+ activesupport (~> 3.1.3)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ actionpack (3.1.3)
12
+ activemodel (= 3.1.3)
13
+ activesupport (= 3.1.3)
14
+ builder (~> 3.0.0)
15
+ erubis (~> 2.7.0)
16
+ i18n (~> 0.6)
17
+ rack (~> 1.3.5)
18
+ rack-cache (~> 1.1)
19
+ rack-mount (~> 0.8.2)
20
+ rack-test (~> 0.6.1)
21
+ sprockets (~> 2.0.3)
22
+ activemodel (3.1.3)
23
+ activesupport (= 3.1.3)
24
+ builder (~> 3.0.0)
25
+ i18n (~> 0.6)
26
+ activesupport (3.1.3)
27
+ multi_json (~> 1.0)
28
+ builder (3.0.0)
29
+ erubis (2.7.0)
30
+ hike (1.2.1)
31
+ i18n (0.6.0)
32
+ minitest (2.11.2)
33
+ multi_json (1.1.0)
34
+ rack (1.3.6)
35
+ rack-cache (1.1)
36
+ rack (>= 0.4)
37
+ rack-mount (0.8.3)
38
+ rack (>= 1.0.0)
39
+ rack-test (0.6.1)
40
+ rack (>= 1.0)
41
+ sprockets (2.0.3)
42
+ hike (~> 1.2)
43
+ rack (~> 1.0)
44
+ tilt (~> 1.1, != 1.3.0)
45
+ tilt (1.3.3)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ grep_routes!
52
+ minitest (~> 2.11.2)
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ Grep Routes
2
+ ===========
3
+ Running `rake routes` is super slow and a waste of time.
4
+
5
+ `grep_routes` is similar to `rake routes | grep someroute` but way faster.
6
+
7
+ Install
8
+ -------
9
+
10
+ gem install grep_routes
11
+
12
+ Usage
13
+ -----
14
+ All commands should be run from the root of your Rails3 project.
15
+
16
+ Show all your routes:
17
+
18
+ grep_routes
19
+
20
+ Grep through your routes:
21
+
22
+ grep_routes privacy_policy
23
+
24
+ Grep through your routes using regex:
25
+
26
+ grep_routes "(privacy_policy|terms|\w+_id)"
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs = %w(lib spec)
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ end
9
+
10
+ task :default => :test
data/bin/grep_routes ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ $:.unshift File.expand_path('../../lib/', __FILE__)
4
+ require "grep_routes"
5
+
6
+ path = './config/routes.rb'
7
+ path = './spec/fixtures/routes.rb'
8
+
9
+ gr = GrepRoutes.new(path)
10
+ gr.eval_routes
11
+
12
+ # filter
13
+ gr.filter_routes(ARGV[0]) if ARGV.any?
14
+
15
+ $stdout.puts gr.formatted_routes
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ Gem::Specification.new do |s|
4
+ s.name = "grep_routes"
5
+ s.version = "0.0.1"
6
+ s.authors = ["Tyler Montgomery"]
7
+ s.email = ["tyler.a.montgomery@gmail.com"]
8
+ s.homepage = "http://github.com/ubermajestix/grep_routes"
9
+ s.summary = %q{Fast Routes for Rails}
10
+ s.description = %q{Greppin in ur routes}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_dependency "activesupport" , "~> 3.1.3"
18
+ s.add_dependency "actionpack" , "~> 3.1.3"
19
+
20
+ s.add_development_dependency "minitest", "~> 2.11.2"
21
+ end
@@ -0,0 +1,129 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/hash/reverse_merge'
3
+ require 'active_support/core_ext/enumerable'
4
+ require 'action_dispatch'
5
+
6
+ class GrepRoutes
7
+ class Rails
8
+ def self.env
9
+ @_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
10
+ end
11
+ end
12
+
13
+ attr_reader :path_to_routes_file
14
+ attr_reader :route_file
15
+ attr_reader :class_name
16
+ attr_reader :pattern
17
+
18
+ def initialize(path_to_routes_file)
19
+ @path_to_routes_file = path_to_routes_file
20
+ @route_file = File.read(path_to_routes_file)
21
+ @class_name = route_file.match(/(\w+)::Application\.routes\.draw/)[1]
22
+ self.init_rails_class
23
+ self.init_rack_apps
24
+ end
25
+
26
+ # To make this fast we don't load your rails app or any of your gems.
27
+ # Instead we build YourRailsApp::Application class and define one method on it.
28
+ # The #routes method is almost the same as what Rails provides and once we eval
29
+ # the routes file, it will be filled up with your routes.
30
+ def init_rails_class
31
+ rails_app.module_eval do
32
+ self.const_set('Application', Class.new) unless self.const_defined?('Application')
33
+ self.const_get('Application', Class.new).class_eval do
34
+ def self.routes
35
+ @routes ||= ActionDispatch::Routing::RouteSet.new
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def rails_app
42
+ Object.const_set(class_name,Module.new) unless Object.const_defined?(class_name)
43
+ Object.const_get(class_name,Module.new)
44
+ end
45
+
46
+ # If there are any mounted Rack apps in your routes, we'll have to grab their
47
+ # classes and define #call on them so they look like Rack apps to the router.
48
+ #
49
+ # Only the last class needs to have #call defined on it but all the objects
50
+ # "above" it need to be defined as Modules - basically we define the object
51
+ # heirarchy.
52
+ def init_rack_apps
53
+ rack_apps = route_file.scan(/mount (.+?\s+)/).flatten.map(&:strip)
54
+ rack_apps.each do |class_name|
55
+ objects = class_name.split("::")
56
+ rack_class = objects.pop
57
+ last_object = Object
58
+ objects.each do |obj|
59
+ last_object.const_set(obj, Module.new) unless last_object.const_defined?(obj)
60
+ last_object = last_object.const_get(obj)
61
+ end
62
+ make_rackapp(last_object, rack_class)
63
+ end
64
+ end
65
+
66
+ def make_rackapp(mod, obj)
67
+ mod.const_set(obj,Class.new) unless mod.const_defined?(obj)
68
+ mod.const_get(obj,Class.new).class_eval do
69
+ def self.call
70
+ end
71
+ end
72
+ end
73
+
74
+ # This evals the routes file. After this method is called the RouteSet will
75
+ # have all of our routes inside it.
76
+ def eval_routes
77
+ eval(route_file)
78
+ end
79
+
80
+ # A shortcut to the RouteSet we defined in init_rails_class.
81
+ def route_set
82
+ rails_app.const_get('Application', Class.new).routes
83
+ end
84
+
85
+ # Returns an Array of Hashes to make it easier to reference parts of the route.
86
+ # This is stolen from the Rail's routes rake task.
87
+ def routes
88
+ return @routes if @routes
89
+
90
+ @routes = route_set.routes.collect do |route|
91
+ reqs = route.requirements.dup
92
+ reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
93
+ reqs = reqs.empty? ? "" : reqs.inspect
94
+ {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
95
+ end
96
+ # Skip the route if it's internal info route
97
+ @routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
98
+ return @routes
99
+ end
100
+
101
+ # This method filters the routes by matching the basic route string that will
102
+ # outputted against a string or regex.
103
+ #
104
+ # You should call this method before formatted_routes so that the offsets will
105
+ # only apply to the filtered routes. Otherwise you'll have really weird output
106
+ # like when you run `rake routes | grep somepattern`.
107
+ def filter_routes(pattern)
108
+ @pattern = Regexp.new pattern
109
+ @routes = routes.select{|r| "#{r[:name]} #{r[:verb]} #{r[:path]} #{r[:reqs]}".match pattern}
110
+ @routes
111
+ end
112
+
113
+ # This formats the route as an Array of Strings.
114
+ # This is stolen from the Rail's routes rake task.
115
+ def formatted_routes
116
+ name_width = routes.map{ |r| r[:name].length }.max
117
+ verb_width = routes.map{ |r| r[:verb].length }.max
118
+ path_width = routes.map{ |r| r[:path].length }.max
119
+ routes.collect do |r|
120
+ string = "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
121
+ string.gsub(pattern){|s| "\e[35m#{$1}\e[0m"} if pattern
122
+ end
123
+ end
124
+
125
+ def print
126
+ puts formatted_routes
127
+ end
128
+
129
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrepRoutes do
4
+ subject do
5
+ GrepRoutes.new('spec/fixtures/routes.rb')
6
+ end
7
+
8
+ it "should set the path to the routes file as an instance var" do
9
+ subject.path_to_routes_file.must_equal 'spec/fixtures/routes.rb'
10
+ end
11
+
12
+ it "should define the rails application class from the routes file" do
13
+ subject.rails_app.to_s.must_equal 'SomeRailsApp'
14
+ end
15
+
16
+ it "should have a shortcut to the RouteSet" do
17
+ subject.route_set.must_be_kind_of ActionDispatch::Routing::RouteSet
18
+ end
19
+
20
+ it "there should be routes after we eval the routes file" do
21
+ subject.eval_routes
22
+ subject.route_set.routes.wont_be_empty
23
+ end
24
+
25
+ it "should filter routes" do
26
+ subject.eval_routes
27
+ subject.filter_routes('privacy')
28
+ subject.routes.length.must_equal 1
29
+ end
30
+
31
+ it "should filter routes using a regex" do
32
+ subject.eval_routes
33
+ subject.filter_routes('(privacy|terms)')
34
+ subject.routes.length.must_equal 2
35
+ end
36
+ end
@@ -0,0 +1,63 @@
1
+ # This is psuedo-real routes file that has lots of fun stuff like scopes and
2
+ # namespaces, resources and named routes as well as conditional logic based on
3
+ # the Rails.env.
4
+ SomeRailsApp::Application.routes.draw do
5
+
6
+ resource :user_session, only: [ :create, :new, :destroy ]
7
+
8
+
9
+ namespace :admin do
10
+
11
+ resources :companies do
12
+ end
13
+
14
+ resources :employees, only: [:index] do
15
+ member do
16
+ put :revoke_access
17
+ end
18
+ end
19
+ end
20
+
21
+ resources :vacation do
22
+ get :home
23
+ get :store
24
+ member do
25
+ get :discussion
26
+ end
27
+ match 'profile_edit' => 'profiles#edit'
28
+ match 'profile_update' => 'profiles#update'
29
+ get :settings
30
+ post :settings
31
+ end
32
+
33
+ resources :subscriptions, :only => [:index, :update]
34
+
35
+ match 'login' => 'user_sessions#new'
36
+ match 'logout' => 'user_sessions#destroy'
37
+ match 'home' => 'companies#home'
38
+ match 'forgot_password' => 'password_resets#new'
39
+ match 'activate/:token' => 'activation#new', as: 'activate_account'
40
+
41
+ scope ':nickname/:slug', as: 'customer' do
42
+ match '/' => 'customer#show'
43
+ match '/settings' => 'customer#settings'
44
+ match '/ask_for_password' => 'customer#ask_for_password'
45
+ match '/submit_password' => 'customer#submit_password', coditions: {method: :post}
46
+ scope ':segment_slug', as: 'segment' do
47
+ match 'photos/:id' => 'photos#show', :as => "photo_show"
48
+ match 'posts/:id' => 'posts#show', :as => "post_show"
49
+ match 'videos/:id' => 'videos#show', :as => "video_show"
50
+ match 'details/:id' => 'triptrackers#show', :as => "detail_show"
51
+ end
52
+ end
53
+
54
+ match 'privacy' => 'welcome#privacy'
55
+ match 'terms' => 'welcome#terms'
56
+
57
+ root to: 'welcome#index'
58
+
59
+ # if development, the url is concourse.dev, otherwise it's www. for production, and enviroment_name. for anything else
60
+ default_url_options host: Rails.env.development? ? 'yoursite.dev': Rails.env.production? ? "www.example.com" : "#{ Rails.env }.example.com"
61
+
62
+
63
+ end
@@ -0,0 +1,19 @@
1
+ YourApp::Application.routes.draw do
2
+
3
+ resources :users, only: [:update]
4
+
5
+ match '/facebook/:action' => 'facebook', as: 'facebook'
6
+
7
+ if Rails.env.development?
8
+ # These are little Rack apps we defined somewhere in our Rails app.
9
+ # For our purposes here, we don't care what they do, we just have to define
10
+ # #call on them to statisfy the router.
11
+ #
12
+ # See the mail_view gem for previewing your mailer views in the browser.
13
+ mount Preview::ErrorMailer => 'mail_view/error_mailer'
14
+ mount Preview::UserMailer => 'mail_view/user_mailer'
15
+ mount Preview::StoreMailer => 'mail_view/store_mailer'
16
+ mount Preview::BrandMailer => 'mail_view/brand_mailer'
17
+ end
18
+
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ gem 'minitest' # ensures you're using the gem, and not the built in MT
3
+ require 'minitest/autorun'
4
+ $:.unshift('./lib')
5
+ require 'grep_routes'
6
+
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe GrepRoutes do
4
+
5
+ subject do
6
+ GrepRoutes.new('spec/fixtures/routes_with_rack_mounts.rb')
7
+ end
8
+
9
+ it "should handle mounted Rack apps" do
10
+ subject.init_rack_apps
11
+ subject.eval_routes
12
+ subject.routes.length.must_equal 6
13
+ end
14
+
15
+ it "should print the rack app routes" do
16
+ subject.init_rack_apps
17
+ subject.eval_routes
18
+ subject.print
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grep_routes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tyler Montgomery
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70298229753960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70298229753960
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &70298229753480 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.1.3
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70298229753480
36
+ - !ruby/object:Gem::Dependency
37
+ name: minitest
38
+ requirement: &70298229752960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.11.2
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70298229752960
47
+ description: Greppin in ur routes
48
+ email:
49
+ - tyler.a.montgomery@gmail.com
50
+ executables:
51
+ - grep_routes
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .rvmrc
56
+ - Gemfile
57
+ - Gemfile.lock
58
+ - README.md
59
+ - Rakefile
60
+ - bin/grep_routes
61
+ - grep_routes.gemspec
62
+ - lib/grep_routes.rb
63
+ - spec/basic_routes_spec.rb
64
+ - spec/fixtures/routes.rb
65
+ - spec/fixtures/routes_with_rack_mounts.rb
66
+ - spec/spec_helper.rb
67
+ - spec/with_rack_mounts_spec.rb
68
+ homepage: http://github.com/ubermajestix/grep_routes
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.5
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Fast Routes for Rails
92
+ test_files:
93
+ - spec/basic_routes_spec.rb
94
+ - spec/fixtures/routes.rb
95
+ - spec/fixtures/routes_with_rack_mounts.rb
96
+ - spec/spec_helper.rb
97
+ - spec/with_rack_mounts_spec.rb