lib/flextures/flextures.rb in flextures-2.1.0 vs lib/flextures/flextures.rb in flextures-3.0.0

- old
+ new

@@ -6,101 +6,97 @@ require "flextures/flextures_base_config" require "flextures/flextures_extension_modules" require "flextures/flextures_factory" module Flextures - LOAD_DIR = Config.fixture_load_directory - DUMP_DIR = Config.fixture_dump_directory - - # テーブルモデルの作成 + # ActiveRecord Model is created that guess by table_name + # @params [String|Symbol] table_name + # @params [ActiveRecord::Base] model class def self.create_model table_name - # Factoryにオプションで指定があった時 + # when Model is defined in FactoryFilter a = ->{ f = Factory::FACTORIES[table_name.to_sym] f && f[:model] } - # テーブル名からモデル名が推測できるとき + # when program can guess Model name by table_name b = ->{ begin table_name.singularize.camelize.constantize rescue => e nil end } - # モデル名推測不可能なとき + # when cannot guess Model name c = ->{ Class.new(ActiveRecord::Base){ |o| o.table_name=table_name } } a.call || b.call || c.call end - # 設定ファイルが存在すればロード + # load configuration file, if file is exist def self.init_load if defined?(Rails) and Rails.root [ "#{Rails.root.to_path}/config/flextures.config.rb", "#{Rails.root.to_path}/config/flextures.factory.rb", ].each { |fn| load(fn) if File.exist?(fn) } end end - # 全テーブル削除のときにほんとうに消去して良いテーブル一覧を返す + # @return [Array] flextures useable table names def self.deletable_tables tables = ActiveRecord::Base.connection.tables - Flextures::Config.ignore_tables.each do |name| - tables.delete name - end + Flextures::Config.ignore_tables.each { |name| tables.delete name } tables end - # テーブル情報の初期化 + # initialize table data def self.init_tables tables = Flextures::deletable_tables tables.each do |name| - # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう + # if 'name' variable is 'database view', raise error begin Class.new(ActiveRecord::Base){ |o| o.table_name= name }.delete_all rescue => e end end end - # テーブル情報の初期化 def self.delete_tables *tables tables.each do |name| - # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう + # if 'name' variable is 'database view', raise error begin Class.new(ActiveRecord::Base){ |o| o.table_name= name }.delete_all - rescue => e + rescue StandaraError => e end end end - # デバッグ用のメソッド、渡されたブロックを実行する - # 主にテーブルの今の中身を覗きたい時に使う + # It is debug method to use like 'tab' method + # @params [Proc] dumper write dump information def self.table_tap &dumper tables = Flextures::deletable_tables tables.each do |name| - # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう + # if 'name' variable is 'database view', raise error begin klass = Class.new(ActiveRecord::Base){ |o| o.table_name= name; } dumper.call klass - rescue => e + rescue StandaraError => e end end end # parse arguments functions module ARGS - # 書き出し 、読み込み すべきファイルとオプションを書きだす + # parse rake ENV parameters def self.parse option={} table_names = [] - if ENV["T"] or ENV["TABLE"] - table_names = (ENV["T"] or ENV["TABLE"]).split(',').map{ |name| { table: name, file: name } } + if v = (ENV["T"] or ENV["TABLE"]) + table_names = v.split(',').map{ |name| { table: name, file: name } } end - if ENV["M"] or ENV["MODEL"] - table_names = (ENV["M"] or ENV["MODEL"]).split(',').map do |name| + if v = (ENV["M"] or ENV["MODEL"]) + table_names = v.split(',').map do |name| name = name.constantize.table_name { table: name, file: name } end end @@ -120,17 +116,26 @@ table_names = fixtures_args_parser.call ENV["FILE"] if ENV["FILE"] 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 = table_names.map{ |option| option.merge dir: ENV["D"] } if ENV["D"] - # read mode だとcsvもyaml存在しないファイルは返さない + + table_names = table_names.map{ |option| option.merge minus: ENV["MINUS"].to_s.split(",") } if ENV["MINUS"] + table_names = table_names.map{ |option| option.merge plus: ENV["PLUS"].to_s.split(",") } if ENV["PLUS"] + + table_names = table_names.map{ |option| option.merge silent: true } if ENV["OPTION"].to_s.split(",").include?("silent") + table_names = table_names.map{ |option| option.merge unfilter: true } if ENV["OPTION"].to_s.split(",").include?("unfilter") + table_names = table_names.map{ |option| option.merge strict: true } if ENV["OPTION"].to_s.split(",").include?("strict") + + # if mode is 'read mode' and file is not exist value is not return table_names.select! &exist if option[:mode] && option[:mode] == 'read' table_names end # check exist filename block def self.exist - return->(name){ File.exists?("#{LOAD_DIR}#{name}.csv") or File.exists?("#{LOAD_DIR}#{name}.yml") } + return->(name){ File.exists?( File.join( Config.fixture_load_directory, "#{name}.csv") ) or + File.exists?( File.join( Config.fixture_load_directory, "#{name}.yml") ) } end end end