Class: ActiveResource::HttpMock
- Inherits:
-
Object
- Object
- ActiveResource::HttpMock
- Defined in:
- lib/active_resource_test_helper/active_resource/http_mock.rb
Overview
Adds new convenience methods to ActiveResource::HttpMock.
Class Method Summary collapse
-
.dynamic_models ⇒ Object
Return a list of all the dynamic models used by HttpMock.
-
.register_factory(name) ⇒ Object
Add a new dynamic model by registering a new factory.
Instance Method Summary collapse
-
#get(path, headers) ⇒ Object
:nodoc:.
- #old_get ⇒ Object
-
#reset_dynamic_models! ⇒ Object
Unregister all the dynamic models.
Class Method Details
.dynamic_models ⇒ Object
Return a list of all the dynamic models used by HttpMock
11 12 13 |
# File 'lib/active_resource_test_helper/active_resource/http_mock.rb', line 11 def self.dynamic_models @@dynamic_models ||= [] end |
.register_factory(name) ⇒ Object
Add a new dynamic model by registering a new factory. Do not use this method directly, Use the active_resource_factories method provided by the ActiveResourceTestHelper module.
18 19 20 21 |
# File 'lib/active_resource_test_helper/active_resource/http_mock.rb', line 18 def self.register_factory name build_class = Factory.factory_by_name(name).build_class dynamic_models << build_class unless dynamic_models.include? build_class end |
Instance Method Details
#get(path, headers) ⇒ Object
:nodoc:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_resource_test_helper/active_resource/http_mock.rb', line 30 def get(path, headers) #:nodoc: begin # lookup into the static mocks old_get path, headers rescue InvalidRequestError # lookup into the dynamic mocks model_class = HttpMock.dynamic_models.find{|m| !(path =~ dynamic_model_regexp(m)).nil?} if model_class.nil? raise else if $1.nil? and $2.nil? # all the items have been requested items = entries_to_valid_array(model_class.all) response = Response.new(items.to_xml(:root => model_class.to_s)) elsif !$1.nil? # a specific item has been requested match = model_class.find(:id => $1.delete('/').to_i).entries.first if match.nil? response = Response.new(nil, 404) else response = Response.new(match.to_hash.to_xml(:root => model_class.to_s)) end else # query contains params params = {} CGI::parse($2[1,$2.size]).each do |key, value| params[key.to_sym] = value.first end items = entries_to_valid_array(model_class.find(params).entries) response = Response.new(items.to_xml(:root => model_class.to_s)) end self.class.responses << response response end end end |
#old_get ⇒ Object
28 |
# File 'lib/active_resource_test_helper/active_resource/http_mock.rb', line 28 alias old_get get |
#reset_dynamic_models! ⇒ Object
Unregister all the dynamic models.
24 25 26 |
# File 'lib/active_resource_test_helper/active_resource/http_mock.rb', line 24 def reset_dynamic_models! @@dynamic_models.clear end |