tokamak 1.0.0.beta2 → 1.0.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/LICENSE +14 -0
  2. data/README.md +159 -0
  3. data/lib/tokamak/builder/base.rb +74 -0
  4. data/lib/tokamak/{json/builder.rb → builder/json.rb} +38 -29
  5. data/lib/tokamak/builder/values.rb +33 -0
  6. data/lib/tokamak/{atom/builder.rb → builder/xml.rb} +46 -41
  7. data/lib/tokamak/builder.rb +22 -0
  8. data/lib/tokamak/errors.rb +3 -0
  9. data/lib/tokamak/hook/rails.rb +78 -0
  10. data/lib/tokamak/hook/sinatra.rb +18 -0
  11. data/lib/tokamak/hook/tilt.rb +42 -0
  12. data/lib/tokamak/hook.rb +6 -0
  13. data/lib/tokamak/recipes.rb +26 -0
  14. data/lib/tokamak/version.rb +13 -0
  15. data/lib/tokamak.rb +14 -18
  16. data/script/console +7 -0
  17. data/test/rails2_skel/Rakefile +16 -0
  18. data/test/rails2_skel/app/controllers/application_controller.rb +1 -0
  19. data/test/rails2_skel/app/controllers/test_controller.rb +18 -0
  20. data/test/rails2_skel/app/views/test/_feed_member.tokamak +9 -0
  21. data/test/rails2_skel/app/views/test/feed.tokamak +24 -0
  22. data/test/rails2_skel/app/views/test/show.tokamak +31 -0
  23. data/test/rails2_skel/config/boot.rb +110 -0
  24. data/test/rails2_skel/config/environment.rb +20 -0
  25. data/test/rails2_skel/config/environments/development.rb +17 -0
  26. data/test/rails2_skel/config/environments/production.rb +28 -0
  27. data/test/rails2_skel/config/environments/test.rb +28 -0
  28. data/test/rails2_skel/config/initializers/cookie_verification_secret.rb +2 -0
  29. data/test/rails2_skel/config/initializers/mime_types.rb +3 -0
  30. data/test/rails2_skel/config/initializers/new_rails_defaults.rb +10 -0
  31. data/test/rails2_skel/config/initializers/session_store.rb +5 -0
  32. data/test/rails2_skel/config/routes.rb +43 -0
  33. data/test/rails2_skel/script/console +3 -0
  34. data/test/test_helper.rb +7 -0
  35. data/test/tokamak/builder/base_test.rb +28 -0
  36. data/test/tokamak/builder/json_test.rb +227 -0
  37. data/test/tokamak/builder/xml_test.rb +254 -0
  38. data/test/tokamak/helper_test.rb +106 -0
  39. data/test/tokamak/hook/rails_test.rb +74 -0
  40. data/test/tokamak/hook/sinatra_test.rb +85 -0
  41. data/test/tokamak/hook/tilt_test.rb +35 -0
  42. data/test/tokamak/recipes_test.rb +90 -0
  43. metadata +106 -113
  44. data/.document +0 -5
  45. data/.rspec +0 -1
  46. data/Gemfile +0 -27
  47. data/Gemfile.lock +0 -77
  48. data/LICENSE.txt +0 -20
  49. data/README.rdoc +0 -69
  50. data/Rakefile +0 -50
  51. data/VERSION +0 -1
  52. data/lib/tokamak/atom/base.rb +0 -87
  53. data/lib/tokamak/atom/helpers.rb +0 -13
  54. data/lib/tokamak/atom.rb +0 -8
  55. data/lib/tokamak/error.rb +0 -6
  56. data/lib/tokamak/json/base.rb +0 -83
  57. data/lib/tokamak/json/helpers.rb +0 -13
  58. data/lib/tokamak/json.rb +0 -10
  59. data/lib/tokamak/representation/atom/atom.rng +0 -597
  60. data/lib/tokamak/representation/atom/base.rb +0 -140
  61. data/lib/tokamak/representation/atom/category.rb +0 -39
  62. data/lib/tokamak/representation/atom/entry.rb +0 -56
  63. data/lib/tokamak/representation/atom/factory.rb +0 -48
  64. data/lib/tokamak/representation/atom/feed.rb +0 -108
  65. data/lib/tokamak/representation/atom/link.rb +0 -66
  66. data/lib/tokamak/representation/atom/person.rb +0 -46
  67. data/lib/tokamak/representation/atom/source.rb +0 -57
  68. data/lib/tokamak/representation/atom/tag_collection.rb +0 -36
  69. data/lib/tokamak/representation/atom/xml.rb +0 -94
  70. data/lib/tokamak/representation/atom.rb +0 -18
  71. data/lib/tokamak/representation/generic.rb +0 -20
  72. data/lib/tokamak/representation/json/base.rb +0 -25
  73. data/lib/tokamak/representation/json/keys_as_methods.rb +0 -72
  74. data/lib/tokamak/representation/json/link.rb +0 -27
  75. data/lib/tokamak/representation/json/link_collection.rb +0 -21
  76. data/lib/tokamak/representation/json.rb +0 -11
  77. data/lib/tokamak/representation/links.rb +0 -9
  78. data/lib/tokamak/representation.rb +0 -3
  79. data/lib/tokamak/values.rb +0 -29
  80. data/lib/tokamak/xml/base.rb +0 -60
  81. data/lib/tokamak/xml/builder.rb +0 -115
  82. data/lib/tokamak/xml/helpers.rb +0 -13
  83. data/lib/tokamak/xml/link.rb +0 -31
  84. data/lib/tokamak/xml/links.rb +0 -35
  85. data/lib/tokamak/xml.rb +0 -12
  86. data/spec/integration/atom/atom_spec.rb +0 -191
  87. data/spec/integration/full_atom.xml +0 -92
  88. data/spec/integration/full_json.js +0 -46
  89. data/spec/integration/json/json_spec.rb +0 -172
  90. data/spec/integration/xml/xml_spec.rb +0 -203
  91. data/spec/spec_helper.rb +0 -12
