flextures 1.9.8 → 1.9.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.9.8
1
+ 1.9.9
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "flextures"
8
- s.version = "1.9.8"
8
+ s.version = "1.9.9"
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 = "2012-04-20"
12
+ s.date = "2012-04-24"
13
13
  s.description = "load and dump fixtures"
14
14
  s.email = "babanba.n@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
33
33
  "lib/flextures/flextures.rake",
34
34
  "lib/flextures/flextures.rb",
35
35
  "lib/flextures/flextures_base_config.rb",
36
+ "lib/flextures/flextures_command.rb",
36
37
  "lib/flextures/flextures_dumper.rb",
37
38
  "lib/flextures/flextures_extension_modules.rb",
38
39
  "lib/flextures/flextures_factory.rb",
@@ -6,6 +6,7 @@ require 'flextures/flextures'
6
6
  require 'flextures/flextures_factory'
7
7
  require 'flextures/flextures_loader'
8
8
  require 'flextures/flextures_dumper'
9
+ require 'flextures/flextures_command'
9
10
  require 'flextures/flextures_railtie' if defined? Rails
10
11
  require 'flextures/rspec_flextures_support' if defined? RSpec
11
12
  require 'flextures/testunit_flextures_support' if defined? Test::Unit::TestCase
@@ -6,57 +6,32 @@ namespace :db do
6
6
  namespace :flextures do
7
7
  desc "Dump data to csv format"
8
8
  task :dump => :environment do
9
- table_names = Flextures::ARGS.parse
10
- Flextures::init_load
11
- puts "dumping..."
12
- if ["yml","yaml"].include? ENV["FORMAT"]
13
- table_names.each { |fmt| Flextures::Dumper::yml(fmt) }
14
- else
15
- table_names.each { |fmt| Flextures::Dumper::csv(fmt) }
16
- end
9
+ Flextures::Rake::Command::dump
17
10
  end
18
11
 
19
12
  desc "Dump data to prefer csv format"
20
13
  task :csvdump => :environment do
21
- table_names = Flextures::ARGS.parse
22
- Flextures::init_load
23
- table_names.each { |fmt| Flextures::Dumper::csv(fmt) }
14
+ Flextures::Rake::Command::csvdump
24
15
  end
25
16
 
26
17
  desc "Dump data to yaml format"
27
18
  task :ymldump => :environment do
28
- table_names = Flextures::ARGS.parse
29
- Flextures::init_load
30
- table_names.each { |fmt| Flextures::Dumper::yml(fmt) }
19
+ Flextures::Rake::Command::ymldump
31
20
  end
32
21
 
33
22
  desc "load fixture data csv format"
34
23
  task :load => :environment do
35
- table_names = Flextures::ARGS.parse
36
- Flextures::init_load
37
- Flextures::init_tables
38
- puts "loading..."
39
- if ["yml","yaml"].include? ENV["FORMAT"]
40
- table_names.each { |fmt| Flextures::Loader::yml(fmt) }
41
- else
42
- table_names.each { |fmt| Flextures::Loader::csv(fmt) }
43
- end
24
+ Flextures::Rake::Command::load
44
25
  end
45
26
 
46
27
  desc "load fixture data only csv format files"
47
28
  task :csvload => :environment do
48
- table_names = Flextures::ARGS.parse
49
- Flextures::init_load
50
- Flextures::init_tables
51
- table_names.each { |fmt| Flextures::Loader::csv(fmt) }
29
+ Flextures::Rake::Command::csvload
52
30
  end
53
31
 
54
32
  desc "load fixture files only yaml format"
55
33
  task :ymlload => :environment do
56
- table_names = Flextures::ARGS::parse
57
- Flextures::init_load
58
- Flextures::init_tables
59
- table_names.each { |fmt| Flextures::Loader::yml(fmt) }
34
+ Flextures::Rake::Command::ymlload
60
35
  end
61
36
  end
62
37
  end
@@ -43,19 +43,32 @@ module Flextures
43
43
  end
44
44
  end
45
45
 
46
- # テーブル情報の初期化
47
- def self.init_tables(ignore_tables=[])
46
+ # 前テーブル削除のときにほんとうに消去して良いテーブル一覧を返す
47
+ def self.deletable_tables
48
48
  tables = ActiveRecord::Base.connection.tables
49
49
  tables.delete "schema_migrations"
50
- tables.delete ignore_tables
50
+ tables
51
+ end
52
+
53
+ # テーブル情報の初期化
54
+ def self.init_tables
55
+ tables = Flextures::deletable_tables
51
56
  tables.each do |name|
