Permalink
Browse files

rakeタスクを作成

  • Loading branch information...
1 parent 3d80a30 commit d5108501649a5585ac9e721a7b210d412a7db065 @baban committed Nov 5, 2011
View
@@ -27,19 +27,19 @@ pkg
#
# For MacOS:
#
-#.DS_Store
+.DS_Store
# For TextMate
#*.tmproj
#tmtags
# For emacs:
-#*~
-#\#*
-#.\#*
+*~
+\#*
+.\#*
# For vim:
-#*.swp
+*.swp
# For redcar:
#.redcar
View
@@ -89,7 +89,6 @@ Railsのプロジェクトに config/flextures.factory.rb というファイル
フィクスチャの読み込み時に、値を加工して読み込む事が可能になっています
例えば、次の様に記述するとusersテーブルのlast_login_dateの値を、常に現在の時間として設定できます
-
Flextures::Factory.define :users do |f|
f.last_login_date = DateTime.now
end
View
@@ -53,38 +53,3 @@ Rake::RDocTask.new do |rdoc|
rdoc.options = ["--charset", "utf-8", "--line-numbers"]
end
-namespace :db do
- namespace :flextures do
- desc "Load fixture datas fixtures directory"
- task :load => :environment do
- Flextures::ARGS.parse.each { |fmt| Flextures::Loader::load(fmt) }
- end
-
- desc "Load fixture datas fixtures directory onlu csv file"
- task :csvload => :environment do
- Flextures::ARGS.parse.each { |fmt| Flextures::Loader::csv(fmt) }
- end
-
- desc "Load fixture datas fixtures directory onlu yaml file"
- task :ymlload => :environment do
- Flextures::ARGS::parse.each { |fmt| Flextures::Loader::yml(fmt) }
- end
-
- desc "Dump data to the fixtures directory"
- task :dump => :environment do
- Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
- end
-
- desc "Dump data to the fixtures directory csv format"
- task :csvdump => :environment do
- Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
- end
-
- desc "Dump data to the fixtures directory yaml format"
- task :ymldump => :environment do
- Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::yml(fmt) }
- end
-
- end
-end
-
View
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
"VERSION",
"flextures.gemspec",
"init.rb",
+ "flextures.rb",
"lib/flextures.rb",
"lib/flextures_base_config.rb",
"lib/flextures_extension_modules.rb",
View
16 init.rb
@@ -1,16 +0,0 @@
-# encoding: utf-8
-
-require 'ostruct'
-require 'csv'
-
-# 基本設定
-require 'flextures_base_config'
-# 上書き設定ファイルの読み出し
-load "#{Rails.root.to_path}/config/flextures.config.rb"
-require 'flextures'
-require 'flextures_extension_modules'
-require 'flextures_factory'
-#require 'rspec_flextures_support.rb'
-# factory設定
-load "#{Rails.root.to_path}/config/flextures.factory.rb"
-
View
@@ -4,220 +4,13 @@
require 'csv'
# 基本設定
-require 'flextures_base_config'
-require 'flextures'
-require 'flextures_extension_modules'
-require 'flextures_factory'
-require 'rspec_flextures_support.rb'
+require 'flextures/flextures_base_config'
+require 'flextures/flextures_extension_modules'
+require 'flextures/flextures_factory'
+require 'flextures/rspec_flextures_support'
+require 'flextures/flextures_railtie' if defined? Rails
+
# 上書き設定ファイルの読み出し
-#load "#{Rails.root.to_path}/config/flextures.config.rb"
+#load "#{Rails.root.to_path}/config/flextures.config.rb" if defined? Rails
# factory設定
-#load "#{Rails.root.to_path}/config/flextures.factory.rb"
-
-module Flextures
- LOAD_DIR = Config.fixture_load_directory
- DUMP_DIR = Config.fixture_dump_directory
- # 引数解析
- module ARGS
- # 書き出し・読み込み すべきファイルとオプションを書きだす
- def self.parse option={}
- 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 = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
- table_names.first[:file]= ENV["FILE"] if ENV["FILE"] # ファイル名は最初のものしか指定できない
- table_names.first[:file]= ENV["F"] if ENV["F"]
- # read mode だとcsvもyaml存在しないファイルは返さない
- 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
-
- # テーブルモデルの作成
- def self.create_model table_name
- klass = Class.new ActiveRecord::Base
- klass.table_name=table_name
- klass
- end
-
- module Dumper
- PARENT = Flextures
-
- # 適切な型に変換
- def self.trans v
- case v
- when true; 1
- when false; 0
- else; v
- end
- end
-
- # 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"
- 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))}
- end
- end
- end
-
- # 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|
- f<< "#{table_name}_#{idx}:\n" +
- attributes.map { |column|
- v = trans row.send(column)
- " #{column}: #{v}\n"
- }.join
- end
- end
- end
- end
-
- module Loader
- PARENT = Flextures
-
- # 型に応じて勝手にdefault値を設定する
- COMPLETER = {
- binary:->{ 0 },
- boolean:->{ false },
- date:->{ DateTime.now },
- datetime:->{ DateTime.now },
- decimal:->{ 0 },
- float:->{ 0.0 },
- integer:->{ 0 },
- 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) },
- datetime:->(d){ DateTime.parse(d.to_s) },
- decimal:->(d){ d.to_i },
- float:->(d){ d.to_f },
- integer:->(d){ d.to_i },
- string:->(d){ d.to_s },
- text:->(d){ d.to_s },
- time:->(d){ DateTime.parse(d.to_s) },
- timestamp:->(d){ DateTime.parse(d.to_s) },
- }
-
- # csv 優先で存在している fixtures をロード
- def self.load format
- file_name = format[:file] || format[:table]
- dir_name = format[:dir] || LOAD_DIR
- method = nil
- 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 で使用する
- def self.flextures *fixtures
- # :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| Flextures::Loader::load table: table_name }
- fixtures_hash.each{ |k,v| Flextures::Loader::load table: k, file: v } if fixtures_hash
- fixtures
- end
-
- # CSVのデータをロードする
- 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]
- #filter2 = create_filter2 klass.columns, Factory[table_name]
- klass.delete_all
- CSV.open( inpfile ) do |csv|
- keys = csv.shift # keyの設定
- warning "CSV", attributes, keys
- csv.each do |values|
- klass.create filter.call values.extend(Extensions::Array).to_hash(keys)
- end
- end
- end
-
- # YAML形式でデータをロードする
- 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|
- warning "YAML", attributes, h.keys
- klass.create filter.call h
- 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
-
- # フィクスチャから取り出した値を、加工して欲しいデータにするフィルタを作成して返す
- def self.create_filter columns, factory=nil
- # テーブルからカラム情報を取り出し
- column_hash = {}
- columns.each { |col| column_hash[col.name] = col }
- # 自動補完が必要なはずのカラム
- lack_columns = columns.select { |c| !c.null and !c.default }.map &:name
- # ハッシュを受け取って、必要な値に加工してからハッシュで返す
- return->(h){
- h.select! { |k,v| column_hash[k] } # テーブルに存在しないキーが定義されているときは削除
- # 値がnilでないなら型をDBで適切なものに変更
- h.each{ |k,v| nil==v || h[k] = TRANSLATER[column_hash[k].type].call(v) }
- # FactoryFilterを動作させる
- st = OpenStruct.new(h)
- factory.call(st) if factory
- h = st.to_hash
- # 値がnilの列にデフォルト値を補間
- lack_columns.each { |k| nil==h[k] && h[k] = COMPLETER[column_hash[k].type].call }
- h
- }
- end
-
- end
-end
-
+#load "#{Rails.root.to_path}/config/flextures.factory.rb" if defined? Rails
@@ -0,0 +1,12 @@
+
+
+namespace :db do
+ namespace :flextures do
+ task :load do
+ p "load"
+ end
+ task :dump do
+ p :dump
+ end
+ end
+end
File renamed without changes.
@@ -0,0 +1,8 @@
+
+module Flextures
+ class Railtie < Rails::Railtie
+ rake_tasks do
+ load "flextures/flextures.rake"
+ end
+ end
+end

0 comments on commit d510850

Please sign in to comment.