acts_as_api_sequel 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +13 -0
  5. data/History.txt +108 -0
  6. data/README.md +126 -0
  7. data/Rakefile +48 -0
  8. data/acts_as_api.gemspec +28 -0
  9. data/examples/introduction/docco.css +186 -0
  10. data/examples/introduction/index.html +338 -0
  11. data/examples/introduction/index.rb +144 -0
  12. data/examples/introduction/layout.mustache +67 -0
  13. data/lib/acts_as_api.rb +46 -0
  14. data/lib/acts_as_api/adapters.rb +5 -0
  15. data/lib/acts_as_api/adapters/mongoid.rb +11 -0
  16. data/lib/acts_as_api/api_template.rb +139 -0
  17. data/lib/acts_as_api/array.rb +21 -0
  18. data/lib/acts_as_api/base.rb +88 -0
  19. data/lib/acts_as_api/config.rb +58 -0
  20. data/lib/acts_as_api/exceptions.rb +4 -0
  21. data/lib/acts_as_api/rails_renderer.rb +17 -0
  22. data/lib/acts_as_api/rendering.rb +95 -0
  23. data/lib/acts_as_api/responder.rb +39 -0
  24. data/lib/acts_as_api/version.rb +3 -0
  25. data/spec/README.md +55 -0
  26. data/spec/active_record_dummy/.gitignore +15 -0
  27. data/spec/active_record_dummy/Gemfile +16 -0
  28. data/spec/active_record_dummy/README.rdoc +261 -0
  29. data/spec/active_record_dummy/Rakefile +7 -0
  30. data/spec/active_record_dummy/app/assets/images/rails.png +0 -0
  31. data/spec/active_record_dummy/app/assets/javascripts/application.js +15 -0
  32. data/spec/active_record_dummy/app/assets/stylesheets/application.css +13 -0
  33. data/spec/active_record_dummy/app/controllers/application_controller.rb +3 -0
  34. data/spec/active_record_dummy/app/helpers/application_helper.rb +2 -0
  35. data/spec/active_record_dummy/app/mailers/.gitkeep +0 -0
  36. data/spec/active_record_dummy/app/models/.gitkeep +0 -0
  37. data/spec/active_record_dummy/app/models/profile.rb +4 -0
  38. data/spec/active_record_dummy/app/models/task.rb +4 -0
  39. data/spec/active_record_dummy/app/models/untouched.rb +2 -0
  40. data/spec/active_record_dummy/app/models/user.rb +186 -0
  41. data/spec/active_record_dummy/app/views/layouts/application.html.erb +14 -0
  42. data/spec/active_record_dummy/config.ru +4 -0
  43. data/spec/active_record_dummy/config/application.rb +59 -0
  44. data/spec/active_record_dummy/config/boot.rb +6 -0
  45. data/spec/active_record_dummy/config/database.yml +25 -0
  46. data/spec/active_record_dummy/config/environment.rb +5 -0
  47. data/spec/active_record_dummy/config/environments/development.rb +37 -0
  48. data/spec/active_record_dummy/config/environments/production.rb +67 -0
  49. data/spec/active_record_dummy/config/environments/test.rb +37 -0
  50. data/spec/active_record_dummy/config/initializers/backtrace_silencers.rb +7 -0
  51. data/spec/active_record_dummy/config/initializers/generators.rb +2 -0
  52. data/spec/active_record_dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/active_record_dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/active_record_dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/active_record_dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/active_record_dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/active_record_dummy/config/locales/en.yml +5 -0
  58. data/spec/active_record_dummy/config/routes.rb +59 -0
  59. data/spec/active_record_dummy/db/migrate/20110214201640_create_tables.rb +44 -0
  60. data/spec/active_record_dummy/db/schema.rb +47 -0
  61. data/spec/active_record_dummy/db/seeds.rb +7 -0
  62. data/spec/active_record_dummy/doc/README_FOR_APP +2 -0
  63. data/spec/active_record_dummy/lib/assets/.gitkeep +0 -0
  64. data/spec/active_record_dummy/lib/tasks/.gitkeep +0 -0
  65. data/spec/active_record_dummy/log/.gitkeep +0 -0
  66. data/spec/active_record_dummy/public/404.html +26 -0
  67. data/spec/active_record_dummy/public/422.html +26 -0
  68. data/spec/active_record_dummy/public/500.html +25 -0
  69. data/spec/active_record_dummy/public/favicon.ico +0 -0
  70. data/spec/active_record_dummy/public/index.html +241 -0
  71. data/spec/active_record_dummy/public/robots.txt +5 -0
  72. data/spec/active_record_dummy/script/rails +6 -0
  73. data/spec/active_record_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  74. data/spec/active_record_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  75. data/spec/active_record_dummy/vendor/plugins/.gitkeep +0 -0
  76. data/spec/controllers/plain_objects_controller_spec.rb +36 -0
  77. data/spec/controllers/respond_with_users_controller_spec.rb +142 -0
  78. data/spec/controllers/users_controller_spec.rb +15 -0
  79. data/spec/models/model_spec.rb +31 -0
  80. data/spec/mongoid_dummy/.gitignore +15 -0
  81. data/spec/mongoid_dummy/Gemfile +18 -0
  82. data/spec/mongoid_dummy/README.rdoc +261 -0
  83. data/spec/mongoid_dummy/Rakefile +7 -0
  84. data/spec/mongoid_dummy/app/assets/images/rails.png +0 -0
  85. data/spec/mongoid_dummy/app/assets/javascripts/application.js +15 -0
  86. data/spec/mongoid_dummy/app/assets/stylesheets/application.css +13 -0
  87. data/spec/mongoid_dummy/app/controllers/application_controller.rb +3 -0
  88. data/spec/mongoid_dummy/app/helpers/application_helper.rb +2 -0
  89. data/spec/mongoid_dummy/app/mailers/.gitkeep +0 -0
  90. data/spec/mongoid_dummy/app/models/.gitkeep +0 -0
  91. data/spec/mongoid_dummy/app/models/profile.rb +10 -0
  92. data/spec/mongoid_dummy/app/models/task.rb +12 -0
  93. data/spec/mongoid_dummy/app/models/untouched.rb +7 -0
  94. data/spec/mongoid_dummy/app/models/user.rb +48 -0
  95. data/spec/mongoid_dummy/app/views/layouts/application.html.erb +14 -0
  96. data/spec/mongoid_dummy/config.ru +4 -0
  97. data/spec/mongoid_dummy/config/application.rb +65 -0
  98. data/spec/mongoid_dummy/config/boot.rb +6 -0
  99. data/spec/mongoid_dummy/config/environment.rb +5 -0
  100. data/spec/mongoid_dummy/config/environments/development.rb +31 -0
  101. data/spec/mongoid_dummy/config/environments/production.rb +64 -0
  102. data/spec/mongoid_dummy/config/environments/test.rb +35 -0
  103. data/spec/mongoid_dummy/config/initializers/backtrace_silencers.rb +7 -0
  104. data/spec/mongoid_dummy/config/initializers/generators.rb +2 -0
  105. data/spec/mongoid_dummy/config/initializers/include_acts_as_api.rb +3 -0
  106. data/spec/mongoid_dummy/config/initializers/inflections.rb +15 -0
  107. data/spec/mongoid_dummy/config/initializers/mime_types.rb +5 -0
  108. data/spec/mongoid_dummy/config/initializers/secret_token.rb +7 -0
  109. data/spec/mongoid_dummy/config/initializers/session_store.rb +8 -0
  110. data/spec/mongoid_dummy/config/initializers/wrap_parameters.rb +10 -0
  111. data/spec/mongoid_dummy/config/locales/en.yml +5 -0
  112. data/spec/mongoid_dummy/config/mongoid.yml +120 -0
  113. data/spec/mongoid_dummy/config/routes.rb +59 -0
  114. data/spec/mongoid_dummy/db/seeds.rb +7 -0
  115. data/spec/mongoid_dummy/doc/README_FOR_APP +2 -0
  116. data/spec/mongoid_dummy/lib/assets/.gitkeep +0 -0
  117. data/spec/mongoid_dummy/lib/tasks/.gitkeep +0 -0
  118. data/spec/mongoid_dummy/log/.gitkeep +0 -0
  119. data/spec/mongoid_dummy/public/404.html +26 -0
  120. data/spec/mongoid_dummy/public/422.html +26 -0
  121. data/spec/mongoid_dummy/public/500.html +25 -0
  122. data/spec/mongoid_dummy/public/favicon.ico +0 -0
  123. data/spec/mongoid_dummy/public/index.html +241 -0
  124. data/spec/mongoid_dummy/public/robots.txt +5 -0
  125. data/spec/mongoid_dummy/script/rails +6 -0
  126. data/spec/mongoid_dummy/vendor/assets/javascripts/.gitkeep +0 -0
  127. data/spec/mongoid_dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  128. data/spec/mongoid_dummy/vendor/plugins/.gitkeep +0 -0
  129. data/spec/shared_engine/.gitignore +7 -0
  130. data/spec/shared_engine/Gemfile +14 -0
  131. data/spec/shared_engine/MIT-LICENSE +20 -0
  132. data/spec/shared_engine/README.rdoc +3 -0
  133. data/spec/shared_engine/Rakefile +29 -0
  134. data/spec/shared_engine/app/assets/images/shared_engine/.gitkeep +0 -0
  135. data/spec/shared_engine/app/assets/javascripts/shared_engine/application.js +15 -0
  136. data/spec/shared_engine/app/assets/stylesheets/shared_engine/application.css +13 -0
  137. data/spec/shared_engine/app/controllers/shared_engine/application_controller.rb +4 -0
  138. data/spec/shared_engine/app/controllers/shared_engine/plain_objects_controller.rb +14 -0
  139. data/spec/shared_engine/app/controllers/shared_engine/respond_with_users_controller.rb +64 -0
  140. data/spec/shared_engine/app/controllers/shared_engine/users_controller.rb +71 -0
  141. data/spec/shared_engine/app/helpers/shared_engine/application_helper.rb +4 -0
  142. data/spec/shared_engine/app/models/plain_object.rb +17 -0
  143. data/spec/shared_engine/app/models/user_template.rb +154 -0
  144. data/spec/shared_engine/app/views/layouts/shared_engine/application.html.erb +14 -0
  145. data/spec/shared_engine/config/routes.rb +28 -0
  146. data/spec/shared_engine/dummy/README.rdoc +261 -0
  147. data/spec/shared_engine/dummy/Rakefile +7 -0
  148. data/spec/shared_engine/dummy/app/assets/javascripts/application.js +15 -0
  149. data/spec/shared_engine/dummy/app/assets/stylesheets/application.css +13 -0
  150. data/spec/shared_engine/dummy/app/controllers/application_controller.rb +3 -0
  151. data/spec/shared_engine/dummy/app/helpers/application_helper.rb +2 -0
  152. data/spec/shared_engine/dummy/app/mailers/.gitkeep +0 -0
  153. data/spec/shared_engine/dummy/app/models/.gitkeep +0 -0
  154. data/spec/shared_engine/dummy/app/views/layouts/application.html.erb +14 -0
  155. data/spec/shared_engine/dummy/config.ru +4 -0
  156. data/spec/shared_engine/dummy/config/application.rb +62 -0
  157. data/spec/shared_engine/dummy/config/boot.rb +10 -0
  158. data/spec/shared_engine/dummy/config/database.yml +25 -0
  159. data/spec/shared_engine/dummy/config/environment.rb +5 -0
  160. data/spec/shared_engine/dummy/config/environments/development.rb +37 -0
  161. data/spec/shared_engine/dummy/config/environments/production.rb +67 -0
  162. data/spec/shared_engine/dummy/config/environments/test.rb +37 -0
  163. data/spec/shared_engine/dummy/config/initializers/backtrace_silencers.rb +7 -0
  164. data/spec/shared_engine/dummy/config/initializers/inflections.rb +15 -0
  165. data/spec/shared_engine/dummy/config/initializers/mime_types.rb +5 -0
  166. data/spec/shared_engine/dummy/config/initializers/secret_token.rb +7 -0
  167. data/spec/shared_engine/dummy/config/initializers/session_store.rb +8 -0
  168. data/spec/shared_engine/dummy/config/initializers/wrap_parameters.rb +14 -0
  169. data/spec/shared_engine/dummy/config/locales/en.yml +5 -0
  170. data/spec/shared_engine/dummy/config/routes.rb +4 -0
  171. data/spec/shared_engine/dummy/lib/assets/.gitkeep +0 -0
  172. data/spec/shared_engine/dummy/log/.gitkeep +0 -0
  173. data/spec/shared_engine/dummy/public/404.html +26 -0
  174. data/spec/shared_engine/dummy/public/422.html +26 -0
  175. data/spec/shared_engine/dummy/public/500.html +25 -0
  176. data/spec/shared_engine/dummy/public/favicon.ico +0 -0
  177. data/spec/shared_engine/dummy/script/rails +6 -0
  178. data/spec/shared_engine/lib/magic/rails/engine.rb +69 -0
  179. data/spec/shared_engine/lib/shared_engine.rb +4 -0
  180. data/spec/shared_engine/lib/shared_engine/engine.rb +5 -0
  181. data/spec/shared_engine/lib/shared_engine/version.rb +3 -0
  182. data/spec/shared_engine/lib/tasks/shared_engine_tasks.rake +4 -0
  183. data/spec/shared_engine/script/rails +8 -0
  184. data/spec/shared_engine/shared_engine.gemspec +22 -0
  185. data/spec/spec.opts +1 -0
  186. data/spec/spec_helper.rb +22 -0
  187. data/spec/support/api_test_helpers.rb +23 -0
  188. data/spec/support/controller_examples.rb +458 -0
  189. data/spec/support/it_supports.rb +3 -0
  190. data/spec/support/model_examples/associations.rb +272 -0
  191. data/spec/support/model_examples/callbacks.rb +38 -0
  192. data/spec/support/model_examples/closures.rb +49 -0
  193. data/spec/support/model_examples/conditional_if.rb +165 -0
  194. data/spec/support/model_examples/conditional_unless.rb +165 -0
  195. data/spec/support/model_examples/enabled.rb +10 -0
  196. data/spec/support/model_examples/extending.rb +112 -0
  197. data/spec/support/model_examples/methods.rb +23 -0
  198. data/spec/support/model_examples/options.rb +53 -0
  199. data/spec/support/model_examples/renaming.rb +50 -0
  200. data/spec/support/model_examples/simple.rb +23 -0
  201. data/spec/support/model_examples/sub_nodes.rb +105 -0
  202. data/spec/support/model_examples/undefined.rb +7 -0
  203. data/spec/support/model_examples/untouched.rb +13 -0
  204. data/spec/support/routing.rb +4 -0
  205. data/spec/support/simple_fixtures.rb +59 -0
  206. metadata +499 -0