52
57
  # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう
53
58
  begin
54
- klass = Class.new(ActiveRecord::Base){ |o| o.table_name= name }
55
- klass.delete_all
59
+ Class.new(ActiveRecord::Base){ |o| o.table_name= name }.delete_all
60
+ rescue => e
61
+ end
62
+ end
63
+ end
64
+
65
+ # テーブル情報の初期化
66
+ def self.delete_tables *tables
67
+ tables.each do |name|
68
+ # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう
69
+ begin
70
+ Class.new(ActiveRecord::Base){ |o| o.table_name= name }.delete_all
56
71
  rescue => e
57
- p :init_table_error
58
- p klass.table_name
59
72
  end
60
73
  end
61
74
  end
@@ -63,8 +76,7 @@ module Flextures
63
76
  # デバッグ用のメソッド、渡されたブロックを実行する
64
77
  # 主にテーブルの今の中身を覗きたい時に使う
65
78
  def self.table_tap &dumper
66
- tables = ActiveRecord::Base.connection.tables
67
- tables.delete "schema_migrations"
79
+ tables = Flextures::deletable_tables
68
80
  tables.each do |name|
69
81
  # テーブルではなくviewを拾って止まる場合があるのでrescueしてしまう
70
82
  begin
@@ -87,7 +99,7 @@ module Flextures
87
99
  table_names = (ENV["M"] or ENV["MODEL"]).split(',').map{ |name| { table: name.constantize.table_name } }
88
100
  end
89
101
  if table_names.empty?
90
- table_names = ActiveRecord::Base.connection.tables.map{ |name| { table: name } }
102
+ table_names = Flextures::deletable_tables.map{ |table| { table: table } }
91
103
  end
92
104
  # ENV["FIXTURES"]の中身を解析
93
105
  fixtures_args_parser =->(s){
@@ -100,7 +112,7 @@ module Flextures
100
112
  table_names = fixtures_args_parser.call ENV["F"] if ENV["F"]
101
113
  table_names = table_names.map{ |option| option.merge dir: ENV["DIR"] } if ENV["DIR"]
102
114
  # read mode だとcsvもyaml存在しないファイルは返さない
103
- table_names.select! &exist if option[:mode] && option[:mode].to_sym == :read
115
+ table_names.select! &exist if option[:mode] && option[:mode] == 'read'
104
116
  table_names
105
117
  end
106
118
 
@@ -0,0 +1,65 @@
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
+ module Rake
13
+ module Command
14
+ def self.dump
15
+ Flextures::init_load
16
+ table_names = Flextures::ARGS.parse
17
+ puts "dumping..."
18
+ if ["yml","yaml"].member? ENV["FORMAT"]
19
+ table_names.map { |fmt| Flextures::Dumper::yml(fmt) }
20
+ else
21
+ table_names.map { |fmt| Flextures::Dumper::csv(fmt) }
22
+ end
23
+ end
24
+
25
+ def self.csvdump
26
+ Flextures::init_load
27
+ puts "dumping..."
28
+ table_names = Flextures::ARGS.parse
29
+ table_names.map { |fmt| Flextures::Dumper::csv(fmt) }
30
+ end
31
+
32
+ def self.ymldump
33
+ Flextures::init_load
34
+ puts "dumping..."
35
+ table_names = Flextures::ARGS.parse
36
+ table_names.map { |fmt| Flextures::Dumper::yml(fmt) }
37
+ end
38
+
39
+ def self.load
40
+ table_names = Flextures::ARGS.parse
41
+ Flextures::init_load
42
+ Flextures::init_tables
43
+ puts "loading..."
44
+ table_names.map { |fmt| Flextures::Loader::load(fmt) }
45
+ end
46
+
47
+ def self.csvload
48
+ table_names = Flextures::ARGS.parse
49
+ Flextures::init_load
50
+ Flextures::init_tables
51
+ puts "loading..."
52
+ table_names.map { |fmt| Flextures::Loader::csv(fmt) }
53
+ end
54
+
55
+ def self.ymlload
56
+ table_names = Flextures::ARGS::parse
57
+ Flextures::init_load
58
+ Flextures::init_tables
59
+ puts "loading..."
60
+ table_names.map { |fmt| Flextures::Loader::yml(fmt) }
61
+ end
62
+ end
63
+ end
64
+ end
65
+
@@ -10,7 +10,8 @@ module Flextures
10
10
  if format == :yml
11
11
  return "null" if d.nil?
12
12
  end
13
- d.to_s
13
+ return nil if d.nil?
14
+ Base64.encode64(d)
14
15
  },
