lib/is_csv_importable/csv_import.rb
def import(obj,wrappers,sep)
      $KCODE = "utf8"
      saved = false
      
      FasterCSV.parse(File.read(@file), :headers => true, :col_sep => sep) do |row|
        
        k = row.to_hash
        #puts k.inspect
        unless wrappers.empty?
          instance = obj.class.new
          wrappers.each_pair do |key,val|
            #puts "row has key? #{k.has_key?(key.to_s)}"
            #puts "key,val for wrapper #{key},#{val}"
            puts "#{k[key.to_s].nil?}, #{val}"
            unless k[key.to_s].nil?
              instance.send("#{val}=", k[key.to_s]) 
            else
              puts "save nothing."
            end
            #puts "key as string #{key.to_s}"
            
          end
        else
          instance = obj.class.new(k)
        end
        
        saved = instance.save
        #puts saved
        break unless saved
      end
      
      return saved
    end
#
# returns true if import works, otherwise false
#
returns true if import works, otherwise false
returns true if import works otherwise false
def import(obj,wrappers,sep)
      $KCODE = "utf8"
      saved = false
      FasterCSV.parse(File.read(@file), :headers => true, :col_sep => sep) do |row|
        k = row.to_hash
        unless wrappers.empty?
          instance = obj.class.new
          wrappers.each_pair do |key,val|
            puts "#{k[key.to_s].nil?}, #{val}"
            unless k[key.to_s].nil?
              instance.send("#{val}=", k[key.to_s]) 
            else
              puts "save nothing."
            end
          end
        else
          instance = obj.class.new(k)
        end
        saved = instance.save
        break unless saved
      end
      return saved
    end
def import ( obj , wrappers , sep ) $KCODE = "utf8" saved = false FasterCSV . parse ( File . read ( @file ) , :headers => true , :col_sep => sep ) do | row | k = row . to_hash unless wrappers . empty? instance = obj . class . new wrappers . each_pair do | key , val | puts "#{k[key.to_s].nil?}, #{val}" unless k [ key . to_s ] . nil? instance . send ( "#{val}=" , k [ key . to_s ] ) else puts "save nothing." end end else instance = obj . class . new ( k ) end saved = instance . save break unless saved end return saved end
returns true if import works, otherwise false
returns true if import works otherwise false
#puts k.inspect #puts "row has key? #{k.has_key?(key.to_s)}" #puts "key,val for wrapper #{key},#{val}" #puts "key as string #{key.to_s}" #puts saved
obj
wrappers
sep