lib/flextures/flextures.rb in flextures-1.0.8 vs lib/flextures/flextures.rb in flextures-1.9.0

- old
+ new

@@ -34,22 +34,31 @@ "#{Rails.root.to_path}/config/flextures.factory.rb", ].each { |fn| load(fn) if File.exist?(fn) } end end + # テーブル情報の初期化 + 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 } + end + # 引数解析 module ARGS # 書き出し 、読み込み すべきファイルとオプションを書きだす def self.parse option={} - Flextures::init_load - table_names = "" - table_names = ENV["TABLE"].split(",") if ENV["TABLE"] - table_names = ENV["T"].split(",") if ENV["T"] - table_names = ENV["MODEL"].constantize.table_name.split(",") if ENV["MODEL"] - table_names = ENV["M"].constantize.table_name.split(",") if ENV["M"] - table_names = ActiveRecord::Base.connection.tables if ""==table_names - table_names = table_names.map{ |name| { table: name } } + table_names = [] + if ENV["T"] or ENV["TABLE"] + table_names = (ENV["T"] or ENV["TABLE"]).split(',').map{ |name| { table: name } } + end + if ENV["M"] or ENV["MODEL"] + table_names = (ENV["M"] or ENV["MODEL"]).split(',').map{ |name| { table: name.constantize.table_name } } + end + if table_names.empty? + table_names = ActiveRecord::Base.connection.tables.map{ |name| { table: name } } + end # ENV["FIXTURES"]の中身を解析 fixtures_args_parser =->(s){ names = s.split(',') ( names.size==1 and ENV.values_at("M", "MODEL", "T", "TABLE").first ) ? [ table_names.first.merge( file: names.first ) ] : @@ -82,31 +91,30 @@ end end # csv で fixtures を dump def self.csv format - table_name = format[:table] - file_name = format[:file] || table_name + file_name = format[:file] || format[:table] dir_name = format[:dir] || DUMP_DIR outfile = "#{dir_name}#{file_name}.csv" + table_name = format[:table] klass = PARENT.create_model(table_name) attributes = klass.columns.map { |colum| colum.name } - CSV.open(outfile,'w') do |csv| csv<< attributes klass.all.each do |row| - csv<< attributes.map { |column| trans(row.send(column))} + csv<< attributes.map { |column| trans(row.send(column)) } end end end # yaml で fixtures を dump def self.yml format - table_name = format[:table] - file_name = format[:file] || table_name + file_name = format[:file] || format[:table] dir_name = format[:dir] || DUMP_DIR outfile = "#{dir_name}#{file_name}.yml" + table_name = format[:table] klass = PARENT::create_model(table_name) attributes = klass.columns.map { |colum| colum.name } File.open(outfile,"w") do |f| klass.all.each_with_index do |row,idx| f<< "#{table_name}_#{idx}:\n" + @@ -162,12 +170,18 @@ method = :yml if File.exist? "#{dir_name}#{file_name}.yml" self::send(method, format) if method end # fixturesをまとめてロード、主にテストtest/unit, rspec で使用する + # + # 全テーブルが対象 + # fixtures :all + # テーブル名で一覧する + # fixtures :users, :items + # ハッシュで指定 + # fixtures :users => :users2 def self.flextures *fixtures - PARENT::init_load # :allですべてのfixtureを反映 fixtures = ActiveRecord::Base.connection.tables if fixtures.size== 1 and :all == fixtures.first fixtures_hash = fixtures.pop if fixtures.last and fixtures.last.is_a? Hash # ハッシュ取り出し fixtures.each{ |table_name| Loader::load table: table_name } @@ -182,17 +196,17 @@ 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) - o = filter.call h + args = [h, file_name] + o = filter.call *args[0,filter.arity] o.save end end end @@ -206,10 +220,11 @@ attributes = klass.columns.map &:name filter = create_filter klass, Factory[table_name] klass.delete_all YAML.load(File.open(inpfile)).each do |k,h| warning "YAML", attributes, h.keys - o = filter.call h + args = [h, file_name] + o = filter.call *args[0,filter.arity] o.save end end # 欠けたカラムを検知してメッセージを出しておく