15
16
  boolean:->( d, format ){
16
17
  if format == :yml
@@ -132,6 +133,7 @@ module Flextures
132
133
  csv<< attr_type.map { |h| trans(row[h[:name]], h[:type], :csv) }
133
134
  end
134
135
  end
136
+ outfile
135
137
  end
136
138
 
137
139
  # yaml で fixtures を dump
@@ -142,7 +144,6 @@ module Flextures
142
144
  table_name = format[:table]
143
145
  klass = PARENT::create_model(table_name)
144
146
  attributes = klass.columns.map { |colum| colum.name }
145
-
146
147
  columns = klass.columns
147
148
  # テーブルからカラム情報を取り出し
148
149
  column_hash = {}
@@ -161,6 +162,7 @@ module Flextures
161
162
  }.join
162
163
  end
163
164
  end
165
+ outfile
164
166
  end
165
167
  end
166
168
  end
@@ -31,7 +31,8 @@ module Flextures
31
31
  # 型の変換を行う
32
32
  TRANSLATER = {
33
33
  binary:->(d){
34
- d.to_i
34
+ return d if d.nil?
35
+ Base64.encode64(d)
35
36
  },
36
37
  boolean:->(d){
37
38
  return d if d.nil?
@@ -79,14 +80,36 @@ module Flextures
79
80
  },
80
81
  }
81
82
 
82
- # csv 優先で存在している fixtures をロード
83
- def self.load format
83
+ # どのファイルが存在するかチェック
84
+ # @param [Hash] format ロードしたいファイルの情報
85
+ # @return 存在するファイルの種類(csv,yml)、どちも存在しないならnil
86
+ def self.file_exist format, type = [:csv,:yml]
87
+ table_name = format[:table].to_s
84
88
  file_name = format[:file] || format[:table]
85
89
  dir_name = format[:dir] || LOAD_DIR
86
- method = nil
87
- method = :csv if File.exist? "#{dir_name}#{file_name}.csv"
88
- method = :yml if File.exist? "#{dir_name}#{file_name}.yml"
89
- self::send(method, format) if method
90
+
91
+ ext=->{
92
+ if type.member?(:csv) and File.exist? "#{dir_name}#{file_name}.csv"
93
+ :csv
94
+ elsif type.member?(:yml) and File.exist? "#{dir_name}#{file_name}.yml"
95
+ :yml
96
+ else
97
+ nil
98
+ end
99
+ }.call
100
+
101
+ [table_name, "#{dir_name}#{file_name}",ext]
102
+ end
103
+
104
+ # csv 優先で存在している fixtures をロード
105
+ def self.load format
106
+ table_name, file_name, method = file_exist format
107
+ if method
108
+ self::send(method, format)
109
+ else
110
+ # ファイルが存在しない時
111
+ print "Warning: #{file_name} is not exist!\n"
112
+ end
90
113
  end
91
114
 
92
115
  # fixturesをまとめてロード、主にテストtest/unit, rspec で使用する
@@ -99,7 +122,7 @@ module Flextures
99
122
  # fixtures :users => :users2
100
123
  def self.flextures *fixtures
101
124
  # :allですべてのfixtureを反映
102
- fixtures = ActiveRecord::Base.connection.tables if fixtures.size== 1 and :all == fixtures.first
125
+ fixtures = Flextures::deletable_tables if fixtures.size== 1 and :all == fixtures.first
103
126
  fixtures_hash = fixtures.pop if fixtures.last and fixtures.last.is_a? Hash # ハッシュ取り出し
104
127
  fixtures.each{ |table_name| Loader::load table: table_name }
105
128
  fixtures_hash.each{ |k,v| Loader::load table: k, file: v } if fixtures_hash
@@ -108,15 +131,15 @@ module Flextures
108
131
 
109
132
  # CSVのデータをロードする
110
133
  def self.csv format
111
- table_name = format[:table].to_s
112
- file_name = format[:file] || table_name
113
- dir_name = format[:dir] || LOAD_DIR
114
- inpfile = "#{dir_name}#{file_name}.csv"
134
+ table_name, file_name, ext = file_exist format, [:csv]
135
+
136
+ return nil unless File.exist? "#{file_name}.csv"
137
+
115
138
  klass = PARENT::create_model table_name
116
139
  attributes = klass.columns.map &:name
117
140
  filter = create_filter klass, Factory[table_name], file_name, :csv
