2015-02-12 13:01:05 +03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								module Connectors
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  module Helpers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    module InsertCommand
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      # when included, this module adds method #insert 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)
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-19 18:06:09 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        resource_name = base.to_s.underscore_class
							 | 
						
					
						
							
								
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        method_name = "#{resource_name}_insert".to_sym
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        alias_method method_name, :insert
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      def insert(record)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        begin
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          record.validate!
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-26 12:04:01 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          hash = record.to_mongo_hash
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          hash["created_at"] = Time.now.to_i
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          collection.insert(hash)
							 | 
						
					
						
							
								
									
										
										
										
											2015-07-30 02:22:54 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          record
							 | 
						
					
						
							
								
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        rescue Mongo::OperationFailure => e
							 | 
						
					
						
							
								
									
										
										
										
											2015-10-12 16:58:23 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								          # exception's message doesn't always start from error code
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          if e.message =~ /11000/
							 | 
						
					
						
							
								
									
										
										
										
											2016-01-19 18:06:09 +03:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            resource_name = record.class.to_s.underscore_class
							 | 
						
					
						
							
								
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            raise InvalidRecord.new("Duplicate key error: #{resource_name} with id '#{record.id}'")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								end
							 |