rake_routes_normalizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ .geanyprj
4
+ Gemfile.lock
5
+ files/*
6
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rake_routes_normalizer.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'minitest' do
5
+ # with Minitest::Unit
6
+ #watch(%r|^test/test_(.*)\.rb|)
7
+ #watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
8
+ #watch(%r|^test/test_helper\.rb|) { "test" }
9
+
10
+ # with Minitest::Spec
11
+ watch(%r|^rake_routes_normalizer\.gemspec|) { "spec" }
12
+ watch(%r|^lib/rake_routes_normalizer\.rb|) { "spec" }
13
+ watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
14
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
15
+ watch(%r|^spec/(.*)_spec\.rb|)
16
+ end
data/README.markdown ADDED
@@ -0,0 +1,26 @@
1
+ Rake Routes Normalizer
2
+ ------------------------
3
+
4
+ rake_routes_normalizer is a utility for migrating routes from Rails 2 to 3.
5
+ It can normalize the printout of `rake routes` so that it's easier to diff
6
+ Rails 2 to 3 routes.
7
+
8
+ (Yes, Rails 3 was released last year. But better late than never.)
9
+
10
+ Usage
11
+ =====
12
+
13
+ In a rails directory:
14
+
15
+ rake routes | rake_routes_normalizer
16
+
17
+ Or if you saved the `rake routes` printout:
18
+
19
+ rake_routes_normalizer rake_routes_printout.txt
20
+
21
+ Install
22
+ =======
23
+
24
+ gem install rake_routes_normalizer
25
+
26
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rake_routes_normalizer'
4
+
5
+ case ARGV.length
6
+ when 0
7
+ if $stdin.tty?
8
+ # e.g. rake_routes_normalizer
9
+ RakeRoutesNormalizer::App.start # display the help
10
+ else
11
+ # rake routes | rake_routes_normalizer
12
+ RakeRoutesNormalizer::App.new.normalize(ARGF.read)
13
+ end
14
+ else
15
+ # e.g. rake_routes_normalizer rake_routes_printout.txt
16
+ RakeRoutesNormalizer::App.new.normalize(ARGF.read)
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'hashery/dictionary'
2
+ require 'hashery/key_hash'
3
+ require 'thor'
4
+ require 'valuable'
5
+
6
+ require "rake_routes_normalizer/version"
7
+
8
+ require "rake_routes_normalizer/app"
9
+ require "rake_routes_normalizer/exception_with_default_message"
10
+ require "rake_routes_normalizer/route"
11
+ require "rake_routes_normalizer/route_set"
@@ -0,0 +1,11 @@
1
+ module RakeRoutesNormalizer
2
+ class App < Thor
3
+ desc "RAKE_PRINTOUT_FILE", "Print the normalized version of RAKE_PRINTOUT_FILE"
4
+ def normalize(text)
5
+ table = RouteSet.parse(text).normalize.routes.map do |route|
6
+ [route.name, route.http_verb, route.url_pattern, route.params.inspect]
7
+ end
8
+ print_table table
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module RakeRoutesNormalizer
2
+ class ExceptionWithDefaultMessage < Exception
3
+ attr_reader :options
4
+ def initialize(options={})
5
+ @options = options
6
+ message = options.is_a?(String) ?
7
+ options :
8
+ "#{self.class}. Got #{options.inspect}"
9
+ super(message)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ module RakeRoutesNormalizer
2
+ class Route < Valuable
3
+ LINE_REGEX = %r{ *([abcdefghijklmnopqrstuvwxyz_]*) +([ABCDEFGHIJKLMNOPQRSTUVWXYZ]*) +(\S+) +(.*)}
4
+
5
+ has_value :http_verb
6
+ has_value :name
7
+ has_value :params
8
+ has_value :url_pattern
9
+
10
+ def self.parse(line)
11
+ new.tap do |result|
12
+ tokens = line.match(LINE_REGEX)
13
+ result.name = tokens[1]
14
+ result.http_verb = tokens[2]
15
+ result.url_pattern = tokens[3]
16
+ result.params = eval(tokens[4])
17
+ end
18
+ rescue NoMethodError => e
19
+ raise CannotParseLine.new(:line => line)
20
+ end
21
+
22
+ def <=>(other)
23
+ [:url_pattern, :http_verb].inject(0) do |result, attribute|
24
+ result == 0 ? (self.send(attribute) || '') <=> (other.send(attribute) || '') : result
25
+ end
26
+ end
27
+
28
+ def normalize(options = {})
29
+ clone.tap do |result|
30
+ result.http_verb = 'GET' if result.http_verb.to_s =~ /^\s*$/
31
+ result.name = options[:previous_route].name if result.name =~ /^\s*$/
32
+ result.params = Dictionary[KeyHash[params || {}]].order_by_key
33
+ end
34
+ end
35
+
36
+ class CannotParseLine < ExceptionWithDefaultMessage
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ module RakeRoutesNormalizer
2
+ class RouteSet < Valuable
3
+ has_collection :routes
4
+
5
+ def self.parse(text)
6
+ RouteSet.new.tap do |result|
7
+ result.routes = text.split("\n").reject{|line| line =~ /^\s*$/}.map{|line| Route.parse(line)}
8
+ end
9
+ end
10
+
11
+ def normalize
12
+ result = routes.inject(RouteSet.new) do|route_set, route|
13
+ route_set.routes << route.normalize(:previous_route => route_set.routes.last)
14
+ route_set
15
+ end
16
+ result.routes.sort!
17
+ result
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module RakeRoutesNormalizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rake_routes_normalizer/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rake_routes_normalizer"
7
+ s.version = RakeRoutesNormalizer::VERSION
8
+ s.authors = ["George Mendoza"]
9
+ s.email = ["gsmendoza@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = "Normalize the printout of rake routes so that it's easier to diff"
12
+ s.description = %q{
13
+ rake_routes_normalizer is a utility for migrating routes from Rails 2 to 3. It can
14
+ normalize the printout of rake routes so that it's easier to diff Rails 2 to 3 routes.
15
+ }
16
+
17
+ s.rubyforge_project = "rake_routes_normalizer"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ # specify any dependencies here; for example:
25
+ s.add_development_dependency 'guard-minitest'
26
+ s.add_development_dependency "minitest"
27
+ s.add_development_dependency "ruby-debug"
28
+
29
+ s.add_runtime_dependency "hashery"
30
+ s.add_runtime_dependency "thor"
31
+ s.add_runtime_dependency "valuable"
32
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe RouteSet do
4
+ describe "parse(text)" do
5
+ it "can convert one line of text to a route set" do
6
+ text = %Q{root /(.:format) {:action=>"default", :controller=>"pages"}}
7
+
8
+ route_set = RouteSet.parse(text)
9
+ route_set.routes.size.must_equal 1
10
+ end
11
+
12
+ it "should ignore blank lines" do
13
+ text = %Q|root /(.:format) {:action=>"default", :controller=>"pages"}\n |
14
+
15
+ route_set = RouteSet.parse(text)
16
+ route_set.routes.size.must_equal 1
17
+ end
18
+ end
19
+
20
+ describe "normalize" do
21
+ it "should be a copy of the route set" do
22
+ route_set = RouteSet.new.tap do |route_set|
23
+ route_set.routes << Route.new.tap do |route|
24
+ route.name = 'root'
25
+ route.http_verb = 'GET'
26
+ route.url_pattern = '/(.:format)'
27
+ route.params = {:action=>"default", :controller=>"pages"}
28
+ end
29
+ end
30
+
31
+ copy = route_set.normalize
32
+ copy.must_be_instance_of RouteSet
33
+ copy.wont_be_same_as route_set
34
+ copy.routes.size.must_equal 1
35
+
36
+ route = copy.routes[0]
37
+ route.wont_be_same_as route_set.routes[0]
38
+ route.name.must_equal 'root'
39
+ route.http_verb.must_equal 'GET'
40
+ route.url_pattern.must_equal '/(.:format)'
41
+ route.params.inspect.must_equal %q|{"action"=>"default", "controller"=>"pages"}|
42
+ end
43
+
44
+ it "should normalize its route" do
45
+ route_set = RouteSet.new.tap do |route_set|
46
+ route_set.routes << Route.new.tap do |route|
47
+ route.name = 'root'
48
+ route.http_verb = ''
49
+ route.url_pattern = '/(.:format)'
50
+ route.params = {:action=>"default", :controller=>"pages"}
51
+ end
52
+ end
53
+
54
+ route_set.normalize.routes[0].http_verb.must_equal "GET"
55
+ end
56
+
57
+ it "should normalize a route based the route's previous route" do
58
+ route_set = RouteSet.new(
59
+ :routes => [
60
+ Route.new(:name => 'account'),
61
+ Route.new(:name => '')
62
+ ]
63
+ )
64
+ copy = route_set.normalize
65
+ copy.routes.map(&:name).uniq.must_equal ['account']
66
+ end
67
+
68
+ it "should sort the routes" do
69
+ route_set = RouteSet.new(
70
+ :routes => [
71
+ Route.new(:url_pattern => '/airports(.:format)', :http_verb => 'GET'),
72
+ Route.new(:url_pattern => '/account(.:format)', :http_verb => 'GET')
73
+ ]
74
+ )
75
+ copy = route_set.normalize
76
+ copy.routes.map(&:url_pattern).uniq.must_equal ['/account(.:format)', '/airports(.:format)']
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+
3
+ describe Route do
4
+ describe "parse(text)" do
5
+ it "can convert the text to a route set" do
6
+ text = %q{root GET /(.:format) {:action=>"default", :controller=>"pages"}}
7
+
8
+ route = Route.parse(text)
9
+ route.name.must_equal 'root'
10
+ route.http_verb.must_equal 'GET'
11
+ route.url_pattern.must_equal '/(.:format)'
12
+ route.params.must_equal({:action => "default", :controller => "pages"})
13
+ end
14
+
15
+ it "can convert the text if the text does not have an http verb" do
16
+ text = %q{root /(.:format) {:action=>"default", :controller=>"pages"}}
17
+
18
+ route = Route.parse(text)
19
+ route.name.must_equal 'root'
20
+ route.http_verb.strip.must_equal ''
21
+ route.url_pattern.must_equal '/(.:format)'
22
+ route.params.must_equal({:action => "default", :controller => "pages"})
23
+ end
24
+
25
+ it "can convert the text if the text starts with spaces" do
26
+ text = %q{ root GET /(.:format) {:controller=>"pages", :action=>"default"}}
27
+
28
+ route = Route.parse(text)
29
+ route.name.must_equal 'root'
30
+ route.http_verb.must_equal 'GET'
31
+ route.url_pattern.must_equal '/(.:format)'
32
+ route.params.must_equal({:action => "default", :controller => "pages"})
33
+ end
34
+
35
+ it "can convert the text if the text does not have an name" do
36
+ text = %q{ PUT /account/change_password(.:format) {:controller=>"users", :action=>"change_password"}}
37
+
38
+ route = Route.parse(text)
39
+ route.name.must_equal ''
40
+ route.http_verb.must_equal 'PUT'
41
+ route.url_pattern.must_equal '/account/change_password(.:format)'
42
+ route.params.must_equal({:controller=>"users", :action=>"change_password"})
43
+ end
44
+ end
45
+
46
+ describe "normalize(options)" do
47
+ it "should return a copy of the route" do
48
+ route = Route.new.tap do |route|
49
+ route.name = 'root'
50
+ route.http_verb = 'GET'
51
+ route.url_pattern = '/(.:format)'
52
+ route.params = {:action=>"default", :controller=>"pages"}
53
+ end
54
+
55
+ copy = route.normalize
56
+ copy.must_be_instance_of Route
57
+ copy.wont_be_same_as route
58
+ end
59
+
60
+ it "should set the http_verb to GET if it is blank" do
61
+ route = Route.new(:http_verb => '')
62
+ route.normalize.http_verb.must_equal 'GET'
63
+ end
64
+
65
+ it "should set the name to the options[:previous_route]'s name if it is blank" do
66
+ previous_route = Route.new(:name => 'account_path')
67
+ route = Route.new(:name => '')
68
+ route.normalize(:previous_route => previous_route).name.must_equal 'account_path'
69
+ end
70
+
71
+ it "should sort the route's params by key" do
72
+ route = Route.new(:params => {:controller=>"users", :action=>"edit_password", :protocol=>nil})
73
+ route.normalize.params.inspect.must_equal %q|{"action"=>"edit_password", "controller"=>"users", "protocol"=>nil}|
74
+ end
75
+ end
76
+
77
+ describe "<=>(other)" do
78
+ it "should sort the route and other's url_pattern" do
79
+ routes = [
80
+ Route.new(:url_pattern => '/account(.:format)', :http_verb => 'GET'),
81
+ Route.new(:url_pattern => '/airports(.:format)', :http_verb => 'GET')
82
+ ]
83
+
84
+ (routes[0] <=> routes[1]).must_be :<, 0
85
+ end
86
+
87
+ it "should compare the route and other's url_pattern by http_verb, if the url patterns are the same" do
88
+ routes = [
89
+ Route.new(:url_pattern => '/account(.:format)', :http_verb => 'GET'),
90
+ Route.new(:url_pattern => '/account(.:format)', :http_verb => 'PUT')
91
+ ]
92
+
93
+ (routes[0] <=> routes[1]).must_be :<, 0
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'ruby-debug'
3
+
4
+ require 'rake_routes_normalizer'
5
+
6
+ include RakeRoutesNormalizer
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake_routes_normalizer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - George Mendoza
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-07 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: guard-minitest
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: minitest
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: ruby-debug
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: hashery
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ type: :runtime
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: thor
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ - !ruby/object:Gem::Dependency
91
+ name: valuable
92
+ prerelease: false
93
+ requirement: &id006 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ type: :runtime
103
+ version_requirements: *id006
104
+ description: "\n rake_routes_normalizer is a utility for migrating routes from Rails 2 to 3. It can\n normalize the printout of rake routes so that it's easier to diff Rails 2 to 3 routes.\n "
105
+ email:
106
+ - gsmendoza@gmail.com
107
+ executables:
108
+ - rake_routes_normalizer
109
+ extensions: []
110
+
111
+ extra_rdoc_files: []
112
+
113
+ files:
114
+ - .gitignore
115
+ - Gemfile
116
+ - Guardfile
117
+ - README.markdown
118
+ - Rakefile
119
+ - bin/rake_routes_normalizer
120
+ - lib/rake_routes_normalizer.rb
121
+ - lib/rake_routes_normalizer/app.rb
122
+ - lib/rake_routes_normalizer/exception_with_default_message.rb
123
+ - lib/rake_routes_normalizer/route.rb
124
+ - lib/rake_routes_normalizer/route_set.rb
125
+ - lib/rake_routes_normalizer/version.rb
126
+ - rake_routes_normalizer.gemspec
127
+ - spec/rake_routes_normalizer/route_set_spec.rb
128
+ - spec/rake_routes_normalizer/route_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: ""
131
+ licenses: []
132
+
133
+ post_install_message:
134
+ rdoc_options: []
135
+
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 3
153
+ segments:
154
+ - 0
155
+ version: "0"
156
+ requirements: []
157
+
158
+ rubyforge_project: rake_routes_normalizer
159
+ rubygems_version: 1.8.10
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: Normalize the printout of rake routes so that it's easier to diff
163
+ test_files: []
164
+