lib/flextures/flextures.rb in flextures-1.9.4 vs lib/flextures/flextures.rb in flextures-1.9.5

- old
+ new

@@ -45,11 +45,17 @@ # テーブル情報の初期化 def self.init_tables tables = ActiveRecord::Base.connection.tables tables.delete "schema_migrations" - tables.each{ |name| Class.new(ActiveRecord::Base){ |o| o.table_name=name }.delete_all } + tables.each{ |name| + # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう + begin + Class.new(ActiveRecord::Base){ |o| o.table_name= name }.delete_all + rescue => e + end + } end # 引数解析 module ARGS # 書き出し 、読み込み すべきファイルとオプションを書きだす @@ -150,11 +156,11 @@ string:->{ "" }, text:->{ "" }, time:->{ DateTime.now }, timestamp:->{ DateTime.now }, } - + # 型の変換を行う TRANSLATER = { binary:->(d){ d.to_i }, boolean:->(d){ (0==d || ""==d || !d) ? false : true }, date:->(d){ Date.parse(d.to_s) }, @@ -202,10 +208,11 @@ dir_name = format[:dir] || LOAD_DIR inpfile = "#{dir_name}#{file_name}.csv" klass = PARENT::create_model table_name attributes = klass.columns.map &:name filter = create_filter klass, Factory[table_name] + klass.delete_all CSV.open( inpfile ) do |csv| keys = csv.shift # keyの設定 warning "CSV", attributes, keys csv.each do |values| h = values.extend(Extensions::Array).to_hash(keys) @@ -231,10 +238,10 @@ args = [h, file_name] o = filter.call *args[0,filter.arity] o.save end end - + # 欠けたカラムを検知してメッセージを出しておく def self.warning format, attributes, keys (attributes-keys).each { |name| print "Warning: #{format} colum is missing! [#{name}]\n" } (keys-attributes).each { |name| print "Warning: #{format} colum is left over! [#{name}]\n" } end