30 lines
		
	
	
		
			904 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
		
		
			
		
	
	
			30 lines
		
	
	
		
			904 B
		
	
	
	
		
			Ruby
		
	
	
	
	
	
|   | require 'lib/string_helper' | ||
|  | 
 | ||
|  | module Connectors | ||
|  |   module Helpers | ||
|  |     module UpdateCommand | ||
|  | 
 | ||
|  |       # when included, this module adds method #update and alias for it. | ||
|  |       # Alias name depends on base class name. | ||
|  |       # We need this alias to forward methods from MongoConnector to resources connectors. | ||
|  | 
 | ||
|  |       def self.included(base) | ||
|  |         resource_name = StringHelper.underscore_class(base) | ||
|  |         method_name = "#{resource_name}_update".to_sym | ||
|  |         alias_method method_name, :update | ||
|  |       end | ||
|  | 
 | ||
|  |       def update(record) | ||
|  |         record.validate! | ||
|  |         collection.update({"_id" => record.id}, record.to_mongo_hash) | ||
|  |       rescue Mongo::OperationFailure => e | ||
|  |         if e.message =~ /^11000/ | ||
|  |           resource_name = StringHelper.underscore_class(record.class) | ||
|  |           raise InvalidRecord.new("Duplicate key error: #{resource_name} with id '#{record.id}'") | ||
|  |         end | ||
|  |       end | ||
|  | 
 | ||
|  |     end | ||
|  |   end | ||
|  | end |