| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | module Connectors | 
					
						
							|  |  |  |   module Helpers | 
					
						
							|  |  |  |     module UpdateCommand | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-14 16:51:40 +03:00
										 |  |  |       # this module adds methods #update and "#{resource_name}_update" (they are synonyms). | 
					
						
							|  |  |  |       # We need second method name to forward methods from MongoConnector to resources connectors. | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       def self.included(base) | 
					
						
							| 
									
										
										
										
											2016-01-19 18:06:09 +03:00
										 |  |  |         method_name = "#{base.to_s.underscore_class}_update".to_sym | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |         alias_method method_name, :update | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def update(record) | 
					
						
							| 
									
										
										
										
											2015-11-20 18:31:54 +03:00
										 |  |  |         record.validate! | 
					
						
							|  |  |  |         r = collection.update({"_id" => record.id}, record.to_mongo_hash) | 
					
						
							|  |  |  |         raise RecordNotFound.new("'#{record.id}' not found") if r['n'] == 0
 | 
					
						
							|  |  |  |         record | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |