flextures 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/CHANGELOG +2 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +20 -0
- data/History.txt +5 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +92 -0
- data/Rakefile +56 -0
- data/VERSION +1 -0
- data/flextures.gemspec +67 -0
- data/init.rb +16 -0
- data/lib/flextures.rb +209 -0
- data/lib/flextures_base_config.rb +20 -0
- data/lib/flextures_extension_modules.rb +28 -0
- data/lib/flextures_factory.rb +19 -0
- data/lib/rspec_flextures_support.rb +9 -0
- data/lib/tasks/flextures.rake +32 -0
- data/test/helper.rb +18 -0
- data/test/test_flextures.rb +7 -0
- metadata +115 -0
data/.document
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.6.4)
|
6
|
+
bundler (~> 1.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
rake (0.9.2)
|
10
|
+
rcov (0.9.11)
|
11
|
+
shoulda (2.11.3)
|
12
|
+
|
13
|
+
PLATFORMS
|
14
|
+
ruby
|
15
|
+
|
16
|
+
DEPENDENCIES
|
17
|
+
bundler (~> 1.0.0)
|
18
|
+
jeweler (~> 1.6.4)
|
19
|
+
rcov
|
20
|
+
shoulda
|
data/History.txt
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 baban
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
= flextures
|
2
|
+
|
3
|
+
== Abstruct
|
4
|
+
|
5
|
+
このplug-inは、これまで開発中で溜まっていた
|
6
|
+
Rails標準のfixtureの不満点を解消するために作成しました
|
7
|
+
基本的な操作は単純で次のコマンドで
|
8
|
+
それぞれfixtureのロードとダンプを行います
|
9
|
+
|
10
|
+
rake db:flextures:load
|
11
|
+
rake db:flextures:dump
|
12
|
+
|
13
|
+
通常のfixtureとの主な違いは次の4点です
|
14
|
+
|
15
|
+
1. yamlよりもcsvを優先する
|
16
|
+
2. migrationでテーブル構成が変わっても、ロードを停止しないで、カラムの変更点を検知して値を補完する
|
17
|
+
3. テーブル名と、fixtureのファイル名を一致させないでも自由なロード&ダンプが出来る機能
|
18
|
+
4. FactoyGirl風の読み込みフィルタで、Fixtureのデータを加工しながら読み込む事が出来る
|
19
|
+
|
20
|
+
== インストール方法
|
21
|
+
|
22
|
+
RailsのPlug-inとして実装されていますので
|
23
|
+
ダウンロードしたファイルを
|
24
|
+
vendor/plugin 以下に設置してください
|
25
|
+
|
26
|
+
== 使い方
|
27
|
+
|
28
|
+
=== rakeコマンド
|
29
|
+
|
30
|
+
次のコマンドでfixturesspec/fixtures/ 以下にあるfixtureのロード&ダンプを行います
|
31
|
+
(ディレクトリは設定ファイルで変更可能)
|
32
|
+
|
33
|
+
rake db:flextures:load
|
34
|
+
rake db:flextures:dump
|
35
|
+
|
36
|
+
rake コマンドには以下の様な書式でオプションを指定することができます
|
37
|
+
指摘出来るオプションは、ロードとダンプで共通です
|
38
|
+
|
39
|
+
テーブル名で吐き出し(Userモデルusers)
|
40
|
+
|
41
|
+
rake db:flextures:dump TABLE=users
|
42
|
+
|
43
|
+
Usersモデルのfixture(users.csvか users.yml)をロードする
|
44
|
+
|
45
|
+
rake db:flextures:load MODEL=User
|
46
|
+
|
47
|
+
その他オプションは以下のとおりな感じです:
|
48
|
+
|
49
|
+
TABLE : テーブル名を指定してロード。テーブル名はカンマ切りで複数指定が可能
|
50
|
+
T : TABLEのエイリアス
|
51
|
+
MODEL : モデル名を指定してロード。モデル名はカンマ区切りで複数指定が可能
|
52
|
+
M : モデル名指定のエイリアス
|
53
|
+
DIR : フィクスチャをロード&ダンプするディレクトリを指定する
|
54
|
+
D : ディレクトリ指定のエイリアス
|
55
|
+
FILE : ロード、ダンプをするファイル名をRails標準以外のもので直接指定(Userモデルのusers.csv以外を指定)
|
56
|
+
F : ファイル名指定のエイリアス
|
57
|
+
|
58
|
+
yamlのファイルはロードしたいけどcsvはロードをしたくない
|
59
|
+
またはその逆の場合は、以下のコマンドでそれが可能です
|
60
|
+
|
61
|
+
rake db:flextures:csvload
|
62
|
+
rake db:flextures:ymlload
|
63
|
+
rake db:flextures:csvdump
|
64
|
+
rake db:flextures:ymldump
|
65
|
+
|
66
|
+
通常の load や dump で使えるオプションはこちらでも使用可能です
|
67
|
+
|
68
|
+
=== RSpec flexture support
|
69
|
+
|
70
|
+
RSpec中でfixtures関数の代わりにfixtureのロード機能を使えます
|
71
|
+
基本的な機能は、通常のfixturesと同じですので、fixtures と同じ感覚で使用して下さい
|
72
|
+
|
73
|
+
describe ItemShopController do
|
74
|
+
flextures :users, :items
|
75
|
+
|
76
|
+
基本的な違いは、yamlよりcsvを優先する、カラムの変更点を検知して警告を出しながらもロードを行う等ですが
|
77
|
+
もう一つ、ハッシュ引数で指定する事で、テーブル名、ファイル名を一致させなくても フィクスチャ を読み込ませることができます
|
78
|
+
そのため、すべてのテストケースで依存関係を気にしながら共通のfixtureを使わなくても良い様に出来ます
|
79
|
+
|
80
|
+
describe ItemShopController do
|
81
|
+
flextures :items, :users => :users_for_itmshop # users_for_itemshop.csv をロードする
|
82
|
+
|
83
|
+
=== Flextures factory filter
|
84
|
+
|
85
|
+
Railsのプロジェクトに config/flextures.factory.rb というファイルを作成して、そこにフィルタを記述することによって
|
86
|
+
フィクスチャの読み込み時に、値を加工して読み込む事が可能になっています
|
87
|
+
例えば、次の様に記述するとusersテーブルのlast_login_dateの値を、常に現在の時間として設定できます
|
88
|
+
|
89
|
+
Flextures::Factory.define :users do |f|
|
90
|
+
f.last_login_date = DateTime.now
|
91
|
+
end
|
92
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "flextures"
|
18
|
+
gem.homepage = "http://github.com/baban/flextures"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{load and dump fixtures}
|
21
|
+
gem.description = %Q{load and dump fixtures}
|
22
|
+
gem.email = "babanba.n@gmail.com"
|
23
|
+
gem.authors = ["baban"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
test.rcov_opts << '--exclude "gems/*"'
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "flextures #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
rdoc.options = ["--charset", "utf-8", "--line-numbers"]
|
54
|
+
end
|
55
|
+
|
56
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.9.0
|
data/flextures.gemspec
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{flextures}
|
8
|
+
s.version = "0.9.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["baban"]
|
12
|
+
s.date = %q{2011-11-03}
|
13
|
+
s.description = %q{load and dump fixtures}
|
14
|
+
s.email = %q{babanba.n@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"CHANGELOG",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"History.txt",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"flextures.gemspec",
|
30
|
+
"init.rb",
|
31
|
+
"lib/flextures.rb",
|
32
|
+
"lib/flextures_base_config.rb",
|
33
|
+
"lib/flextures_extension_modules.rb",
|
34
|
+
"lib/flextures_factory.rb",
|
35
|
+
"lib/rspec_flextures_support.rb",
|
36
|
+
"lib/tasks/flextures.rake",
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_flextures.rb"
|
39
|
+
]
|
40
|
+
s.homepage = %q{http://github.com/baban/flextures}
|
41
|
+
s.licenses = ["MIT"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = %q{1.6.2}
|
44
|
+
s.summary = %q{load and dump fixtures}
|
45
|
+
|
46
|
+
if s.respond_to? :specification_version then
|
47
|
+
s.specification_version = 3
|
48
|
+
|
49
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
50
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
51
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
53
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
54
|
+
else
|
55
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
58
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/init.rb
ADDED
@@ -0,0 +1,16 @@
|
|
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.rb
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Flextures
|
4
|
+
LOAD_DIR = Config.fixture_load_directory
|
5
|
+
DUMP_DIR = Config.fixture_dump_directory
|
6
|
+
# �������
|
7
|
+
module ARGS
|
8
|
+
# �����o���E�ǂݍ��� ���ׂ��t�@�C���ƃI�v�V��������������
|
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
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# ��{�ݒ���L�q����
|
4
|
+
module Flextures
|
5
|
+
module Config
|
6
|
+
@@configs={
|
7
|
+
fixture_load_directory: "spec/fixtures/",
|
8
|
+
fixture_dump_directory: "spec/fixtures/",
|
9
|
+
}
|
10
|
+
# �n�b�V����setter�Agetter�ɕϊ�
|
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
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Flextures
|
4
|
+
# Plug-in �����g��
|
5
|
+
class OpenStruct < ::OpenStruct
|
6
|
+
# hash�ɕω�������
|
7
|
+
def to_hash
|
8
|
+
h={}
|
9
|
+
(self.methods - OpenStruct.new.methods)
|
10
|
+
.select{ |name| name.match(/\w+=/) }
|
11
|
+
.map{ |name| name.to_s.gsub(/=/,'').to_sym }
|
12
|
+
.each{ |k| h[k]=self.send(k) }
|
13
|
+
h
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Extensions
|
18
|
+
module Array
|
19
|
+
def to_hash keys
|
20
|
+
h = {}
|
21
|
+
[keys,self].transpose.each{ |k,v| h[k]=v }
|
22
|
+
h
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
@@ -0,0 +1,19 @@
|
|
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
|
+
|
@@ -0,0 +1,32 @@
|
|
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
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'flextures'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flextures
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- baban
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-03 00:00:00.000000000 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda
|
17
|
+
requirement: &77603060 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *77603060
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
requirement: &77602820 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *77602820
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
requirement: &77602580 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.6.4
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *77602580
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rcov
|
50
|
+
requirement: &77602340 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *77602340
|
59
|
+
description: load and dump fixtures
|
60
|
+
email: babanba.n@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files:
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.rdoc
|
66
|
+
files:
|
67
|
+
- .document
|
68
|
+
- CHANGELOG
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- History.txt
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.rdoc
|
74
|
+
- Rakefile
|
75
|
+
- VERSION
|
76
|
+
- flextures.gemspec
|
77
|
+
- init.rb
|
78
|
+
- lib/flextures.rb
|
79
|
+
- lib/flextures_base_config.rb
|
80
|
+
- lib/flextures_extension_modules.rb
|
81
|
+
- lib/flextures_factory.rb
|
82
|
+
- lib/rspec_flextures_support.rb
|
83
|
+
- lib/tasks/flextures.rake
|
84
|
+
- test/helper.rb
|
85
|
+
- test/test_flextures.rb
|
86
|
+
has_rdoc: true
|
87
|
+
homepage: http://github.com/baban/flextures
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
segments:
|
101
|
+
- 0
|
102
|
+
hash: 804855759
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 1.6.2
|
112
|
+
signing_key:
|
113
|
+
specification_version: 3
|
114
|
+
summary: load and dump fixtures
|
115
|
+
test_files: []
|