@@ -0,0 +1,3 @@
1
+ RSpec.configure do |c|
2
+ c.alias_it_should_behave_like_to :it_supports, 'supports:'
3
+ end
@@ -0,0 +1,272 @@
1
+ shared_examples_for "including an association in the api template" do
2
+
3
+ describe "which doesn't acts_as_api" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:include_tasks)
7
+ end
8
+
9
+ it "returns a hash" do
10
+ @response.should be_kind_of(Hash)
11
+ end
12
+
13
+ it "returns the correct number of fields" do
14
+ @response.should have(1).keys
15
+ end
16
+
17
+ it "returns all specified fields" do
18
+ @response.keys.should include(:tasks)
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response[:tasks].should be_an Array
23
+ @response[:tasks].should have(3).tasks
24
+ end
25
+
26
+ it "should contain the associated sub models" do
27
+ @response[:tasks].should include(@destroy_deathstar, @study_with_yoda, @win_rebellion)
28
+ end
29
+ end
30
+
31
+ describe "which does acts_as_api" do
32
+
33
+ context "has_many" do
34
+
35
+ before(:each) do
36
+ Task.acts_as_api
37
+ Task.api_accessible :include_tasks do |t|
38
+ t.add :heading
39
+ t.add :done
40
+ end
41
+ @response = @luke.as_api_response(:include_tasks)
42
+ end
43
+
44
+ it "returns a hash" do
45
+ @response.should be_kind_of(Hash)
46
+ end
47
+
48
+ it "returns the correct number of fields" do
49
+ @response.should have(1).keys
50
+ end
51
+
52
+ it "returns all specified fields" do
53
+ @response.keys.should include(:tasks)
54
+ end
55
+
56
+ it "returns the correct values for the specified fields" do
57
+ @response[:tasks].should be_an Array
58
+ @response[:tasks].should have(3).tasks
59
+ end
60
+
61
+ it "contains the associated child models with the determined api template" do
62
+ @response[:tasks].each do |task|
63
+ task.keys.should include(:heading, :done)
64
+ task.keys.should have(2).attributes
65
+ end
66
+ end
67
+
68
+ it "contains the correct data of the child models" do
69
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :done => t.done, :heading => t.heading } }
70
+ @response[:tasks].should eql task_hash
71
+ end
72
+ end
73
+
74
+ context "has_one" do
75
+
76
+ before(:each) do
77
+ Profile.acts_as_api
78
+ Profile.api_accessible :include_profile do |t|
79
+ t.add :avatar
80
+ t.add :homepage
81
+ end
82
+ @response = @luke.as_api_response(:include_profile)
83
+ end
84
+
85
+ it "returns a hash" do
86
+ @response.should be_kind_of(Hash)
87
+ end
88
+
89
+ it "returns the correct number of fields" do
90
+ @response.should have(1).keys
91
+ end
92
+
93
+ it "returns all specified fields" do
94
+ @response.keys.should include(:profile)
95
+ end
96
+
97
+ it "returns the correct values for the specified fields" do
98
+ @response[:profile].should be_a Hash
99
+ @response[:profile].should have(2).attributes
100
+ end
101
+
102
+ it "contains the associated child models with the determined api template" do
103
+ @response[:profile].keys.should include(:avatar, :homepage)
104
+ end
105
+
106
+ it "contains the correct data of the child models" do
107
+ profile_hash = { :avatar => @luke.profile.avatar, :homepage => @luke.profile.homepage }
108
+ @response[:profile].should eql profile_hash
109
+ end
110
+
111
+ end
112
+ end
113
+
114
+ describe "which does acts_as_api, but with using another template name" do
115
+
116
+ before(:each) do
117
+ Task.acts_as_api
118
+ Task.api_accessible :other_template do |t|
119
+ t.add :description
120
+ t.add :time_spent
121
+ end
122
+ @response = @luke.as_api_response(:other_sub_template)
123
+ end
124
+
125
+ it "returns a hash" do
126
+ @response.should be_kind_of(Hash)
127
+ end
128
+
129
+ it "returns the correct number of fields" do
130
+ @response.should have(2).keys
131
+ end
132
+
133
+ it "returns all specified fields" do
134
+ @response.keys.should include(:first_name)
135
+ end
136
+
137
+ it "returns the correct values for the specified fields" do
138
+ @response.values.should include(@luke.first_name)
139
+ end
140
+
141
+ it "returns all specified fields" do
142
+ @response.keys.should include(:tasks)
143
+ end
144
+
145
+ it "returns the correct values for the specified fields" do
146
+ @response[:tasks].should be_an Array
147
+ @response[:tasks].should have(3).tasks
148
+ end
149
+
150
+ it "contains the associated child models with the determined api template" do
151
+ @response[:tasks].each do |task|
152
+ task.keys.should include(:description, :time_spent)
153
+ task.keys.should have(2).attributes
154
+ end
155
+ end
156
+
157
+ it "contains the correct data of the child models" do
158
+ task_hash = [ @destroy_deathstar, @study_with_yoda, @win_rebellion ].collect{|t| { :description => t.description, :time_spent => t.time_spent } }
159
+ @response[:tasks].should eql task_hash
160
+ end
161
+ end
162
+
163
+ describe "that is scoped" do
164
+
165
+ before(:each) do
166
+ # extend task model with scope
167
+ #class Task < ActiveRecord::Base
168
+ Task.class_eval do
169
+ scope :completed, where(:done => true)
170
+ end
171
+ Task.acts_as_api
172
+ Task.api_accessible :include_completed_tasks do |t|
173
+ t.add :heading
174
+ t.add :done
175
+ end
176
+
177
+ @response = @luke.as_api_response(:include_completed_tasks)
178
+ end
179
+
180
+ it "returns a hash" do
181
+ @response.should be_kind_of(Hash)
182
+ end
183
+
184
+ it "returns the correct number of fields" do
185
+ @response.should have(1).keys
186
+ end
187
+
188
+ it "returns all specified fields" do
189
+ @response.keys.should include(:completed_tasks)
190
+ end
191
+
192
+ it "returns the correct values for the specified fields" do
193
+ @response[:completed_tasks].should be_an Array
194
+ @response[:completed_tasks].should have(2).tasks
195
+ end
196
+
197
+ it "contains the associated child models with the determined api template" do
198
+ @response[:completed_tasks].each do |task|
199
+ task.keys.should include(:heading, :done)
200
+ task.keys.should have(2).attributes
201
+ end
202
+ end
203
+
204
+ it "contains the correct data of the child models" do
205
+ task_hash = [ @destroy_deathstar, @study_with_yoda ].collect{|t| { :done => t.done, :heading => t.heading } }
206
+ @response[:completed_tasks].should eql task_hash
207
+ end
208
+ end
209
+
210
+ describe "handling nil values" do
211
+
212
+ context "has_many" do
213
+
214
+ before(:each) do
215
+ Task.acts_as_api
216
+ Task.api_accessible :include_tasks do |t|
217
+ t.add :heading
218
+ t.add :done
219
+ end
220
+ @response = @han.as_api_response(:include_tasks)
221
+ end
222
+
223
+ it "returns a hash" do
224
+ @response.should be_kind_of(Hash)
225
+ end
226
+
227
+ it "returns the correct number of fields" do
228
+ @response.should have(1).keys
229
+ end
230
+
231
+ it "returns all specified fields" do
232
+ @response.keys.should include(:tasks)
233
+ end
234
+
235
+ it "returns the correct values for the specified fields" do
236
+ @response[:tasks].should be_kind_of(Array)
237
+ end
238
+
239
+ it "contains no associated child models" do
240
+ @response[:tasks].should have(0).items
241
+ end
242
+
243
+ end
244
+
245
+ context "has one" do
246
+ before(:each) do
247
+ Profile.acts_as_api
248
+ Profile.api_accessible :include_profile do |t|
249
+ t.add :avatar
250
+ t.add :homepage
251
+ end
252
+ @response = @han.as_api_response(:include_profile)
253
+ end
254
+
255
+ it "returns a hash" do
256
+ @response.should be_kind_of(Hash)
257
+ end
258
+
259
+ it "returns the correct number of fields" do
260
+ @response.should have(1).keys
261
+ end
262
+
263
+ it "returns all specified fields" do
264
+ @response.keys.should include(:profile)
265
+ end
266
+
267
+ it "returns nil for the association" do
268
+ @response[:profile].should be_nil
269
+ end
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,38 @@
1
+ shared_examples_for "defining a model callback" do
2
+
3
+ describe "for a" do
4
+
5
+ describe "around_api_response" do
6
+
7
+ it "skips rendering if not yielded" do
8
+ @luke.skip_api_response = true
9
+ @luke.as_api_response(:name_only).keys.should include(:skipped)
10
+ end
11
+
12
+ it "renders if yielded" do
13
+ @luke.as_api_response(:name_only).keys.should_not include(:skipped)
14
+ end
15
+
16
+ end
17
+
18
+ describe "before_api_response" do
19
+
20
+ it "is called properly" do
21
+ @luke.as_api_response(:name_only)
22
+ @luke.before_api_response_called?.should eql(true)
23
+ end
24
+
25
+ end
26
+
27
+ describe "after_api_response" do
28
+
29
+ it "is called properly" do
30
+ @luke.as_api_response(:name_only)
31
+ @luke.after_api_response_called?.should eql(true)
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,49 @@
1
+ shared_examples_for "calling a closure in the api template" do
2
+
3
+ describe "i.e. a proc (the record is passed as only parameter)" do
4
+
5
+ before(:each) do
6
+ @response = @luke.as_api_response(:calling_a_proc)
7
+ end
8
+
9
+ it "returns a hash" do
10
+ @response.should be_kind_of(Hash)
11
+ end
12
+
13
+ it "returns the correct number of fields" do
14
+ @response.should have(2).keys
15
+ end
16
+
17
+ it "returns all specified fields" do
18
+ @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
19
+ end
20
+
21
+ it "returns the correct values for the specified fields" do
22
+ @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
23
+ end
24
+ end
25
+
26
+ describe "i.e. a lambda (the record is passed as only parameter)" do
27
+
28
+ before(:each) do
29
+ @response = @luke.as_api_response(:calling_a_lambda)
30
+ end
31
+
32
+ it "returns a hash" do
33
+ @response.should be_kind_of(Hash)
34
+ end
35
+
36
+ it "returns the correct number of fields" do
37
+ @response.should have(2).keys
38
+ end
39
+
40
+ it "returns all specified fields" do
41
+ @response.keys.sort_by(&:to_s).should eql([:all_caps_name, :without_param])
42
+ end
43
+
44
+ it "returns the correct values for the specified fields" do
45
+ @response.values.sort.should eql(["LUKE SKYWALKER", "Time"])
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,165 @@
1
+ shared_examples_for "conditional if statements" do
2
+
3
+ describe "using the :if option" do
4
+
5
+ describe "passing a symbol" do
6
+
7
+ describe "that returns false" do
8
+
9
+ before(:each) do
10
+ @response = @luke.as_api_response(:if_over_thirty)
11
+ end
12
+
13
+ it "returns a hash" do
14
+ @response.should be_kind_of(Hash)
15
+ end
16
+
17
+ it "returns the correct number of fields" do
18
+ @response.should have(1).keys
19
+ end
20
+
21
+ it "won't add the conditional field but all others" do
22
+ @response.keys.should include(:first_name)
23
+ @response.keys.should_not include(:full_name)
24
+ end
25
+
26
+ it "the other specified fields have the correct value" do
27
+ @response.values.should include(@luke.first_name)
28
+ end
29
+
30
+ end
31
+
32
+ describe "that returns nil" do
33
+
34
+ before(:each) do
35
+ @response = @luke.as_api_response(:if_returns_nil)
36
+ end
37
+
38
+ it "returns a hash" do
39
+ @response.should be_kind_of(Hash)
40
+ end
41
+
42
+ it "returns the correct number of fields" do
43
+ @response.should have(1).keys
44
+ end
45
+
46
+ it "won't add the conditional field but all others" do
47
+ @response.keys.should include(:first_name)
48
+ @response.keys.should_not include(:full_name)
49
+ end
50
+
51
+ it "the other specified fields have the correct value" do
52
+ @response.values.should include(@luke.first_name)
53
+ end
54
+
55
+ end
56
+
57
+ describe "that returns true" do
58
+
59
+ before(:each) do
60
+ @response = @han.as_api_response(:if_over_thirty)
61
+ end
62
+
63
+ it "returns a hash" do
64
+ @response.should be_kind_of(Hash)
65
+ end
66
+
67
+ it "returns the correct number of fields" do
68
+ @response.should have(2).keys
69
+ end
70
+
71
+ it "won't add the conditional field but all others" do
72
+ @response.keys.should include(:first_name)
73
+ @response.keys.should include(:last_name)
74
+ end
75
+
76
+ it "the other specified fields have the correct value" do
77
+ @response.values.should include(@han.first_name, @han.last_name)
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+
86
+ describe "passing a proc" do
87
+
88
+ describe "that returns false" do
89
+
90
+ before(:each) do
91
+ @response = @luke.as_api_response(:if_over_thirty_proc)
92
+ end
93
+
94
+ it "returns a hash" do
95
+ @response.should be_kind_of(Hash)
96
+ end
97
+
98
+ it "returns the correct number of fields" do
99
+ @response.should have(1).keys
100
+ end
101
+
102
+ it "won't add the conditional field but all others" do
103
+ @response.keys.should include(:first_name)
104
+ @response.keys.should_not include(:full_name)
105
+ end
106
+
107
+ it "the other specified fields have the correct value" do
108
+ @response.values.should include(@luke.first_name)
109
+ end
110
+
111
+ end
112
+
113
+ describe "that returns nil" do
114
+
115
+ before(:each) do
116
+ @response = @luke.as_api_response(:if_returns_nil_proc)
117
+ end
118
+
119
+ it "returns a hash" do
120
+ @response.should be_kind_of(Hash)
121
+ end
122
+
123
+ it "returns the correct number of fields" do
124
+ @response.should have(1).keys
125
+ end
126
+
127
+ it "won't add the conditional field but all others" do
128
+ @response.keys.should include(:first_name)
129
+ @response.keys.should_not include(:full_name)
130
+ end
131
+
132
+ it "the other specified fields have the correct value" do
133
+ @response.values.should include(@luke.first_name)
134
+ end
135
+
136
+ end
137
+
138
+ describe "that returns true" do
139
+
140
+ before(:each) do
141
+ @response = @han.as_api_response(:if_over_thirty_proc)
142
+ end
143
+
144
+ it "returns a hash" do
145
+ @response.should be_kind_of(Hash)
146
+ end
147
+
148
+ it "returns the correct number of fields" do
149
+ @response.should have(2).keys
150
+ end
151
+
152
+ it "won't add the conditional field but all others" do
153
+ @response.keys.should include(:first_name)
154
+ @response.keys.should include(:last_name)
155
+ end
156
+
157
+ it "the other specified fields have the correct value" do
158
+ @response.values.should include(@han.first_name, @han.last_name)
159
+ end
160
+
161
+ end
162
+
163
+ end
164
+ end
165
+