flextures 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -6
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/flextures.gemspec +9 -8
- data/lib/flextures.rb +8 -209
- data/lib/flextures/flextures.rake +44 -0
- data/lib/flextures/flextures.rb +220 -0
- data/lib/{flextures_base_config.rb → flextures/flextures_base_config.rb} +20 -20
- data/lib/{flextures_extension_modules.rb → flextures/flextures_extension_modules.rb} +31 -28
- data/lib/flextures/flextures_factory.rb +27 -0
- data/lib/flextures/flextures_railtie.rb +9 -0
- data/lib/flextures/rspec_flextures_support.rb +13 -0
- metadata +18 -17
- data/init.rb +0 -16
- data/lib/flextures_factory.rb +0 -19
- data/lib/rspec_flextures_support.rb +0 -9
- data/lib/tasks/flextures.rake +0 -32
data/README.rdoc
CHANGED
@@ -19,9 +19,12 @@ Rails標準のfixtureの不満点を解消するために作成しました
|
|
19
19
|
|
20
20
|
== インストール方法
|
21
21
|
|
22
|
-
RailsのPlug-in
|
23
|
-
|
24
|
-
|
22
|
+
RailsのPlug-inとして使われることを想定しています
|
23
|
+
gem化されているので、bundlerで次のように記述して、普通にbundle install してください
|
24
|
+
|
25
|
+
gem "flextures"
|
26
|
+
|
27
|
+
ちなみに開発環境はruby1.9.2、rails3環境を想定しています
|
25
28
|
|
26
29
|
== 使い方
|
27
30
|
|
@@ -47,12 +50,12 @@ Usersモデルのfixture(users.csvか users.yml)をロードする
|
|
47
50
|
その他オプションは以下のとおりな感じです:
|
48
51
|
|
49
52
|
TABLE : テーブル名を指定してロード。テーブル名はカンマ切りで複数指定が可能
|
50
|
-
T : TABLEのエイリアス
|
51
53
|
MODEL : モデル名を指定してロード。モデル名はカンマ区切りで複数指定が可能
|
52
|
-
M : モデル名指定のエイリアス
|
53
54
|
DIR : フィクスチャをロード&ダンプするディレクトリを指定する
|
54
|
-
D : ディレクトリ指定のエイリアス
|
55
55
|
FILE : ロード、ダンプをするファイル名をRails標準以外のもので直接指定(Userモデルのusers.csv以外を指定)
|
56
|
+
T : TABLEのエイリアス
|
57
|
+
M : モデル名指定のエイリアス
|
58
|
+
D : ディレクトリ指定のエイリアス
|
56
59
|
F : ファイル名指定のエイリアス
|
57
60
|
|
58
61
|
yamlのファイルはロードしたいけどcsvはロードをしたくない
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.1
|
data/flextures.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{flextures}
|
8
|
-
s.version = "0.9.
|
8
|
+
s.version = "0.9.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["baban"]
|
12
|
-
s.date = %q{2011-11-
|
12
|
+
s.date = %q{2011-11-06}
|
13
13
|
s.description = %q{load and dump fixtures}
|
14
14
|
s.email = %q{babanba.n@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,13 +27,14 @@ Gem::Specification.new do |s|
|
|
27
27
|
"Rakefile",
|
28
28
|
"VERSION",
|
29
29
|
"flextures.gemspec",
|
30
|
-
"init.rb",
|
31
30
|
"lib/flextures.rb",
|
32
|
-
"lib/
|
33
|
-
"lib/
|
34
|
-
"lib/
|
35
|
-
"lib/
|
36
|
-
"lib/
|
31
|
+
"lib/flextures/flextures.rake",
|
32
|
+
"lib/flextures/flextures.rb",
|
33
|
+
"lib/flextures/flextures_base_config.rb",
|
34
|
+
"lib/flextures/flextures_extension_modules.rb",
|
35
|
+
"lib/flextures/flextures_factory.rb",
|
36
|
+
"lib/flextures/flextures_railtie.rb",
|
37
|
+
"lib/flextures/rspec_flextures_support.rb",
|
37
38
|
"test/helper.rb",
|
38
39
|
"test/test_flextures.rb"
|
39
40
|
]
|
data/lib/flextures.rb
CHANGED
@@ -1,209 +1,8 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def self.parse option={}
|
10
|
-
table_names = ""
|
11
|
-
table_names = ENV["TABLE"].split(",") if ENV["TABLE"]
|
12
|
-
table_names = ENV["T"].split(",") if ENV["T"]
|
13
|
-
table_names = ENV["MODEL"].constantize.table_name.split(",") if ENV["MODEL"]
|
14
|
-
table_names = ENV["M"].constantize.table_name.split(",") if ENV["M"]
|
15
|
-
table_names = ActiveRecord::Base.connection.tables if ""==table_names
|
16
|
-
table_names = table_names.map{ |name| { table: name } }
|
17
|
-
table_names = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
|
18
|
-
table_names.first[:file]= ENV["FILE"] if ENV["FILE"] # �t�@�C�����͍ŏ��̂��̂����w��ł��Ȃ�
|
19
|
-
table_names.first[:file]= ENV["F"] if ENV["F"]
|
20
|
-
# read mode ����csv��yaml���݂��Ȃ��t�@�C���͕Ԃ��Ȃ�
|
21
|
-
table_names.select! &exist if option[:mode] && option[:mode].to_sym == :read
|
22
|
-
table_names
|
23
|
-
end
|
24
|
-
|
25
|
-
# ���݂��Ă���t�@�C���ōi�荞�ށ@
|
26
|
-
def self.exist
|
27
|
-
return->(name){ File.exists?("#{LOAD_DIR}#{name}.csv") or File.exists?("#{LOAD_DIR}#{name}.yml") }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# �e�[�u�����f���̍쐬
|
32
|
-
def self.create_model table_name
|
33
|
-
klass = Class.new ActiveRecord::Base
|
34
|
-
klass.table_name=table_name
|
35
|
-
klass
|
36
|
-
end
|
37
|
-
|
38
|
-
module Dumper
|
39
|
-
PARENT = Flextures
|
40
|
-
|
41
|
-
# �K�Ȍ^�ɕϊ�
|
42
|
-
def self.trans v
|
43
|
-
case v
|
44
|
-
when true; 1
|
45
|
-
when false; 0
|
46
|
-
else; v
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# csv �� fixtures �� dump
|
51
|
-
def self.csv format
|
52
|
-
table_name = format[:table]
|
53
|
-
file_name = format[:file] || table_name
|
54
|
-
dir_name = format[:dir] || DUMP_DIR
|
55
|
-
outfile = "#{dir_name}#{file_name}.csv"
|
56
|
-
klass = PARENT.create_model(table_name)
|
57
|
-
attributes = klass.columns.map { |colum| colum.name }
|
58
|
-
|
59
|
-
CSV.open(outfile,'w') do |csv|
|
60
|
-
csv<< attributes
|
61
|
-
klass.all.each do |row|
|
62
|
-
csv<< attributes.map { |column| trans(row.send(column))}
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# yaml �� fixtures �� dump
|
68
|
-
def self.yml format
|
69
|
-
table_name = format[:table]
|
70
|
-
file_name = format[:file] || table_name
|
71
|
-
dir_name = format[:dir] || DUMP_DIR
|
72
|
-
outfile = "#{dir_name}#{file_name}.yml"
|
73
|
-
klass = PARENT::create_model(table_name)
|
74
|
-
attributes = klass.columns.map { |colum| colum.name }
|
75
|
-
|
76
|
-
File.open(outfile,"w") do |f|
|
77
|
-
klass.all.each_with_index do |row,idx|
|
78
|
-
f<< "#{table_name}_#{idx}:\n" +
|
79
|
-
attributes.map { |column|
|
80
|
-
v = trans row.send(column)
|
81
|
-
" #{column}: #{v}\n"
|
82
|
-
}.join
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
module Loader
|
89
|
-
PARENT = Flextures
|
90
|
-
|
91
|
-
# �^�ɉ����ď����default�l��ݒ肷��
|
92
|
-
COMPLETER = {
|
93
|
-
binary:->{ 0 },
|
94
|
-
boolean:->{ false },
|
95
|
-
date:->{ DateTime.now },
|
96
|
-
datetime:->{ DateTime.now },
|
97
|
-
decimal:->{ 0 },
|
98
|
-
float:->{ 0.0 },
|
99
|
-
integer:->{ 0 },
|
100
|
-
string:->{ "" },
|
101
|
-
text:->{ "" },
|
102
|
-
time:->{ DateTime.now },
|
103
|
-
timestamp:->{ DateTime.now },
|
104
|
-
}
|
105
|
-
# �^�̕ϊ����s��
|
106
|
-
TRANSLATER = {
|
107
|
-
binary:->(d){ d.to_i },
|
108
|
-
boolean:->(d){ (0==d || ""==d || !d) ? false : true },
|
109
|
-
date:->(d){ Date.parse(d.to_s) },
|
110
|
-
datetime:->(d){ DateTime.parse(d.to_s) },
|
111
|
-
decimal:->(d){ d.to_i },
|
112
|
-
float:->(d){ d.to_f },
|
113
|
-
integer:->(d){ d.to_i },
|
114
|
-
string:->(d){ d.to_s },
|
115
|
-
text:->(d){ d.to_s },
|
116
|
-
time:->(d){ DateTime.parse(d.to_s) },
|
117
|
-
timestamp:->(d){ DateTime.parse(d.to_s) },
|
118
|
-
}
|
119
|
-
|
120
|
-
# csv �D��ő��݂��Ă��� fixtures �����[�h
|
121
|
-
def self.load format
|
122
|
-
file_name = format[:file] || format[:table]
|
123
|
-
dir_name = format[:dir] || LOAD_DIR
|
124
|
-
method = nil
|
125
|
-
method = :csv if File.exist? "#{dir_name}#{file_name}.csv"
|
126
|
-
method = :yml if File.exist? "#{dir_name}#{file_name}.yml"
|
127
|
-
self::send(method, format) if method
|
128
|
-
end
|
129
|
-
|
130
|
-
# fixtures���܂Ƃ߂ă��[�h�A��Ƀe�X�gtest/unit, rspec �Ŏg�p����
|
131
|
-
def self.flextures *fixtures
|
132
|
-
# :all�ł��ׂĂ�fixture�f
|
133
|
-
fixtures = ActiveRecord::Base.connection.tables if fixtures.size== 1 and :all == fixtures.first
|
134
|
-
|
135
|
-
fixtures_hash = fixtures.pop if fixtures.last and fixtures.last.is_a? Hash # �n�b�V�����o��
|
136
|
-
fixtures.each{ |table_name| Flextures::Loader::load table: table_name }
|
137
|
-
fixtures_hash.each{ |k,v| Flextures::Loader::load table: k, file: v } if fixtures_hash
|
138
|
-
fixtures
|
139
|
-
end
|
140
|
-
|
141
|
-
# CSV�̃f�[�^�����[�h����
|
142
|
-
def self.csv format
|
143
|
-
table_name = format[:table].to_s
|
144
|
-
file_name = format[:file] || table_name
|
145
|
-
dir_name = format[:dir] || LOAD_DIR
|
146
|
-
inpfile = "#{dir_name}#{file_name}.csv"
|
147
|
-
|
148
|
-
klass = PARENT::create_model table_name
|
149
|
-
attributes = klass.columns.map &:name
|
150
|
-
filter = create_filter klass.columns, Factory[table_name]
|
151
|
-
#filter2 = create_filter2 klass.columns, Factory[table_name]
|
152
|
-
klass.delete_all
|
153
|
-
CSV.open( inpfile ) do |csv|
|
154
|
-
keys = csv.shift # key�̐ݒ�
|
155
|
-
warning "CSV", attributes, keys
|
156
|
-
csv.each do |values|
|
157
|
-
klass.create filter.call values.extend(Extensions::Array).to_hash(keys)
|
158
|
-
end
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
# YAML�`���Ńf�[�^�����[�h����
|
163
|
-
def self.yml format
|
164
|
-
table_name = format[:table].to_s
|
165
|
-
file_name = format[:file] || table_name
|
166
|
-
dir_name = format[:dir] || LOAD_DIR
|
167
|
-
inpfile = "#{dir_name}#{file_name}.yml"
|
168
|
-
|
169
|
-
klass = PARENT::create_model table_name
|
170
|
-
attributes = klass.columns.map &:name
|
171
|
-
filter = create_filter klass.columns, Factory[table_name]
|
172
|
-
klass.delete_all
|
173
|
-
YAML.load(File.open(inpfile)).each do |k,h|
|
174
|
-
warning "YAML", attributes, h.keys
|
175
|
-
klass.create filter.call h
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
# �������J���������m����b�Z�[�W���o������
|
180
|
-
def self.warning format, attributes, keys
|
181
|
-
(attributes-keys).each { |name| print "Warning: #{format} colum is missing! [#{name}]\n" }
|
182
|
-
(keys-attributes).each { |name| print "Warning: #{format} colum is left over! [#{name}]\n" }
|
183
|
-
end
|
184
|
-
|
185
|
-
# �t�B�N�X�`��������o�����l���A���H���ė~�����f�[�^�ɂ���t�B���^���쐬���ĕԂ�
|
186
|
-
def self.create_filter columns, factory=nil
|
187
|
-
# �e�[�u������J�����������o��
|
188
|
-
column_hash = {}
|
189
|
-
columns.each { |col| column_hash[col.name] = col }
|
190
|
-
# �����⊮���K�v�Ȃ͂��̃J����
|
191
|
-
lack_columns = columns.select { |c| !c.null and !c.default }.map &:name
|
192
|
-
# �n�b�V��������āA�K�v�Ȓl�ɉ��H���Ă���n�b�V���ŕԂ�
|
193
|
-
->(h){
|
194
|
-
h.select! { |k,v| column_hash[k] } # �e�[�u���ɑ��݂��Ȃ��L�[����`����Ă���Ƃ��͍폜
|
195
|
-
# �l��nil�łȂ��Ȃ�^��DB�œK�Ȃ��̂ɕύX
|
196
|
-
h.each{ |k,v| nil==v || h[k] = TRANSLATER[column_hash[k].type].call(v) }
|
197
|
-
# FactoryFilter�삳����
|
198
|
-
st = OpenStruct.new(h)
|
199
|
-
factory.call(st) if factory
|
200
|
-
h = st.to_hash
|
201
|
-
# �l��nil�̗�Ƀf�t�H���g�l����
|
202
|
-
lack_columns.each { |k| nil==h[k] && h[k] = COMPLETER[column_hash[k].type].call }
|
203
|
-
h
|
204
|
-
}
|
205
|
-
end
|
206
|
-
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'flextures/flextures_base_config'
|
4
|
+
require 'flextures/flextures_extension_modules'
|
5
|
+
require 'flextures/flextures'
|
6
|
+
require 'flextures/flextures_factory'
|
7
|
+
require 'flextures/flextures_railtie' if defined? Rails
|
8
|
+
require 'flextures/rspec_flextures_support'
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
require 'flextures/flextures_base_config'
|
7
|
+
require 'flextures/flextures'
|
8
|
+
require 'flextures/flextures_extension_modules'
|
9
|
+
require 'flextures/flextures_factory'
|
10
|
+
#require 'flextures/rspec_flextures_support'
|
11
|
+
|
12
|
+
namespace :db do
|
13
|
+
namespace :flextures do
|
14
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
15
|
+
task :dump => :environment do
|
16
|
+
Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
20
|
+
task :csvdump => :environment do
|
21
|
+
#Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
25
|
+
task :ymldump => :environment do
|
26
|
+
#Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::yml(fmt) }
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
30
|
+
task :load => :environment do
|
31
|
+
#Flextures::ARGS.parse.each { |fmt| Flextures::Loader::load(fmt) }
|
32
|
+
end
|
33
|
+
|
34
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
35
|
+
task :csvload => :environment do
|
36
|
+
#Flextures::ARGS.parse.each { |fmt| Flextures::Loader::csv(fmt) }
|
37
|
+
end
|
38
|
+
|
39
|
+
desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
40
|
+
task :ymlload => :environment do
|
41
|
+
#Flextures::ARGS::parse.each { |fmt| Flextures::Loader::yml(fmt) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
require 'flextures/flextures_base_config'
|
7
|
+
require 'flextures/flextures_extension_modules'
|
8
|
+
require 'flextures/flextures_factory'
|
9
|
+
require 'flextures/rspec_flextures_support.rb' if defined?(RSpec)
|
10
|
+
load "#{Rails.root.to_path}/config/flextures.config.rb" if defined?(Rails) and Rails.root
|
11
|
+
load "#{Rails.root.to_path}/config/flextures.factory.rb" if defined?(Rails) and Rails.root
|
12
|
+
|
13
|
+
module Flextures
|
14
|
+
LOAD_DIR = Config.fixture_load_directory
|
15
|
+
DUMP_DIR = Config.fixture_dump_directory
|
16
|
+
|
17
|
+
# 引数解析
|
18
|
+
module ARGS
|
19
|
+
# 書き出し・読み込み すべきファイルとオプションを書きだす
|
20
|
+
def self.parse option={}
|
21
|
+
table_names = ""
|
22
|
+
table_names = ENV["TABLE"].split(",") if ENV["TABLE"]
|
23
|
+
table_names = ENV["T"].split(",") if ENV["T"]
|
24
|
+
table_names = ENV["MODEL"].constantize.table_name.split(",") if ENV["MODEL"]
|
25
|
+
table_names = ENV["M"].constantize.table_name.split(",") if ENV["M"]
|
26
|
+
table_names = ActiveRecord::Base.connection.tables if ""==table_names
|
27
|
+
table_names = table_names.map{ |name| { table: name } }
|
28
|
+
table_names = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
|
29
|
+
table_names.first[:file]= ENV["FILE"] if ENV["FILE"] # ファイル名は最初のものしか指定できない
|
30
|
+
table_names.first[:file]= ENV["F"] if ENV["F"]
|
31
|
+
# read mode だとcsvもyaml存在しないファイルは返さない
|
32
|
+
table_names.select! &exist if option[:mode] && option[:mode].to_sym == :read
|
33
|
+
table_names
|
34
|
+
end
|
35
|
+
|
36
|
+
# 存在しているファイルで絞り込む
|
37
|
+
def self.exist
|
38
|
+
return->(name){ File.exists?("#{LOAD_DIR}#{name}.csv") or File.exists?("#{LOAD_DIR}#{name}.yml") }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# テーブルモデルの作成
|
43
|
+
def self.create_model table_name
|
44
|
+
klass = Class.new ActiveRecord::Base
|
45
|
+
klass.table_name=table_name
|
46
|
+
klass
|
47
|
+
end
|
48
|
+
|
49
|
+
module Dumper
|
50
|
+
PARENT = Flextures
|
51
|
+
|
52
|
+
# 適切な型に変換
|
53
|
+
def self.trans v
|
54
|
+
case v
|
55
|
+
when true; 1
|
56
|
+
when false; 0
|
57
|
+
else; v
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# csv で fixtures を dump
|
62
|
+
def self.csv format
|
63
|
+
table_name = format[:table]
|
64
|
+
file_name = format[:file] || table_name
|
65
|
+
dir_name = format[:dir] || DUMP_DIR
|
66
|
+
outfile = "#{dir_name}#{file_name}.csv"
|
67
|
+
klass = PARENT.create_model(table_name)
|
68
|
+
attributes = klass.columns.map { |colum| colum.name }
|
69
|
+
|
70
|
+
CSV.open(outfile,'w') do |csv|
|
71
|
+
csv<< attributes
|
72
|
+
klass.all.each do |row|
|
73
|
+
csv<< attributes.map { |column| trans(row.send(column))}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# yaml で fixtures を dump
|
79
|
+
def self.yml format
|
80
|
+
table_name = format[:table]
|
81
|
+
file_name = format[:file] || table_name
|
82
|
+
dir_name = format[:dir] || DUMP_DIR
|
83
|
+
outfile = "#{dir_name}#{file_name}.yml"
|
84
|
+
klass = PARENT::create_model(table_name)
|
85
|
+
attributes = klass.columns.map { |colum| colum.name }
|
86
|
+
|
87
|
+
File.open(outfile,"w") do |f|
|
88
|
+
klass.all.each_with_index do |row,idx|
|
89
|
+
f<< "#{table_name}_#{idx}:¥n" +
|
90
|
+
attributes.map { |column|
|
91
|
+
v = trans row.send(column)
|
92
|
+
" #{column}: #{v}¥n"
|
93
|
+
}.join
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
module Loader
|
100
|
+
PARENT = Flextures
|
101
|
+
|
102
|
+
# 型に応じて勝手にdefault値を設定する
|
103
|
+
COMPLETER = {
|
104
|
+
binary:->{ 0 },
|
105
|
+
boolean:->{ false },
|
106
|
+
date:->{ DateTime.now },
|
107
|
+
datetime:->{ DateTime.now },
|
108
|
+
decimal:->{ 0 },
|
109
|
+
float:->{ 0.0 },
|
110
|
+
integer:->{ 0 },
|
111
|
+
string:->{ "" },
|
112
|
+
text:->{ "" },
|
113
|
+
time:->{ DateTime.now },
|
114
|
+
timestamp:->{ DateTime.now },
|
115
|
+
}
|
116
|
+
# 型の変換を行う
|
117
|
+
TRANSLATER = {
|
118
|
+
binary:->(d){ d.to_i },
|
119
|
+
boolean:->(d){ (0==d || ""==d || !d) ? false : true },
|
120
|
+
date:->(d){ Date.parse(d.to_s) },
|
121
|
+
datetime:->(d){ DateTime.parse(d.to_s) },
|
122
|
+
decimal:->(d){ d.to_i },
|
123
|
+
float:->(d){ d.to_f },
|
124
|
+
integer:->(d){ d.to_i },
|
125
|
+
string:->(d){ d.to_s },
|
126
|
+
text:->(d){ d.to_s },
|
127
|
+
time:->(d){ DateTime.parse(d.to_s) },
|
128
|
+
timestamp:->(d){ DateTime.parse(d.to_s) },
|
129
|
+
}
|
130
|
+
|
131
|
+
# csv 優先で存在している fixtures をロード
|
132
|
+
def self.load format
|
133
|
+
file_name = format[:file] || format[:table]
|
134
|
+
dir_name = format[:dir] || LOAD_DIR
|
135
|
+
method = nil
|
136
|
+
method = :csv if File.exist? "#{dir_name}#{file_name}.csv"
|
137
|
+
method = :yml if File.exist? "#{dir_name}#{file_name}.yml"
|
138
|
+
self::send(method, format) if method
|
139
|
+
end
|
140
|
+
|
141
|
+
# fixturesをまとめてロード、主にテストtest/unit, rspec で使用する
|
142
|
+
def self.flextures *fixtures
|
143
|
+
# :allですべてのfixtureを反映
|
144
|
+
fixtures = ActiveRecord::Base.connection.tables if fixtures.size== 1 and :all == fixtures.first
|
145
|
+
|
146
|
+
fixtures_hash = fixtures.pop if fixtures.last and fixtures.last.is_a? Hash # ハッシュ取り出し
|
147
|
+
fixtures.each{ |table_name| Flextures::Loader::load table: table_name }
|
148
|
+
fixtures_hash.each{ |k,v| Flextures::Loader::load table: k, file: v } if fixtures_hash
|
149
|
+
fixtures
|
150
|
+
end
|
151
|
+
|
152
|
+
# CSVのデータをロードする
|
153
|
+
def self.csv format
|
154
|
+
table_name = format[:table].to_s
|
155
|
+
file_name = format[:file] || table_name
|
156
|
+
dir_name = format[:dir] || LOAD_DIR
|
157
|
+
inpfile = "#{dir_name}#{file_name}.csv"
|
158
|
+
|
159
|
+
klass = PARENT::create_model table_name
|
160
|
+
attributes = klass.columns.map &:name
|
161
|
+
filter = create_filter klass.columns, Factory[table_name]
|
162
|
+
#filter2 = create_filter2 klass.columns, Factory[table_name]
|
163
|
+
klass.delete_all
|
164
|
+
CSV.open( inpfile ) do |csv|
|
165
|
+
keys = csv.shift # keyの設定
|
166
|
+
warning "CSV", attributes, keys
|
167
|
+
csv.each do |values|
|
168
|
+
klass.create filter.call values.extend(Extensions::Array).to_hash(keys)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# YAML形式でデータをロードする
|
174
|
+
def self.yml format
|
175
|
+
table_name = format[:table].to_s
|
176
|
+
file_name = format[:file] || table_name
|
177
|
+
dir_name = format[:dir] || LOAD_DIR
|
178
|
+
inpfile = "#{dir_name}#{file_name}.yml"
|
179
|
+
|
180
|
+
klass = PARENT::create_model table_name
|
181
|
+
attributes = klass.columns.map &:name
|
182
|
+
filter = create_filter klass.columns, Factory[table_name]
|
183
|
+
klass.delete_all
|
184
|
+
YAML.load(File.open(inpfile)).each do |k,h|
|
185
|
+
warning "YAML", attributes, h.keys
|
186
|
+
klass.create filter.call h
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# 欠けたカラムを検知してメッセージを出しておく
|
191
|
+
def self.warning format, attributes, keys
|
192
|
+
(attributes-keys).each { |name| print "Warning: #{format} colum is missing! [#{name}]¥n" }
|
193
|
+
(keys-attributes).each { |name| print "Warning: #{format} colum is left over! [#{name}]¥n" }
|
194
|
+
end
|
195
|
+
|
196
|
+
# フィクスチャから取り出した値を、加工して欲しいデータにするフィルタを作成して返す
|
197
|
+
def self.create_filter columns, factory=nil
|
198
|
+
# テーブルからカラム情報を取り出し
|
199
|
+
column_hash = {}
|
200
|
+
columns.each { |col| column_hash[col.name] = col }
|
201
|
+
# 自動補完が必要なはずのカラム
|
202
|
+
lack_columns = columns.select { |c| !c.null and !c.default }.map &:name
|
203
|
+
# ハッシュを受け取って、必要な値に加工してからハッシュで返す
|
204
|
+
return->(h){
|
205
|
+
h.select! { |k,v| column_hash[k] } # テーブルに存在しないキーが定義されているときは削除
|
206
|
+
# 値がnilでないなら型をDBで適切なものに変更
|
207
|
+
h.each{ |k,v| nil==v || h[k] = TRANSLATER[column_hash[k].type].call(v) }
|
208
|
+
# FactoryFilterを動作させる
|
209
|
+
st = OpenStruct.new(h)
|
210
|
+
factory.call(st) if factory
|
211
|
+
h = st.to_hash
|
212
|
+
# 値がnilの列にデフォルト値を補間
|
213
|
+
lack_columns.each { |k| nil==h[k] && h[k] = COMPLETER[column_hash[k].type].call }
|
214
|
+
h
|
215
|
+
}
|
216
|
+
end
|
217
|
+
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
@@ -1,20 +1,20 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
#
|
4
|
-
module Flextures
|
5
|
-
module Config
|
6
|
-
@@configs={
|
7
|
-
fixture_load_directory: "spec/fixtures/",
|
8
|
-
fixture_dump_directory: "spec/fixtures/",
|
9
|
-
}
|
10
|
-
#
|
11
|
-
class<< self
|
12
|
-
@@configs.each do |setting_key, setting_value|
|
13
|
-
define_method setting_key do @@configs[setting_key] end
|
14
|
-
define_method "#{setting_key}=" do |arg| @@configs[setting_key]=arg end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# 基本設定を記述する
|
4
|
+
module Flextures
|
5
|
+
module Config
|
6
|
+
@@configs={
|
7
|
+
fixture_load_directory: "spec/fixtures/",
|
8
|
+
fixture_dump_directory: "spec/fixtures/",
|
9
|
+
}
|
10
|
+
# ハッシュをsetter、getterに変換
|
11
|
+
class<< self
|
12
|
+
@@configs.each do |setting_key, setting_value|
|
13
|
+
define_method setting_key do @@configs[setting_key] end
|
14
|
+
define_method "#{setting_key}=" do |arg| @@configs[setting_key]=arg end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
@@ -1,28 +1,31 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
module Flextures
|
7
|
+
# Plug-in 内部拡張
|
8
|
+
class OpenStruct < ::OpenStruct
|
9
|
+
# hashに変化させる
|
10
|
+
def to_hash
|
11
|
+
h={}
|
12
|
+
(self.methods - OpenStruct.new.methods)
|
13
|
+
.select{ |name| name.match(/¥w+=/) }
|
14
|
+
.map{ |name| name.to_s.gsub(/=/,'').to_sym }
|
15
|
+
.each{ |k| h[k]=self.send(k) }
|
16
|
+
h
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Extensions
|
21
|
+
module Array
|
22
|
+
def to_hash keys
|
23
|
+
h = {}
|
24
|
+
[keys,self].transpose.each{ |k,v| h[k]=v }
|
25
|
+
h
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
require 'flextures/flextures_base_config'
|
7
|
+
require 'flextures/flextures_extension_modules'
|
8
|
+
require 'flextures/flextures'
|
9
|
+
require 'flextures/flextures_factory'
|
10
|
+
|
11
|
+
module Flextures
|
12
|
+
# ロードするデータを必要に応じて加工する
|
13
|
+
class Factory
|
14
|
+
FACTORIES={}
|
15
|
+
# Factory を定義
|
16
|
+
def self.define table_name, &block
|
17
|
+
FACTORIES[table_name.to_sym]=block
|
18
|
+
end
|
19
|
+
|
20
|
+
# Factoryを取得
|
21
|
+
def self.get table_name
|
22
|
+
FACTORIES[table_name.to_sym]
|
23
|
+
end
|
24
|
+
def self.[](table_name); self.get(table_name); end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: flextures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,12 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-06 00:00:00.000000000 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: shoulda
|
17
|
-
requirement: &
|
17
|
+
requirement: &87123700 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :development
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *87123700
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: bundler
|
28
|
-
requirement: &
|
28
|
+
requirement: &87123460 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 1.0.0
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *87123460
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: jeweler
|
39
|
-
requirement: &
|
39
|
+
requirement: &87123220 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: 1.6.4
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *87123220
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rcov
|
50
|
-
requirement: &
|
50
|
+
requirement: &87122980 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,7 +55,7 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *87122980
|
59
59
|
description: load and dump fixtures
|
60
60
|
email: babanba.n@gmail.com
|
61
61
|
executables: []
|
@@ -74,13 +74,14 @@ files:
|
|
74
74
|
- Rakefile
|
75
75
|
- VERSION
|
76
76
|
- flextures.gemspec
|
77
|
-
- init.rb
|
78
77
|
- lib/flextures.rb
|
79
|
-
- lib/
|
80
|
-
- lib/
|
81
|
-
- lib/
|
82
|
-
- lib/
|
83
|
-
- lib/
|
78
|
+
- lib/flextures/flextures.rake
|
79
|
+
- lib/flextures/flextures.rb
|
80
|
+
- lib/flextures/flextures_base_config.rb
|
81
|
+
- lib/flextures/flextures_extension_modules.rb
|
82
|
+
- lib/flextures/flextures_factory.rb
|
83
|
+
- lib/flextures/flextures_railtie.rb
|
84
|
+
- lib/flextures/rspec_flextures_support.rb
|
84
85
|
- test/helper.rb
|
85
86
|
- test/test_flextures.rb
|
86
87
|
has_rdoc: true
|
@@ -99,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
99
100
|
version: '0'
|
100
101
|
segments:
|
101
102
|
- 0
|
102
|
-
hash:
|
103
|
+
hash: -599778417
|
103
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
105
|
none: false
|
105
106
|
requirements:
|
data/init.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'ostruct'
|
4
|
-
require 'csv'
|
5
|
-
|
6
|
-
# ��������
|
7
|
-
require 'flextures_base_config'
|
8
|
-
# �������ե�������ɤ߽Ф�
|
9
|
-
require "#{Rails.root.to_path}/config/flextures.config.rb" if File.exist? "#{Rails.root.to_path}/config/flextures.config.rb"
|
10
|
-
require 'flextures'
|
11
|
-
require 'flextures_extension_modules'
|
12
|
-
require 'flextures_factory'
|
13
|
-
require 'rspec_flextures_support.rb'
|
14
|
-
# factory����
|
15
|
-
require "#{Rails.root.to_path}/config/flextures.factory.rb" if File.exist? "#{Rails.root.to_path}/config/flextures.factory.rb"
|
16
|
-
|
data/lib/flextures_factory.rb
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module Flextures
|
4
|
-
# �����ɤ���ǡ�����ɬ�פ˱����Ʋù�����
|
5
|
-
class Factory
|
6
|
-
FACTORIES={}
|
7
|
-
# Factory �����
|
8
|
-
def self.define table_name, &block
|
9
|
-
FACTORIES[table_name.to_sym]=block
|
10
|
-
end
|
11
|
-
|
12
|
-
# Factory�����
|
13
|
-
def self.get table_name
|
14
|
-
FACTORIES[table_name.to_sym]
|
15
|
-
end
|
16
|
-
def self.[](table_name); self.get(table_name); end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
data/lib/tasks/flextures.rake
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
namespace :db do
|
4
|
-
namespace :flextures do
|
5
|
-
#desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
6
|
-
task :dump => :environment do
|
7
|
-
Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
|
8
|
-
end
|
9
|
-
|
10
|
-
#desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
11
|
-
task :csvdump => :environment do
|
12
|
-
Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::csv(fmt) }
|
13
|
-
end
|
14
|
-
|
15
|
-
#desc "Dump data to the test/fixtures/ directory. Use TABLE=table_name"
|
16
|
-
task :ymldump => :environment do
|
17
|
-
Flextures::ARGS.parse.each { |fmt| Flextures::Dumper::yml(fmt) }
|
18
|
-
end
|
19
|
-
|
20
|
-
task :load => :environment do
|
21
|
-
Flextures::ARGS.parse.each { |fmt| Flextures::Loader::load(fmt) }
|
22
|
-
end
|
23
|
-
|
24
|
-
task :csvload => :environment do
|
25
|
-
Flextures::ARGS.parse.each { |fmt| Flextures::Loader::csv(fmt) }
|
26
|
-
end
|
27
|
-
|
28
|
-
task :ymlload => :environment do
|
29
|
-
Flextures::ARGS::parse.each { |fmt| Flextures::Loader::yml(fmt) }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|