@@ -0,0 +1,227 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ class Tokamak::Builder::JsonTest < Test::Unit::TestCase
4
+
5
+ def test_media_type_should_be_json
6
+ assert_equal ["application/json"], Tokamak::Builder::Json.media_types
7
+ end
8
+
9
+ def test_the_most_simple_json
10
+ obj = [{ :foo => "bar" }]
11
+ json = Tokamak::Builder::Json.build(obj) do |collection|
12
+ collection.values do |values|
13
+ values.id "an_id"
14
+ end
15
+
16
+ collection.members do |member, some_foos|
17
+ member.values do |values|
18
+ values.id some_foos[:foo]
19
+ end
20
+ end
21
+ end
22
+
23
+ hash = JSON.parse(json).extend(Methodize)
24
+
25
+ assert_equal "an_id", hash.id
26
+ assert_equal "bar" , hash.members.first.id
27
+ end
28
+
29
+ def test_root_set_on_builder
30
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
31
+ json = Tokamak::Builder::Json.build(obj, :root => "foos") do |collection|
32
+ collection.values do |values|
33
+ values.id "an_id"
34
+ end
35
+
36
+ collection.members do |member, some_foos|
37
+ member.values do |values|
38
+ values.id some_foos[:foo]
39
+ end
40
+ end
41
+ end
42
+
43
+ hash = JSON.parse(json).extend(Methodize)
44
+
45
+ assert hash.has_key?("foos")
46
+ assert_equal "an_id", hash.foos.id
47
+ assert_equal "bar" , hash.foos.members.first.id
48
+ end
49
+
50
+ def test_collection_set_on_members
51
+ obj = { :foo => "bar" }
52
+ a_collection = [1,2,3,4]
53
+ json = Tokamak::Builder::Json.build(obj) do |collection|
54
+ collection.values do |values|
55
+ values.id "an_id"
56
+ end
57
+
58
+ collection.members(:collection => a_collection) do |member, number|
59
+ member.values do |values|
60
+ values.id number
61
+ end
62
+ end
63
+ end
64
+
65
+ hash = JSON.parse(json).extend(Methodize)
66
+
67
+ assert_equal "an_id", hash.id
68
+ assert_equal 1 , hash.members.first.id
69
+ assert_equal 4 , hash.members.size
70
+ end
71
+
72
+ def test_raise_exception_for_not_passing_a_collection_as_parameter_to_members
73
+ obj = 42
74
+
75
+ assert_raise Tokamak::BuilderError do
76
+ json = Tokamak::Builder::Json.build(obj) do |collection, number|
77
+ collection.values do |values|
78
+ values.id number
79
+ end
80
+
81
+ collection.members do |member, item|
82
+ member.values do |values|
83
+ values.id item
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def test_root_set_on_members
91
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
92
+ json = Tokamak::Builder::Json.build(obj) do |collection|
93
+ collection.values do |values|
94
+ values.id "an_id"
95
+ end
96
+
97
+ collection.members(:root => "foos") do |member, some_foos|
98
+ member.values do |values|
99
+ values.id some_foos[:foo]
100
+ end
101
+ end
102
+ end
103
+
104
+ hash = JSON.parse(json).extend(Methodize)
105
+
106
+ assert_equal "an_id", hash.id
107
+ assert_equal "bar" , hash.foos.first.id
108
+ assert_equal 2 , hash.foos.size
109
+ end
110
+
111
+ def test_nested_crazy_values
112
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
113
+ json = Tokamak::Builder::Json.build(obj) do |collection|
114
+ collection.values do |values|
115
+ values.body {
116
+ values.face {
117
+ values.eyes "blue"
118
+ values.mouth "large"
119
+ }
120
+ values.legs [
121
+ { :right => { :fingers_count => 5 } }, { :left => { :fingers_count => 4 } }
122
+ ]
123
+ }
124
+ end
125
+ end
126
+
127
+ hash = JSON.parse(json).extend(Methodize)
128
+
129
+ assert_equal "blue" , hash.body.face.eyes
130
+ assert_equal "large", hash.body.face.mouth
131
+ assert_equal 2 , hash.body.legs.count
132
+ assert_equal 4 , hash.body.legs.last.left.fingers_count
133
+ end
134
+
135
+ def test_build_full_collection
136
+ time = Time.now
137
+ some_articles = [
138
+ {:id => 1, :title => "a great article", :updated => time},
139
+ {:id => 2, :title => "another great article", :updated => time}
140
+ ]
141
+
142
+ json = Tokamak::Builder::Json.build(some_articles) do |collection|
143
+ collection.values do |values|
144
+ values.id "http://example.com/json"
145
+ values.title "Feed"
146
+ values.updated time
147
+
148
+ values.author {
149
+ values.name "John Doe"
150
+ values.email "joedoe@example.com"
151
+ }
152
+
153
+ values.author {
154
+ values.name "Foo Bar"
155
+ values.email "foobar@example.com"
156
+ }
157
+ end
158
+
159
+ collection.link("next" , "http://a.link.com/next")
160
+ collection.link("previous", "http://a.link.com/previous")
161
+
162
+ collection.members(:root => "articles") do |member, article|
163
+ member.values do |values|
164
+ values.id "uri:#{article[:id]}"
165
+ values.title article[:title]
166
+ values.updated article[:updated]
167
+ end
168
+
169
+ member.link("image", "http://example.com/image/1")
170
+ member.link("image", "http://example.com/image/2", :type => "application/json")
171
+ end
172
+ end
173
+
174
+ hash = JSON.parse(json)
175
+ hash.extend(Methodize)
176
+
177
+ assert_equal "John Doe" , hash.author.first.name
178
+ assert_equal "foobar@example.com" , hash.author.last.email
179
+ assert_equal "http://example.com/json", hash.id
180
+
181
+ assert_equal "http://a.link.com/next" , hash.link.first.href
182
+ assert_equal "next" , hash.link.first.rel
183
+ assert_equal "application/json" , hash.link.last.type
184
+
185
+ assert_equal "uri:1" , hash.articles.first.id
186
+ assert_equal "a great article" , hash.articles.first.title
187
+ assert_equal "http://example.com/image/1" , hash.articles.last.link.first.href
188
+ assert_equal "image" , hash.articles.last.link.first.rel
189
+ assert_equal "application/json" , hash.articles.last.link.last.type
190
+ end
191
+
192
+ def test_build_full_member
193
+ time = Time.now
194
+ an_article = {:id => 1, :title => "a great article", :updated => time}
195
+
196
+ json = Tokamak::Builder::Json.build(an_article, :root => "article") do |member, article|
197
+ member.values do |values|
198
+ values.id "uri:#{article[:id]}"
199
+ values.title article[:title]
200
+ values.updated article[:updated]
201
+
202
+ values.domain("xmlns" => "http://a.namespace.com") {
203
+ member.link("image", "http://example.com/image/1")
204
+ member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
205
+ }
206
+ end
207
+
208
+ member.link("image", "http://example.com/image/1")
209
+ member.link("image", "http://example.com/image/2", :type => "application/json")
210
+ end
211
+
212
+ hash = JSON.parse(json)
213
+ hash.extend(Methodize)
214
+
215
+ assert_equal "uri:1" , hash.article.id
216
+ assert_equal "a great article" , hash.article.title
217
+ assert_equal "http://example.com/image/1" , hash.article.link.first.href
218
+ assert_equal "image" , hash.article.link.first.rel
219
+ assert_equal "application/json" , hash.article.link.first.type
220
+
221
+ assert_equal "http://example.com/image/1" , hash.article.domain.link.first.href
222
+ assert_equal "image" , hash.article.domain.link.first.rel
223
+ assert_equal "application/json" , hash.article.domain.link.first.type
224
+ assert_equal "http://a.namespace.com" , hash.article.domain.xmlns
225
+ end
226
+ end
227
+
@@ -0,0 +1,254 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_helper')
2
+
3
+ class Tokamak::Builder::XmlTest < Test::Unit::TestCase
4
+
5
+ def test_media_type_should_be_xml
6
+ assert_equal ["application/xml","text/xml"], Tokamak::Builder::Xml.media_types
7
+ end
8
+
9
+ def test_the_most_simple_xml
10
+ obj = [{ :foo => "bar" }]
11
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
12
+ collection.values do |values|
13
+ values.id "an_id"
14
+ end
15
+
16
+ collection.members do |member, some_foos|
17
+ member.values do |values|
18
+ values.id some_foos[:foo]
19
+ end
20
+ end
21
+ end
22
+
23
+ xml = Nokogiri::XML::Document.parse(xml)
24
+
25
+ assert_equal "root" , xml.root.name
26
+ assert_equal "an_id", xml.css("root id").first.text
27
+ assert_equal "bar" , xml.css("root > member > id").first.text
28
+ end
29
+
30
+ def test_root_set_on_builder
31
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
32
+ xml = Tokamak::Builder::Xml.build(obj, :root => "foos") do |collection|
33
+ collection.values do |values|
34
+ values.id "an_id"
35
+ end
36
+
37
+ collection.members do |member, some_foos|
38
+ member.values do |values|
39
+ values.id some_foos[:foo]
40
+ end
41
+ end
42
+ end
43
+
44
+ xml = Nokogiri::XML::Document.parse(xml)
45
+
46
+ assert_equal "foos" , xml.root.name
47
+ assert_equal "an_id", xml.css("foos id").first.text
48
+ assert_equal "bar" , xml.css("foos > member > id").first.text
49
+ end
50
+
51
+ def test_collection_set_on_members
52
+ obj = { :foo => "bar" }
53
+ a_collection = [1,2,3,4]
54
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
55
+ collection.values do |values|
56
+ values.id "an_id"
57
+ end
58
+
59
+ collection.members(:collection => a_collection) do |member, number|
60
+ member.values do |values|
61
+ values.id number
62
+ end
63
+ end
64
+ end
65
+
66
+ xml = Nokogiri::XML::Document.parse(xml)
67
+
68
+ assert_equal "an_id", xml.css("root id").first.text
69
+ assert_equal "1" , xml.css("root > member > id").first.text
70
+ assert_equal 4 , xml.css("root > member > id").size
71
+ end
72
+
73
+ def test_raise_exception_for_not_passing_a_collection_as_parameter_to_members
74
+ obj = 42
75
+
76
+ assert_raise Tokamak::BuilderError do
77
+ json = Tokamak::Builder::Json.build(obj) do |collection, number|
78
+ collection.values do |values|
79
+ values.id number
80
+ end
81
+
82
+ collection.members do |member, item|
83
+ member.values do |values|
84
+ values.id item
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ def test_root_set_on_members
92
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
93
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
94
+ collection.values do |values|
95
+ values.id "an_id"
96
+ end
97
+
98
+ collection.members(:root => "foos") do |member, some_foos|
99
+ member.values do |values|
100
+ values.id some_foos[:foo]
101
+ end
102
+ end
103
+ end
104
+
105
+ xml = Nokogiri::XML::Document.parse(xml)
106
+
107
+ assert_equal "an_id", xml.css("root id").first.text
108
+ assert_equal "bar" , xml.css("root > foos > id").first.text
109
+ assert_equal 2 , xml.css("root > foos > id").size
110
+ end
111
+
112
+ def test_nested_crazy_values
113
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
114
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
115
+ collection.values do |values|
116
+ values.body {
117
+ values.face {
118
+ values.eyes "blue"
119
+ values.mouth "large"
120
+ }
121
+ }
122
+ end
123
+ end
124
+
125
+ xml = Nokogiri::XML::Document.parse(xml)
126
+
127
+ assert_equal "blue" , xml.css("root > body > face > eyes").first.text
128
+ assert_equal "large", xml.css("root > body > face > mouth").first.text
129
+ end
130
+
131
+ def test_xml_attributes_on_values
132
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
133
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
134
+ collection.values do |values|
135
+ values.body(:type => "fat", :gender => "male") {
136
+ values.face {
137
+ values.eyes "blue"
138
+ values.mouth "large", :teeth_count => 32
139
+ }
140
+ }
141
+ end
142
+ end
143
+
144
+ xml = Nokogiri::XML::Document.parse(xml)
145
+
146
+ assert_equal "fat" , xml.css("root > body").first["type"]
147
+ assert_equal "32" , xml.css("root > body > face > mouth").first["teeth_count"]
148
+ end
149
+
150
+ def test_xml_namespaces_on_values
151
+ obj = [{ :foo => "bar" }, { :foo => "zue" }]
152
+ xml = Tokamak::Builder::Xml.build(obj) do |collection|
153
+ collection.values do |values|
154
+ values.body("xmlns:biology" => "http://a.biology.namespace.com") {
155
+ values["biology"].face {
156
+ values["biology"].eyes "blue"
157
+ values["biology"].mouth "large", :teeth_count => 32
158
+ }
159
+ }
160
+ end
161
+ end
162
+
163
+ xml = Nokogiri::XML::Document.parse(xml)
164
+
165
+ assert_equal "biology", xml.at_xpath(".//biology:face", {"biology" => "http://a.biology.namespace.com"}).namespace.prefix
166
+ end
167
+
168
+ def test_build_full_collection
169
+ time = Time.now
170
+ some_articles = [
171
+ {:id => 1, :title => "a great article", :updated => time},
172
+ {:id => 2, :title => "another great article", :updated => time}
173
+ ]
174
+
175
+ xml = Tokamak::Builder::Xml.build(some_articles) do |collection|
176
+ collection.values do |values|
177
+ values.id "http://example.com/json"
178
+ values.title "Feed"
179
+ values.updated time
180
+
181
+ values.author {
182
+ values.name "John Doe"
183
+ values.email "joedoe@example.com"
184
+ }
185
+
186
+ values.author {
187
+ values.name "Foo Bar"
188
+ values.email "foobar@example.com"
189
+ }
190
+ end
191
+
192
+ collection.link("next" , "http://a.link.com/next")
193
+ collection.link("previous", "http://a.link.com/previous")
194
+
195
+ collection.members(:root => "articles") do |member, article|
196
+ member.values do |values|
197
+ values.id "uri:#{article[:id]}"
198
+ values.title article[:title]
199
+ values.updated article[:updated]
200
+ end
201
+
202
+ member.link("image", "http://example.com/image/1")
203
+ member.link("image", "http://example.com/image/2", :type => "application/json")
204
+ end
205
+ end
206
+
207
+ xml = Nokogiri::XML::Document.parse(xml)
208
+
209
+ assert_equal "John Doe" , xml.css("root > author").first.css("name").first.text
210
+ assert_equal "foobar@example.com" , xml.css("root > author").last.css("email").first.text
211
+
212
+ assert_equal "http://a.link.com/next" , xml.css("root > link").first["href"]
213
+ assert_equal "next" , xml.css("root > link").first["rel"]
214
+ assert_equal "application/xml" , xml.css("root > link").last["type"]
215
+
216
+ assert_equal "uri:1" , xml.css("root > articles").first.css("id").first.text
217
+ assert_equal "a great article" , xml.css("root > articles").first.css("title").first.text
218
+ assert_equal "http://example.com/image/1" , xml.css("root > articles").first.css("link").first["href"]
219
+ assert_equal "image" , xml.css("root > articles").first.css("link").first["rel"]
220
+ assert_equal "application/json" , xml.css("root > articles").first.css("link").last["type"]
221
+ end
222
+
223
+ def test_build_full_member
224
+ time = Time.now
225
+ an_article = {:id => 1, :title => "a great article", :updated => time}
226
+
227
+ xml = Tokamak::Builder::Xml.build(an_article, :root => "article") do |member, article|
228
+ member.values do |values|
229
+ values.id "uri:#{article[:id]}"
230
+ values.title article[:title]
231
+ values.updated article[:updated]
232
+
233
+ values.domain("xmlns" => "http://a.namespace.com") {
234
+ member.link("image", "http://example.com/image/1")
235
+ member.link("image", "http://example.com/image/2", :type => "application/atom+xml")
236
+ }
237
+ end
238
+
239
+ member.link("image", "http://example.com/image/1")
240
+ member.link("image", "http://example.com/image/2", :type => "application/json")
241
+ end
242
+
243
+ xml = Nokogiri::XML::Document.parse(xml)
244
+
245
+ assert_equal "http://example.com/image/1" , xml.css("article > link").first["href"]
246
+ assert_equal "image" , xml.css("article > link").first["rel"]
247
+ assert_equal "application/json" , xml.css("article > link").last["type"]
248
+
249
+ assert_equal "http://example.com/image/1" , xml.xpath("/article/xmlns:domain/xmlns:link", {"xmlns" => "http://a.namespace.com"}).first["href"]
250
+ assert_equal "image" , xml.xpath("/article/xmlns:domain/xmlns:link", {"xmlns" => "http://a.namespace.com"}).first["rel"]
251
+ assert_equal "application/atom+xml" , xml.xpath("/article/xmlns:domain/xmlns:link", {"xmlns" => "http://a.namespace.com"}).last["type"]
252
+ end
253
+ end
254
+
@@ -0,0 +1,106 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
2
+
3
+ # every builder comes with a default helper, with collection and member methods. See lib/tokamak/builder.rb
4
+ class TestDefaultHelper
5
+ extend Tokamak::Builder::Json.helper
6
+ end
7
+
8
+ # but here you can cheack how to pass other default options to helper methods
9
+ class DummyAtom < Tokamak::Builder::Base
10
+ builder_for "application/atom+xml"
11
+
12
+ collection_helper_default_options :atom_type => :feed
13
+ member_helper_default_options :atom_type => :entry
14
+
15
+ def initialize(obj, options = {})
16
+ #do nothing
17
+ end
18
+
19
+ def representation
20
+ "puft!"
21
+ end
22
+ end
23
+ class AtomGenerator
24
+ extend DummyAtom.helper
25
+ end
26
+
27
+ # how to complete override the helper
28
+ module MyHelper
29
+ # these examples are just to show that you can freely change the behavior
30
+ # of the helper methods, since you respect the methods signature
31
+ def collection(obj, *args, &block)
32
+ member(obj, *args, &block)
33
+ end
34
+
35
+ def member(obj, *args, &block)
36
+ default_options = {:my_option => "my_value"}
37
+ default_options.merge!(args.shift)
38
+ args.unshift(default_options)
39
+ OverwrittenHelperBuilder.build(obj, *args, &block)
40
+ end
41
+ end
42
+ class OverwrittenHelperBuilder < Tokamak::Builder::Base
43
+ builder_for "some/media+type"
44
+
45
+ # just implement this method passing the new helper
46
+ def self.helper
47
+ MyHelper
48
+ end
49
+
50
+ def initialize(obj, options = {})
51
+ #do nothing
52
+ end
53
+
54
+ def representation
55
+ "pleft!"
56
+ end
57
+ end
58
+ class TestOverwrittenHelper
59
+ extend OverwrittenHelperBuilder.helper
60
+ end
61
+
62
+ class Tokamak::Builder::HelperTest < Test::Unit::TestCase
63
+
64
+ def test_default_helper
65
+ obj = { :foo => "bar" }
66
+ a_collection = [1,2,3,4]
67
+ json = TestDefaultHelper.collection(obj) do |collection|
68
+ collection.values do |values|
69
+ values.id "an_id"
70
+ end
71
+
72
+ collection.members(:collection => a_collection) do |member, number|
73
+ member.values do |values|
74
+ values.id number
75
+ end
76
+ end
77
+ end
78
+
79
+ hash = JSON.parse(json).extend(Methodize)
80
+
81
+ assert_equal "an_id", hash.id
82
+ assert_equal 1 , hash.members.first.id
83
+ assert_equal 4 , hash.members.size
84
+ end
85
+
86
+ def test_helper_with_options_overwritten
87
+ numbers = [1,2,3,4,5]
88
+ result = AtomGenerator.collection(numbers, :other_option => "an_option") do |collection, num, opt|
89
+ assert_equal :feed , opt[:atom_type]
90
+ assert_equal "an_option", opt[:other_option]
91
+ assert_equal [1,2,3,4,5], num
92
+ end
93
+ assert_equal "puft!", result
94
+ end
95
+
96
+ def test_overwritten_helper
97
+ numbers = [1,2,3,4,5]
98
+ result = TestOverwrittenHelper.collection(numbers, :other_option => "an_option") do |collection, num, opt|
99
+ assert_equal "an_option", opt[:other_option]
100
+ assert_equal "my_value" , opt[:my_option]
101
+ assert_equal [1,2,3,4,5], num
102
+ end
103
+ assert_equal "pleft!", result
104
+ end
105
+ end
106
+
@@ -0,0 +1,74 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'ruby-debug'
4
+ require "methodize"
5
+ require "rack/conneg"
6
+
7
+ require File.expand_path(File.dirname(__FILE__) + '/../../rails2_skel/config/environment.rb')
8
+
9
+ # put the require below to use tokamak in your rails project
10
+ require "tokamak/hook/rails"
11
+
12
+ class Tokamak::Hook::RailsTest < ActionController::IntegrationTest
13
+
14
+ def test_view_generation_with_json
15
+ get '/test/show', {}, :accept => 'application/json'
16
+
17
+ json = @controller.response.body
18
+ hash = JSON.parse(json).extend(Methodize)
19
+
20
+ assert_equal "John Doe" , hash.author.first.name
21
+ assert_equal "foobar@example.com" , hash.author.last.email
22
+ assert_equal "http://example.com/json", hash.id
23
+
24
+ assert_equal "http://a.link.com/next" , hash.link.first.href
25
+ assert_equal "next" , hash.link.first.rel
26
+ assert_equal "application/json" , hash.link.last.type
27
+
28
+ assert_equal "uri:1" , hash.articles.first.id
29
+ assert_equal "a great article" , hash.articles.first.title
30
+ assert_equal "http://example.com/image/1" , hash.articles.last.link.first.href
31
+ assert_equal "image" , hash.articles.last.link.first.rel
32
+ assert_equal "application/json" , hash.articles.last.link.last.type
33
+ end
34
+
35
+ def test_view_generation_with_xml
36
+ get '/test/show', {}, :accept => 'application/xml'
37
+
38
+ xml = @controller.response.body
39
+ xml = Nokogiri::XML::Document.parse(xml)
40
+
41
+ assert_equal "John Doe" , xml.css("root > author").first.css("name").first.text
42
+ assert_equal "foobar@example.com" , xml.css("root > author").last.css("email").first.text
43
+
44
+ assert_equal "http://a.link.com/next" , xml.css("root > link").first["href"]
45
+ assert_equal "next" , xml.css("root > link").first["rel"]
46
+ assert_equal "application/xml" , xml.css("root > link").last["type"]
47
+
48
+ assert_equal "uri:1" , xml.css("root > articles").first.css("id").first.text
49
+ assert_equal "a great article" , xml.css("root > articles").first.css("title").first.text
50
+ assert_equal "http://example.com/image/1" , xml.css("root > articles").first.css("link").first["href"]
51
+ assert_equal "image" , xml.css("root > articles").first.css("link").first["rel"]
52
+ assert_equal "application/json" , xml.css("root > articles").first.css("link").last["type"]
53
+ end
54
+
55
+ def test_view_generation_with_partial
56
+ get '/test/feed', {}, :accept => 'application/xml'
57
+
58
+ xml = @controller.response.body
59
+ xml = Nokogiri::XML::Document.parse(xml)
60
+
61
+ assert_equal "John Doe" , xml.css("root > author").first.css("name").first.text
62
+ assert_equal "foobar@example.com" , xml.css("root > author").last.css("email").first.text
63
+
64
+ assert_equal "http://a.link.com/next" , xml.css("root > link").first["href"]
65
+ assert_equal "next" , xml.css("root > link").first["rel"]
66
+ assert_equal "application/xml" , xml.css("root > link").last["type"]
67
+
68
+ assert_equal "uri:1" , xml.css("root > articles").first.css("id").first.text
69
+ assert_equal "a great article" , xml.css("root > articles").first.css("title").first.text
70
+ assert_equal "http://example.com/image/1" , xml.css("root > articles").first.css("link").first["href"]
71
+ assert_equal "image" , xml.css("root > articles").first.css("link").first["rel"]
72
+ assert_equal "application/json" , xml.css("root > articles").first.css("link").last["type"]
73
+ end
74
+ end