lib/flextures/flextures.rb in flextures-1.0.3 vs lib/flextures/flextures.rb in flextures-1.0.4
- old
+ new
@@ -38,25 +38,32 @@
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 } }
+ # 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 ) ] :
+ names.map{ |name| { table: name, file: name } }
+ }
+ table_names = fixtures_args_parser.call ENV["FIXTURES"] if ENV["FIXTURES"]
+ table_names = fixtures_args_parser.call ENV["F"] if ENV["F"]
table_names = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
- table_names = ENV["FIXTURES"].split(',').map{ |name| { table: name } } if ENV["FIXTURES"]
- table_names = ENV["F"].split(',').map{ |name| { table: name } } if ENV["F"]
# read mode だとcsvもyaml存在しないファイルは返さない
- table_names.select! &exist if option[:mode] && option[:mode].to_sym == :read
+ table_names.select! &exist if option[:mode] && option[:mode].to_sym == :read
table_names
end
- # 存在しているファイルで絞り込む
+ # 存在しているファイルで絞り込む
def self.exist
return->(name){ File.exists?("#{LOAD_DIR}#{name}.csv") or File.exists?("#{LOAD_DIR}#{name}.yml") }
end
end
- # データを吐き出す処理をまとめる
+ # データを吐き出す処理をまとめる
module Dumper
PARENT = Flextures
# 適切な型に変換
def self.trans v
@@ -65,11 +72,11 @@
when false; 0
else; v
end
end
- # csv で fixtures を dump
+ # csv で fixtures を dump
def self.csv format
table_name = format[:table]
file_name = format[:file] || table_name
dir_name = format[:dir] || DUMP_DIR
outfile = "#{dir_name}#{file_name}.csv"
@@ -82,26 +89,25 @@
csv<< attributes.map { |column| trans(row.send(column))}
end
end
end
- # yaml で fixtures を dump
+ # yaml で fixtures を dump
def self.yml format
table_name = format[:table]
file_name = format[:file] || table_name
dir_name = format[:dir] || DUMP_DIR
outfile = "#{dir_name}#{file_name}.yml"
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|
+ klass.all.each_with_index do |row,idx|
f<< "#{table_name}_#{idx}:\n" +
attributes.map { |column|
v = trans row.send(column)
" #{column}: #{v}\n"
- }.*
+ }.join
end
end
end
end
@@ -147,16 +153,16 @@
method = :csv if File.exist? "#{dir_name}#{file_name}.csv"
method = :yml if File.exist? "#{dir_name}#{file_name}.yml"
self::send(method, format) if method
end
- # fixturesをまとめてロード、主にテストtest/unit, rspec で使用する
+ # fixturesをまとめてロード、主にテストtest/unit, rspec で使用する
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 }
fixtures_hash.each{ |k,v| Loader::load table: k, file: v } if fixtures_hash
fixtures
end
@@ -165,11 +171,10 @@
def self.csv format
table_name = format[:table].to_s
file_name = format[:file] || table_name
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.columns, Factory[table_name]
klass.delete_all
CSV.open( inpfile ) do |csv|
@@ -189,10 +194,9 @@
def self.yml format
table_name = format[:table].to_s
file_name = format[:file] || table_name
dir_name = format[:dir] || LOAD_DIR
inpfile = "#{dir_name}#{file_name}.yml"
-
klass = PARENT::create_model table_name
attributes = klass.columns.map &:name
filter = create_filter klass.columns, Factory[table_name]
klass.delete_all
YAML.load(File.open(inpfile)).each do |k,h|