described_routes 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.3.0 2009-05-03
2
+
3
+ * 1 major enhancement:
4
+ * text output:
5
+ * rake described_routes:text
6
+ * curl <app>/described_routes.txt
7
+
1
8
  == 0.2.1 2009-04-29
2
9
 
3
10
  * 1 minor fix:
@@ -2,5 +2,5 @@ require 'described_routes/resource_template'
2
2
 
3
3
  module DescribedRoutes
4
4
  # rubygem version
5
- VERSION = "0.2.2"
5
+ VERSION = "0.3.0"
6
6
  end
@@ -10,6 +10,7 @@ module DescribedRoutes
10
10
  respond_to do |format|
11
11
  format.html # index.html.erb
12
12
  format.json { render :json => ResourceTemplate.to_json(resource_templates) }
13
+ format.text { render :text => ResourceTemplate.to_text(resource_templates) }
13
14
  format.yaml do
14
15
  yaml = resource_templates.to_yaml(resource_templates)
15
16
  yaml = yaml.grep(/(name|rel|path_template|uri_template|resources):|^---/).to_s if ['true', '1'].member?(params["short"])
@@ -31,14 +32,15 @@ module DescribedRoutes
31
32
  respond_to do |format|
32
33
  format.html # show.html.erb
33
34
  format.json { render :json => resource_template.to_json }
35
+ format.text { render :text => resource_template.to_text }
36
+ format.xml do
37
+ render :xml => resource_template.to_xml(Builder::XmlMarkup.new(:indent => 2)).target!
38
+ end
34
39
  format.yaml do
35
40
  yaml = resource_template.to_yaml
36
41
  yaml = yaml.grep(/(name|rel|path_template|uri_template|resources):|^---/).to_s if ['true', '1'].member?(params["short"])
37
42
  render :text => yaml
38
43
  end
39
- format.xml do
40
- render :xml => resource_template.to_xml(Builder::XmlMarkup.new(:indent => 2)).target!
41
- end
42
44
  end
43
45
  end
44
46
  end
@@ -27,7 +27,7 @@ module DescribedRoutes
27
27
  segs.gsub!(/:[a-zA-Z0-9_]+/) do |match|
28
28
  if match == ":id" && controller
29
29
  if controller == "described_routes/rails"
30
- "{route_name}"
30
+ ":route_name"
31
31
  else
32
32
  ":#{controller.singularize.sub(/.*\//, "")}_id"
33
33
  end
@@ -69,12 +69,14 @@ module DescribedRoutes
69
69
  # optional_params #=> ["format"]
70
70
  # action #=> "edit"
71
71
  # options #=> ["GET"]
72
- # name #=> "edit_user"
72
+ # name #=> "edit_user"
73
+ # controller #=> "rails"
73
74
 
74
75
  # create a new route hash
75
76
  resource = {
76
77
  "path_template" => template,
77
- "options" => options,
78
+ "options" => options,
79
+ "controller" => controller
78
80
  }
79
81
  resource["params"] = params unless params.empty?
80
82
  resource["optional_params"] = optional_params unless optional_params.empty?
@@ -104,7 +106,7 @@ module DescribedRoutes
104
106
 
105
107
  tree = map_key_tree(key_tree) do |key, children|
106
108
  resource = resources[key]
107
- resource["resource_templates"] = children unless children.empty?
109
+
108
110
  resource.delete("options") if resource["options"] == [""]
109
111
  resource["uri_template"] = base_url + resource["path_template"] if base_url && resource["path_template"]
110
112
 
@@ -127,6 +129,17 @@ module DescribedRoutes
127
129
  end
128
130
  end
129
131
 
132
+ controller = resource["controller"]
133
+ unless children.empty?
134
+ resource["resource_templates"] = children.sort_by{|c|
135
+ [
136
+ (c["controller"] == controller) ? "" : c["controller"], # group by controller, parent controller first
137
+ (c["params"] || []).length, # fewer params first
138
+ c["name"] # make determininistic
139
+ ]
140
+ }
141
+ end
142
+
130
143
  resource
131
144
  end
132
145
  end
@@ -170,6 +170,40 @@ module DescribedRoutes
170
170
  end
171
171
  h
172
172
  end
173
+
174
+ def self.to_table(resource_templates, parent_template = nil, t = [], indent = '')
175
+ resource_templates.inject(t) do |table, resource_template|
176
+ if parent_template
177
+ link = (resource_template.rel || '')
178
+ new_params = resource_template.params - parent_template.params
179
+ else
180
+ link = resource_template.name
181
+ new_params = resource_template.params
182
+ end
183
+ link += new_params.map{|p| "{#{p}}"}.join(', ')
184
+ table << [
185
+ indent + link,
186
+ resource_template.name || '',
187
+ resource_template.options.join(', '),
188
+ resource_template.uri_template || resource_template.path_template
189
+ ]
190
+ to_table(resource_template.resource_templates, resource_template, t, indent + ' ')
191
+ end
192
+ t
193
+ end
194
+
195
+ def self.to_text(resource_templates)
196
+ table = self.to_table(resource_templates)
197
+
198
+ 0.upto(2) do |i|
199
+ width = table.map{|row| row[i].length}.max
200
+ table.each do |row|
201
+ row[i] = row[i].ljust(width)
202
+ end
203
+ end
204
+
205
+ table.map{|row| row.join(' ')}.join("\n") + "\n"
206
+ end
173
207
  end
174
208
  end
175
209
 
@@ -30,6 +30,12 @@ namespace :described_routes do
30
30
  DescribedRoutes::RailsRoutes.get_resource_templates
31
31
  ).target!
32
32
  end
33
+
34
+ desc "Describe resource structure in text format"
35
+ task :text => :environment do
36
+ puts DescribedRoutes::ResourceTemplate.to_text(
37
+ DescribedRoutes::RailsRoutes.get_resource_templates)
38
+ end
33
39
 
34
40
  # unsupported, for testing
35
41
  task :ruby => :environment do
@@ -26,6 +26,9 @@ class TestResourceTemplate < Test::Unit::TestCase
26
26
  assert_equal("articles", user_articles.rel)
27
27
  assert_nil(user_article.rel)
28
28
  assert_equal("edit", edit_user_article.rel)
29
+
30
+ assert_equal("/users/{user_id}/articles{-prefix|.|format}", user_articles.path_template)
31
+ assert_equal("http://localhost:3000/users/{user_id}/articles{-prefix|.|format}", user_articles.uri_template)
29
32
 
30
33
  assert(user_articles.resource_templates.member?(user_article))
31
34
  assert(user_article.resource_templates.member?(edit_user_article))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: described_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Burrows
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-29 00:00:00 +01:00
12
+ date: 2009-05-03 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency