diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-02-19 21:20:15 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-02-19 21:20:15 +0100 |
commit | d8f1ee4b41352b870f617b01099b2877f754d32c (patch) | |
tree | dd6bdf1d1ded6aab7bb926109a24e942fc740b73 /activeresource/lib | |
parent | 8ba1fc18e13c03966d411947180022c1730e81ff (diff) | |
parent | 7c0e008973e594ebf53607362c1dfbe34b693600 (diff) | |
download | rails-d8f1ee4b41352b870f617b01099b2877f754d32c.tar.gz rails-d8f1ee4b41352b870f617b01099b2877f754d32c.tar.bz2 rails-d8f1ee4b41352b870f617b01099b2877f754d32c.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 12 | ||||
-rw-r--r-- | activeresource/lib/active_resource/http_mock.rb | 66 | ||||
-rw-r--r-- | activeresource/lib/active_resource/validations.rb | 2 |
3 files changed, 31 insertions, 49 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 94418fb559..a8c0da31f2 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -462,7 +462,7 @@ module ActiveResource # that_guy.valid? # => false # that_guy.new? # => true def create(attributes = {}) - returning(self.new(attributes)) { |res| res.save } + self.new(attributes).tap { |resource| resource.save } end # Core method for finding resources. Used similarly to Active Record's +find+ method. @@ -600,7 +600,7 @@ module ActiveResource end def instantiate_record(record, prefix_options = {}) - returning new(record) do |resource| + new(record).tap do |resource| resource.prefix_options = prefix_options end end @@ -747,7 +747,7 @@ module ActiveResource # def ==(other) other.equal?(self) || (other.instance_of?(self.class) && other.id == id && other.prefix_options == prefix_options) - end + end # Tests for equality (delegates to ==). def eql?(other) @@ -773,7 +773,7 @@ module ActiveResource # my_invoice.customer # => That Company # next_invoice.customer # => That Company def dup - returning self.class.new do |resource| + self.class.new.tap do |resource| resource.attributes = @attributes resource.prefix_options = @prefix_options end @@ -985,14 +985,14 @@ module ActiveResource # Update the resource on the remote service. def update - returning connection.put(element_path(prefix_options), encode, self.class.headers) do |response| + connection.put(element_path(prefix_options), encode, self.class.headers).tap do |response| load_attributes_from_response(response) end end # Create (i.e., \save to the remote service) the \new resource. def create - returning connection.post(collection_path, encode, self.class.headers) do |response| + connection.post(collection_path, encode, self.class.headers).tap do |response| self.id = id_from_response(response) load_attributes_from_response(response) end diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 0b4549f759..7d7e378436 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -59,7 +59,7 @@ module ActiveResource # end module_eval <<-EOE, __FILE__, __LINE__ def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) - @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers) + @responses << [Request.new(:#{method}, path, nil, request_headers), Response.new(body || "", status, response_headers)] end EOE end @@ -91,21 +91,17 @@ module ActiveResource @@requests ||= [] end - # Returns a hash of <tt>request => response</tt> pairs for all all responses this mock has delivered, where +request+ - # is an instance of ActiveResource::Request and the response is, naturally, an instance of - # ActiveResource::Response. + # Returns the list of requests and their mocked responses. Look up a + # response for a request using responses.assoc(request). def responses - @@responses ||= {} + @@responses ||= [] end # Accepts a block which declares a set of requests and responses for the HttpMock to respond to. See the main # ActiveResource::HttpMock description for a more detailed explanation. def respond_to(pairs = {}) #:yields: mock reset! - pairs.each do |(path, response)| - responses[path] = response - end - + responses.concat pairs.to_a if block_given? yield Responder.new(responses) else @@ -120,29 +116,23 @@ module ActiveResource end end - for method in [ :post, :put ] - # def post(path, body, headers) - # request = ActiveResource::Request.new(:post, path, body, headers) - # self.class.requests << request - # self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for #{request}")) - # end - module_eval <<-EOE, __FILE__, __LINE__ - def #{method}(path, body, headers) - request = ActiveResource::Request.new(:#{method}, path, body, headers) - self.class.requests << request - self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}")) - end - EOE - end - - for method in [ :get, :delete, :head ] - module_eval <<-EOE, __FILE__, __LINE__ - def #{method}(path, headers) - request = ActiveResource::Request.new(:#{method}, path, nil, headers) - self.class.requests << request - self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for \#{request}")) - end - EOE + # body? methods + { true => %w(post put), + false => %w(get delete head) }.each do |has_body, methods| + methods.each do |method| + # def post(path, body, headers) + # request = ActiveResource::Request.new(:post, path, body, headers) + # self.class.requests << request + # self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for #{request}")) + # end + module_eval <<-EOE, __FILE__, __LINE__ + def #{method}(path, #{'body, ' if has_body}headers) + request = ActiveResource::Request.new(:#{method}, path, #{has_body ? 'body, ' : 'nil, '}headers) + self.class.requests << request + self.class.responses.assoc(request).try(:second) || raise(InvalidRequestError.new("No response recorded for \#{request}")) + end + EOE + end end def initialize(site) #:nodoc: @@ -157,21 +147,13 @@ module ActiveResource @method, @path, @body, @headers = method, path, body, headers.merge(ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[method] => 'application/xml') end - def ==(other_request) - other_request.hash == hash - end - - def eql?(other_request) - self == other_request + def ==(req) + path == req.path && method == req.method && headers == req.headers end def to_s "<#{method.to_s.upcase}: #{path} [#{headers}] (#{body})>" end - - def hash - "#{path}#{method}#{headers}".hash - end end class Response diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index 4bc906d291..de3339935f 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -203,7 +203,7 @@ module ActiveResource def from_xml(xml) clear humanized_attributes = @base.attributes.keys.inject({}) { |h, attr_name| h.update(attr_name.humanize => attr_name) } - messages = Hash.from_xml(xml)['errors']['error'] rescue [] + messages = Array.wrap(Hash.from_xml(xml)['errors']['error']) rescue [] messages.each do |message| attr_message = humanized_attributes.keys.detect do |attr_name| if message[0, attr_name.size + 1] == "#{attr_name} " |