module ActsAsCsvImport::Hook
  def acts_as_csv_import(*args)
    @csv_options = args.extract_options!
    
    
  
    def self.import_csv_fields
      @csv_options[:fields]
    end

    def self.import_csv(list, import_csv)
      puts self.instance_methods 
      
      if self.methods.include?(:custom_csv_import)
        custom_csv_import(list, import_csv)
      else
        default_import_csv(list, import_csv)
      end

    end

    def self.default_import_csv(list, import_csv)
      list.each do |row|
        if row["past_id"] #false
          if n = self.where(:past_id => row["past_id"]).first
          else
            n = self.new
          end
        else
          n = self.new
        end

        row.each do |key, value|
          
          if self.type_for_attribute(key) and self.type_for_attribute(key).type == :decimal 
            eval "n.#{key} = value.to_s.gsub(' ', '').gsub(',', '.')"
          else
            eval "n.#{key} = value"
          end
          

        end

        n.save

        import_csv.import_csv_elements << ImportCsvElement.new(:element => n)

      end
    end
    
    
    
  end
  
  
  
  
end