118
141
  klass.delete_all
119
- CSV.open( inpfile ) do |csv|
142
+ CSV.open( "#{file_name}.csv" ) do |csv|
120
143
  keys = csv.shift # keyの設定
121
144
  warning "CSV", attributes, keys
122
145
  csv.each do |values|
@@ -125,23 +148,25 @@ module Flextures
125
148
  o.save
126
149
  end
127
150
  end
151
+ "#{file_name}.csv"
128
152
  end
129
153
 
130
154
  # YAML形式でデータをロードする
131
155
  def self.yml format
132
- table_name = format[:table].to_s
133
- file_name = format[:file] || table_name
134
- dir_name = format[:dir] || LOAD_DIR
135
- inpfile = "#{dir_name}#{file_name}.yml"
156
+ table_name, file_name, ext = file_exist format, [:yml]
157
+
158
+ return nil unless File.exist? "#{file_name}.yml"
159
+
136
160
  klass = PARENT::create_model table_name
137
161
  attributes = klass.columns.map &:name
138
162
  filter = create_filter klass, Factory[table_name], file_name, :yml
139
163
  klass.delete_all
140
- YAML.load(File.open(inpfile)).each do |k,h|
164
+ YAML.load(File.open("#{file_name}.yml")).each do |k,h|
141
165
  warning "YAML", attributes, h.keys
142
166
  o = filter.call h
143
167
  o.save
144
168
  end
169
+ "#{file_name}.yml"
145
170
  end
146
171
 
147
172
  # 欠けたカラムを検知してメッセージを出しておく
@@ -165,11 +190,12 @@ module Flextures
165
190
  h.select! { |k,v| column_hash[k] }
166
191
  o = klass.new
167
192
  # 値がnilでないなら型をDBで適切なものに変更
168
- h.each{ |k,v| nil==v || o[k] = TRANSLATER[column_hash[k].type].call(v) }
169
- not_nullable_columns.each{ |k| o[k]==nil && o[k] = TRANSLATER[column_hash[k].type].call(k) }
193
+ h.each{ |k,v| nil==v || o[k] = (TRANSLATER[column_hash[k].type] ? TRANSLATER[column_hash[k].type].call(v) : nil) }
170
194
  # FactoryFilterを動作させる
171
195
  factory.call(*[o, :load, filename, ext][0,factory.arity]) if factory
172
196
  # 値がnilの列にデフォルト値を補間
197
+ not_nullable_columns.each{ |k| o[k]==nil && o[k] = (COMPLETER[column_hash[k].type] ? COMPLETER[column_hash[k].type].call : nil) }
198
+ # 列ごと抜けているデータを保管
173
199
  lack_columns.each { |k| nil==o[k] && o[k] = COMPLETER[column_hash[k].type].call }
174
200
  o
175
201
  }
@@ -4,16 +4,32 @@
4
4
  module RSpec
5
5
  module Core
6
6
  module Hooks
7
+ # 引数で渡されたファイルを読み込みする
7
8
  def flextures *_
8
9
  before { Flextures::Loader::flextures *_ }
9
10
  end
11
+
12
+ # 引数で渡されたテーブルのデータをdeleteする
13
+ def flextures_delete *_
14
+ before {
15
+ if _.empty?
16
+ Flextures::init_tables
17
+ else
18
+ Flextures::delete_tables *_
19
+ end
20
+ }
21
+ end
22
+
23
+ def flextures_set_config
24
+ # TODO: ハッシュで渡された設定をセットする
25
+ end
10
26
  end
11
27
  end
12
28
 
13
29
  module Rails
14
30
  module FlextureSupport
15
- @@configs={ load_count: 0 }
16
31
  def self.included(m)
32
+ # 実行前にテーブルの初期化
17
33
  Flextures::init_tables
18
34
  end
19
35
  end
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: 1.9.8
4
+ version: 1.9.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -67,6 +67,7 @@ files:
67
67
  - lib/flextures/flextures.rake
68
68
  - lib/flextures/flextures.rb
69
69
  - lib/flextures/flextures_base_config.rb
70
+ - lib/flextures/flextures_command.rb
70
71
  - lib/flextures/flextures_dumper.rb
71
72
  - lib/flextures/flextures_extension_modules.rb
72
73
  - lib/flextures/flextures_factory.rb
@@ -89,7 +90,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
90
  version: '0'
90
91
  segments:
91
92
  - 0
92
- hash: 294980813
93
+ hash: 497886195
93
94
  required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  none: false
95
96
  requirements: