morpheus 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/.rvmrc +1 -0
  2. data/Gemfile +21 -0
  3. data/Gemfile.lock +134 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.rdoc +44 -0
  6. data/Rakefile +48 -0
  7. data/VERSION +1 -0
  8. data/autotest/discover.rb +7 -0
  9. data/lib/ext/typhoeus.rb +37 -0
  10. data/lib/morpheus/associations/association.rb +110 -0
  11. data/lib/morpheus/associations/belongs_to_association.rb +45 -0
  12. data/lib/morpheus/associations/has_many_association.rb +70 -0
  13. data/lib/morpheus/associations/has_one_association.rb +46 -0
  14. data/lib/morpheus/base.rb +66 -0
  15. data/lib/morpheus/client/associations.rb +47 -0
  16. data/lib/morpheus/client/inflections.rb +3 -0
  17. data/lib/morpheus/client/log_subscriber.rb +64 -0
  18. data/lib/morpheus/client/railtie.rb +25 -0
  19. data/lib/morpheus/configuration.rb +49 -0
  20. data/lib/morpheus/errors.rb +32 -0
  21. data/lib/morpheus/filter.rb +18 -0
  22. data/lib/morpheus/mixins/associations.rb +55 -0
  23. data/lib/morpheus/mixins/attributes.rb +133 -0
  24. data/lib/morpheus/mixins/conversion.rb +21 -0
  25. data/lib/morpheus/mixins/filtering.rb +18 -0
  26. data/lib/morpheus/mixins/finders.rb +58 -0
  27. data/lib/morpheus/mixins/introspection.rb +25 -0
  28. data/lib/morpheus/mixins/persistence.rb +46 -0
  29. data/lib/morpheus/mixins/reflections.rb +24 -0
  30. data/lib/morpheus/mixins/request_handling.rb +34 -0
  31. data/lib/morpheus/mixins/response_parsing.rb +27 -0
  32. data/lib/morpheus/mixins/url_support.rb +36 -0
  33. data/lib/morpheus/mock.rb +66 -0
  34. data/lib/morpheus/reflection.rb +22 -0
  35. data/lib/morpheus/relation.rb +57 -0
  36. data/lib/morpheus/request.rb +41 -0
  37. data/lib/morpheus/request_cache.rb +18 -0
  38. data/lib/morpheus/request_queue.rb +44 -0
  39. data/lib/morpheus/response.rb +24 -0
  40. data/lib/morpheus/response_parser.rb +80 -0
  41. data/lib/morpheus/type_caster.rb +80 -0
  42. data/lib/morpheus/url_builder.rb +52 -0
  43. data/lib/morpheus.rb +64 -0
  44. data/morpheus.gemspec +191 -0
  45. data/spec/dummy/Rakefile +7 -0
  46. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  47. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  48. data/spec/dummy/app/models/purchase.rb +3 -0
  49. data/spec/dummy/app/resources/attendee.rb +2 -0
  50. data/spec/dummy/app/resources/author.rb +5 -0
  51. data/spec/dummy/app/resources/automobile.rb +6 -0
  52. data/spec/dummy/app/resources/book.rb +5 -0
  53. data/spec/dummy/app/resources/conference.rb +3 -0
  54. data/spec/dummy/app/resources/dog.rb +10 -0
  55. data/spec/dummy/app/resources/item.rb +5 -0
  56. data/spec/dummy/app/resources/meeting.rb +7 -0
  57. data/spec/dummy/app/resources/speaker.rb +3 -0
  58. data/spec/dummy/app/resources/state.rb +5 -0
  59. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  60. data/spec/dummy/config/application.rb +45 -0
  61. data/spec/dummy/config/boot.rb +10 -0
  62. data/spec/dummy/config/database.yml +22 -0
  63. data/spec/dummy/config/environment.rb +5 -0
  64. data/spec/dummy/config/environments/development.rb +26 -0
  65. data/spec/dummy/config/environments/production.rb +49 -0
  66. data/spec/dummy/config/environments/test.rb +35 -0
  67. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  68. data/spec/dummy/config/initializers/inflections.rb +10 -0
  69. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  70. data/spec/dummy/config/initializers/morpheus.rb +3 -0
  71. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  72. data/spec/dummy/config/initializers/session_store.rb +8 -0
  73. data/spec/dummy/config/locales/en.yml +5 -0
  74. data/spec/dummy/config/routes.rb +58 -0
  75. data/spec/dummy/config.ru +4 -0
  76. data/spec/dummy/db/migrate/20110605002144_create_purchases.rb +13 -0
  77. data/spec/dummy/db/test.sqlite3 +0 -0
  78. data/spec/dummy/public/404.html +26 -0
  79. data/spec/dummy/public/422.html +26 -0
  80. data/spec/dummy/public/500.html +26 -0
  81. data/spec/dummy/public/favicon.ico +0 -0
  82. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  83. data/spec/dummy/script/rails +6 -0
  84. data/spec/morpheus/associations/association_spec.rb +44 -0
  85. data/spec/morpheus/associations/belongs_to_association_spec.rb +5 -0
  86. data/spec/morpheus/associations/has_many_association_spec.rb +17 -0
  87. data/spec/morpheus/associations/has_one_association_spec.rb +5 -0
  88. data/spec/morpheus/base_spec.rb +126 -0
  89. data/spec/morpheus/client/associations_spec.rb +44 -0
  90. data/spec/morpheus/configuration_spec.rb +136 -0
  91. data/spec/morpheus/mixins/associations_spec.rb +141 -0
  92. data/spec/morpheus/mixins/attributes_spec.rb +99 -0
  93. data/spec/morpheus/mixins/conversion_spec.rb +76 -0
  94. data/spec/morpheus/mixins/finders_spec.rb +255 -0
  95. data/spec/morpheus/mixins/introspection_spec.rb +154 -0
  96. data/spec/morpheus/mixins/persistence_spec.rb +161 -0
  97. data/spec/morpheus/mixins/reflection_spec.rb +100 -0
  98. data/spec/morpheus/mixins/response_parsing_spec.rb +5 -0
  99. data/spec/morpheus/mock_spec.rb +133 -0
  100. data/spec/morpheus/relation_spec.rb +71 -0
  101. data/spec/morpheus/request_cache_spec.rb +5 -0
  102. data/spec/morpheus/request_spec.rb +5 -0
  103. data/spec/morpheus/response_spec.rb +73 -0
  104. data/spec/morpheus/type_caster_spec.rb +343 -0
  105. data/spec/shared/active_model_lint_test.rb +14 -0
  106. data/spec/spec_helper.rb +32 -0
  107. data/spec/support/configuration.rb +26 -0
  108. metadata +427 -0
@@ -0,0 +1,343 @@
1
+ require 'spec_helper'
2
+
3
+ describe Morpheus::TypeCaster, "#cast" do
4
+
5
+ context "when the type_class is nil" do
6
+
7
+ it "casts 'hello' to 'hello'" do
8
+ Morpheus::TypeCaster.cast('hello', nil).should eql('hello')
9
+ end
10
+
11
+ it "casts '2011-04-10' to '2011-04-10'" do
12
+ Morpheus::TypeCaster.cast('2011-04-10', nil).should eql('2011-04-10')
13
+ end
14
+
15
+ it "cast '3' to '3'" do
16
+ Morpheus::TypeCaster.cast('3', nil).should eql('3')
17
+ end
18
+
19
+ it "casts '10-04-2011T05:00:00Z' to '10-04-2011T05:00:00Z'" do
20
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', nil).should eql('10-04-2011T05:00:00Z')
21
+ end
22
+
23
+ it "casts 'true' to 'true'" do
24
+ Morpheus::TypeCaster.cast('true', nil).should eql('true')
25
+ end
26
+
27
+ it "casts 4 to 4" do
28
+ Morpheus::TypeCaster.cast(4, nil).should eql(4)
29
+ end
30
+
31
+ it "casts true to true" do
32
+ Morpheus::TypeCaster.cast(true, nil).should eql(true)
33
+ end
34
+
35
+ it "cast DateTime to DateTime" do
36
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), nil).should eql(DateTime.parse('10-04-2011T05:00:00Z'))
37
+ end
38
+
39
+ it "casts Date to Date" do
40
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), nil).should eql(Date.parse('2011-04-10'))
41
+ end
42
+
43
+ it "casts Time to Time" do
44
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), nil).should eql(Time.parse('10-04-2011T05:00:00Z'))
45
+ end
46
+
47
+ end
48
+
49
+ context "when the type_class is String" do
50
+
51
+ it "casts 'hello' to 'hello'" do
52
+ Morpheus::TypeCaster.cast('hello', :string).should eql('hello')
53
+ end
54
+
55
+ it "casts '2011-04-10' to '2011-04-10'" do
56
+ Morpheus::TypeCaster.cast('2011-04-10', :string).should eql('2011-04-10')
57
+ end
58
+
59
+ it "cast '3' to '3'" do
60
+ Morpheus::TypeCaster.cast('3', :string).should eql('3')
61
+ end
62
+
63
+ it "casts '10-04-2011T05:00:00Z' to '10-04-2011T05:00:00Z'" do
64
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :string).should eql('10-04-2011T05:00:00Z')
65
+ end
66
+
67
+ it "casts 'true' to 'true'" do
68
+ Morpheus::TypeCaster.cast('true', :string).should eql('true')
69
+ end
70
+
71
+ it "casts 4 to '4'" do
72
+ Morpheus::TypeCaster.cast(4, :string).should eql('4')
73
+ end
74
+
75
+ it "casts true to 'true'" do
76
+ Morpheus::TypeCaster.cast(true, :string).should eql('true')
77
+ end
78
+
79
+ it "cast DateTime to '10-04-2011T05:00:00Z'" do
80
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :string).should eql('2011-04-10T05:00:00+00:00')
81
+ end
82
+
83
+ it "casts Date to '2011-04-10'" do
84
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :string).should eql('2011-04-10')
85
+ end
86
+
87
+ it "casts Time to '10-04-2011T05:00:00Z'" do
88
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :string).should eql('Sun Apr 10 05:00:00 UTC 2011')
89
+ end
90
+
91
+ end
92
+
93
+ context "when the type_class is Integer" do
94
+
95
+ it "casts 'hello' to 0" do
96
+ Morpheus::TypeCaster.cast('hello', :integer).should eql(0)
97
+ end
98
+
99
+ it "casts '2011-04-10' to 10" do
100
+ Morpheus::TypeCaster.cast('2011-04-10', :integer).should eql(2011)
101
+ end
102
+
103
+ it "cast '3' to 3" do
104
+ Morpheus::TypeCaster.cast('3', :integer).should eql(3)
105
+ end
106
+
107
+ it "casts '10-04-2011T05:00:00Z' to 10" do
108
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :integer).should eql(10)
109
+ end
110
+
111
+ it "casts 'true' to 0" do
112
+ Morpheus::TypeCaster.cast('true', :integer).should eql(0)
113
+ end
114
+
115
+ it "casts 4 to 4" do
116
+ Morpheus::TypeCaster.cast(4, :integer).should eql(4)
117
+ end
118
+
119
+ it "casts true to 1" do
120
+ Morpheus::TypeCaster.cast(true, :integer).should eql(1)
121
+ end
122
+
123
+ it "casts false to 0" do
124
+ Morpheus::TypeCaster.cast(false, :integer).should eql(0)
125
+ end
126
+
127
+ it "cast DateTime to 1302411600" do
128
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :integer).should eql(1302411600)
129
+ end
130
+
131
+ it "casts Date to 1302393600" do
132
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :integer).should eql(1302418800)
133
+ end
134
+
135
+ it "casts Time to 1302411600" do
136
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :integer).should eql(1302411600)
137
+ end
138
+
139
+ end
140
+
141
+ context "when the type_class is DateTime" do
142
+
143
+ it "casts 'hello' to nil" do
144
+ Morpheus::TypeCaster.cast('hello', :datetime).should eql(nil)
145
+ end
146
+
147
+ it "casts '2011-04-10' to DateTime" do
148
+ Morpheus::TypeCaster.cast('2011-04-10', :datetime).should eql(DateTime.parse('2011-04-10'))
149
+ end
150
+
151
+ it "cast '3' to nil" do
152
+ Morpheus::TypeCaster.cast('3', :datetime).should eql(nil)
153
+ end
154
+
155
+ it "casts '10-04-2011T05:00:00Z' to DateTime" do
156
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :datetime).should eql(DateTime.parse('10-04-2011T05:00:00Z'))
157
+ end
158
+
159
+ it "casts 'true' to nil" do
160
+ Morpheus::TypeCaster.cast('true', :datetime).should eql(nil)
161
+ end
162
+
163
+ it "casts 4 to nil" do
164
+ Morpheus::TypeCaster.cast(4, :datetime).should eql(nil)
165
+ end
166
+
167
+ it "casts true to nil" do
168
+ Morpheus::TypeCaster.cast(true, :datetime).should eql(nil)
169
+ end
170
+
171
+ it "cast DateTime to DateTime" do
172
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :datetime).should eql(DateTime.parse('10-04-2011T05:00:00Z'))
173
+ end
174
+
175
+ it "casts Date to DateTime" do
176
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :datetime).should eql(DateTime.parse('2011-04-10'))
177
+ end
178
+
179
+ it "casts Time to DateTime" do
180
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :datetime).should eql(DateTime.parse('10-04-2011T05:00:00Z'))
181
+ end
182
+
183
+ end
184
+
185
+ context "when the type_class is Date" do
186
+
187
+ it "casts 'hello' to nil" do
188
+ Morpheus::TypeCaster.cast('hello', :date).should eql(nil)
189
+ end
190
+
191
+ it "casts '2011-04-10' to Date" do
192
+ Morpheus::TypeCaster.cast('2011-04-10', :date).should eql(Date.parse('10-04-2011'))
193
+ end
194
+
195
+ it "cast '3' to nil" do
196
+ Morpheus::TypeCaster.cast('3', :date).should eql(nil)
197
+ end
198
+
199
+ it "casts '10-04-2011T05:00:00Z' to Date" do
200
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :date).should eql(Date.parse('10-04-2011T05:00:00Z'))
201
+ end
202
+
203
+ it "casts 'true' to nil" do
204
+ Morpheus::TypeCaster.cast('true', :date).should eql(nil)
205
+ end
206
+
207
+ it "casts 4 to nil" do
208
+ Morpheus::TypeCaster.cast(4, :date).should eql(nil)
209
+ end
210
+
211
+ it "casts true to nil" do
212
+ Morpheus::TypeCaster.cast(true, :date).should eql(nil)
213
+ end
214
+
215
+ it "cast DateTime to Date" do
216
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :date).should eql(Date.parse('10-04-2011T05:00:00Z'))
217
+ end
218
+
219
+ it "casts Date to Date" do
220
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :date).should eql(Date.parse('2011-04-10'))
221
+ end
222
+
223
+ it "casts Time to Date" do
224
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :date).should eql(Date.parse('10-04-2011T05:00:00Z'))
225
+ end
226
+
227
+ end
228
+
229
+ context "when the type_class is Time" do
230
+
231
+ it "casts 'hello' to nil" do
232
+ Morpheus::TypeCaster.cast('hello', :time).should eql(nil)
233
+ end
234
+
235
+ it "casts '2011-04-10' to Time" do
236
+ Morpheus::TypeCaster.cast('2011-04-10', :time).should eql('2011-04-10'.to_time)
237
+ end
238
+
239
+ it "cast '3' to nil" do
240
+ Morpheus::TypeCaster.cast('3', :time).should eql(nil)
241
+ end
242
+
243
+ it "casts '10-04-2011T05:00:00Z' to Time" do
244
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :time).should eql(Time.parse('10-04-2011T05:00:00Z'))
245
+ end
246
+
247
+ it "casts 'true' to nil" do
248
+ Morpheus::TypeCaster.cast('true', :time).should eql(nil)
249
+ end
250
+
251
+ it "casts 4 to nil" do
252
+ Morpheus::TypeCaster.cast(4, :time).should eql(nil)
253
+ end
254
+
255
+ it "casts true to nil" do
256
+ Morpheus::TypeCaster.cast(true, :time).should eql(nil)
257
+ end
258
+
259
+ it "cast DateTime to Time" do
260
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :time).should eql(Time.parse('10-04-2011T05:00:00Z'))
261
+ end
262
+
263
+ it "casts Date to Time" do
264
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :time).should eql(Time.parse('2011-04-10'))
265
+ end
266
+
267
+ it "casts Time to Time" do
268
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :time).should eql(Time.parse('10-04-2011T05:00:00Z'))
269
+ end
270
+
271
+ end
272
+
273
+ context "when the type_class is a boolean" do
274
+
275
+ it "casts 'hello' to true" do
276
+ Morpheus::TypeCaster.cast('hello', :boolean).should eql(true)
277
+ end
278
+
279
+ it "casts '2011-04-10' to true" do
280
+ Morpheus::TypeCaster.cast('2011-04-10', :boolean).should eql(true)
281
+ end
282
+
283
+ it "cast '3' to true" do
284
+ Morpheus::TypeCaster.cast('3', :boolean).should eql(true)
285
+ end
286
+
287
+ it "casts '10-04-2011T05:00:00Z' to true" do
288
+ Morpheus::TypeCaster.cast('10-04-2011T05:00:00Z', :boolean).should eql(true)
289
+ end
290
+
291
+ it "casts 'true' to true" do
292
+ Morpheus::TypeCaster.cast('true', :boolean).should eql(true)
293
+ end
294
+
295
+ it "casts 4 to true" do
296
+ Morpheus::TypeCaster.cast(4, :boolean).should eql(true)
297
+ end
298
+
299
+ it "casts true to true" do
300
+ Morpheus::TypeCaster.cast(true, :boolean).should eql(true)
301
+ end
302
+
303
+ it "casts nil to false" do
304
+ Morpheus::TypeCaster.cast(nil, :boolean).should eql(false)
305
+ end
306
+
307
+ it "casts 'false' to false" do
308
+ Morpheus::TypeCaster.cast('false', :boolean).should eql(false)
309
+ end
310
+
311
+ it "casts 1 to true" do
312
+ Morpheus::TypeCaster.cast(1, :boolean).should eql(true)
313
+ end
314
+
315
+ it "casts '0' to false" do
316
+ Morpheus::TypeCaster.cast('0', :boolean).should eql(false)
317
+ end
318
+
319
+ it "cast DateTime to true" do
320
+ Morpheus::TypeCaster.cast(DateTime.parse('10-04-2011T05:00:00Z'), :boolean).should eql(true)
321
+ end
322
+
323
+ it "casts Date to true" do
324
+ Morpheus::TypeCaster.cast(Date.parse('2011-04-10'), :boolean).should eql(true)
325
+ end
326
+
327
+ it "casts Time to true" do
328
+ Morpheus::TypeCaster.cast(Time.parse('10-04-2011T05:00:00Z'), :boolean).should eql(true)
329
+ end
330
+
331
+ end
332
+
333
+ context "when the type_class is unknown" do
334
+
335
+ it "raises an error" do
336
+ lambda {
337
+ Morpheus::TypeCaster.cast(nil, :made_up_class)
338
+ }.should raise_error(Morpheus::UnrecognizedTypeCastClass, "Can't typecast to made_up_class!")
339
+ end
340
+
341
+ end
342
+
343
+ end
@@ -0,0 +1,14 @@
1
+ shared_examples_for "ActiveModel" do
2
+ require 'test/unit/assertions'
3
+ require 'active_model/lint'
4
+ include Test::Unit::Assertions
5
+ include ActiveModel::Lint::Tests
6
+
7
+ before do
8
+ @model = model_under_test
9
+ end
10
+
11
+ ActiveModel::Lint::Tests.public_instance_methods.map { |method| method.to_s }.grep(/^test/).each do |method|
12
+ example(method.gsub('_', ' ')) { send method }
13
+ end
14
+ end
@@ -0,0 +1,32 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+ require "rspec/rails"
7
+ require 'pry'
8
+
9
+ ActionMailer::Base.delivery_method = :test
10
+ ActionMailer::Base.perform_deliveries = true
11
+ ActionMailer::Base.default_url_options[:host] = "test.com"
12
+
13
+ Rails.backtrace_cleaner.remove_silencers!
14
+
15
+ # Run any available migration
16
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
17
+
18
+ # Load support files
19
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
20
+ Dir["#{File.dirname(__FILE__)}/shared/**/*.rb"].each { |f| require f }
21
+
22
+ RSpec.configure do |config|
23
+ # Remove this line if you don't want RSpec's should and should_not
24
+ # methods or matchers
25
+ require 'rspec/expectations'
26
+ config.include RSpec::Matchers
27
+
28
+ # == Mock Framework
29
+ config.mock_with :rspec
30
+
31
+ config.color_enabled = true
32
+ end
@@ -0,0 +1,26 @@
1
+ Morpheus::Configuration.host = 'http://localhost:3000'
2
+ Morpheus::Configuration.hydra = Typhoeus::Hydra.new
3
+ Morpheus::Configuration.allow_net_connect = false
4
+
5
+ module MorpheusHelper
6
+
7
+ def build_morpheus_response(status, content, errors = nil)
8
+ body = {
9
+ :status => status,
10
+ :content => content
11
+ }
12
+ body.merge!(errors) if errors
13
+ Morpheus::Response.new(:code => status, :headers => "", :body => body.to_json, :time => 0.3)
14
+ end
15
+
16
+ end
17
+
18
+ RSpec.configure do |config|
19
+ config.include(MorpheusHelper)
20
+
21
+ config.before(:each) do
22
+ Morpheus::Configuration.hydra.clear_stubs
23
+ Morpheus::RequestQueue.queue.clear
24
+ Morpheus::RequestCache.cache.clear
25
+ end
26
+ end