described_routes 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.6.1 2009-06-28
2
+
3
+ * Add all_preorder and all_postorder to ResourceTemplate and ResourceTemplates
4
+
1
5
  == 0.6.0 2009-06-28
2
6
 
3
7
  * Add automatic link header/element generation
@@ -2,5 +2,5 @@ require 'resource_template'
2
2
 
3
3
  module DescribedRoutes
4
4
  # rubygem version
5
- VERSION = "0.6.0"
5
+ VERSION = "0.6.1"
6
6
  end
@@ -203,6 +203,16 @@ class ResourceTemplate
203
203
  resource_templates.select{|resource_template| matching_rel === resource_template.rel}
204
204
  end
205
205
 
206
+ # self and descendants
207
+ def all_preorder
208
+ [self] + resource_templates.all_preorder
209
+ end
210
+
211
+ # defendants and self
212
+ def all_postorder
213
+ resource_templates.all_postorder + [self]
214
+ end
215
+
206
216
  class ResourceTemplates < Array
207
217
  # Initialize Resources (i.e. a new collection of ResourceTemplate objects) from given collection of ResourceTemplates or hashes
208
218
  def initialize(collection=[], parent=nil)
@@ -262,6 +272,20 @@ class ResourceTemplate
262
272
  end
263
273
  h
264
274
  end
275
+
276
+ # All members and their descendants, descendants last
277
+ def all_preorder
278
+ inject([]) do |a, resource_template|
279
+ a += resource_template.all_preorder
280
+ end
281
+ end
282
+
283
+ # All members and their descendants, descendants first
284
+ def all_postorder
285
+ inject([]) do |a, resource_template|
286
+ a += resource_template.all_postorder
287
+ end
288
+ end
265
289
 
266
290
  # for #to_text
267
291
  def to_table(parent_template = nil, t = [], indent = '')
@@ -2,12 +2,13 @@ require 'test/unit'
2
2
  require 'resource_template'
3
3
 
4
4
  class TestResourceTemplate < Test::Unit::TestCase
5
- attr_reader :json, :resource_templates, :resource_templates_by_name, :user_articles, :user_article, :edit_user_article
5
+ attr_reader :json, :resource_templates, :resource_templates_by_name, :user, :user_articles, :user_article, :edit_user_article
6
6
 
7
7
  def setup
8
8
  @json ||= File.read(File.dirname(__FILE__) + '/fixtures/described_routes_test.json')
9
9
  @resource_templates = ResourceTemplate::ResourceTemplates.new(JSON.parse(@json))
10
10
  @resource_templates_by_name = @resource_templates.all_by_name
11
+ @user = @resource_templates_by_name['user']
11
12
  @user_articles = @resource_templates_by_name['user_articles']
12
13
  @user_article = @resource_templates_by_name['user_article']
13
14
  @edit_user_article = @resource_templates_by_name['edit_user_article']
@@ -128,4 +129,38 @@ class TestResourceTemplate < Test::Unit::TestCase
128
129
  ],
129
130
  user_articles.resource_templates.expand_links({'user_id' => 'dojo'}).to_parsed)
130
131
  end
132
+
133
+ def test_all_preorder
134
+ assert_equal(
135
+ [
136
+ "user",
137
+ "user_articles",
138
+ "user_article",
139
+ "edit_user_article",
140
+ "new_user_article",
141
+ "recent_user_articles",
142
+ "edit_user",
143
+ "user_profile",
144
+ "edit_user_profile",
145
+ "new_user_profile"
146
+ ],
147
+ user.all_preorder.map{|rt| rt.name})
148
+ end
149
+
150
+ def test_all_postorder
151
+ assert_equal(
152
+ [
153
+ "edit_user_article",
154
+ "user_article",
155
+ "new_user_article",
156
+ "recent_user_articles",
157
+ "user_articles",
158
+ "edit_user",
159
+ "edit_user_profile",
160
+ "new_user_profile",
161
+ "user_profile",
162
+ "user"
163
+ ],
164
+ user.all_postorder.map{|rt| rt.name})
165
+ end
131
166
  end
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.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Burrows